<?php if (!defined('PmWiki')) exit();
/*
    bibleref.php
    Copyright 2021 Peter Bowers
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    Script to replace Bible references with expanded verses using BibleGateway ref tool
*/
$RecipeInfo['FileList']['Version'] = '2021-08-21';

# if (:bibleref [...]:) is included in your page then BibleGateway will replace Bible
# references with a special link including tooltip and link to the reference in question
# See https://www.biblegateway.com/share/#reftag
# Options:
#   version=ESV or version=NLT or etc. (default version=NIV)
#   clickTooltip=X (X is either "true" or "false", default false)
#   showTooltips=X (X is either "true" or "false", default true)
Markup('bibleref', 'directives', 
  '/\\(:bibleref\\s*(.*?):\\)/i',
  'BibleRef');

# if (:biblesearch [...]:) is included in your page then a form will be inserted
# providing the capability to search the Bible via BibleGateway
# See https://www.biblegateway.com/share/#reftag
# Options:
#   title="Title at top of form" (default "Lookup a word or passage in the Bible")
#   buttontext="Text On Search Button" (default "Search BibleGateway.com")
#   logo=0 (default is to show a BibleGateway logo; you can turn it off with this option)
Markup('biblesearch', 'directives', 
  '/\\(:biblesearch\\s*(.*?):\\)/i',
  'BibleSearch');

# Replace (:biblevotd [...]:) with a verse of the day from BibleGateway
# See https://www.biblegateway.com/usage/votd/custom_votd/
# Options:
#   version=ESV or version=NLT or etc. (default version=NIV)
Markup('biblevotd', 'directives', 
  '/\\(:biblevotd\\s*(.*?):\\)/i',
  'BibleVOTD');

# Insert script from BG to replace Bible references with a styled & functional link
# See https://www.biblegateway.com/share/#reftag
function BibleRef($m) {
	global $HTMLFooterFmt;
	extract($GLOBALS['MarkupToHTML']);
	$args = ParseArgs($m[1]);
	SDVA($args, [ 'version'=>'NIV', 'clickTooltip'=>'false', 'showTooltips'=>'true' ] );
	$HTMLFooterFmt[] = <<<EOF
<script src="https://www.biblegateway.com/public/link-to-us/tooltips/bglinks.js" type="text/javascript"></script>
<script type="text/javascript">
BGLinks.version = "$args[version]";
BGLinks.clickTooltip = $args[clickTooltip];
BGLinks.showTooltips = $args[showTooltips];
BGLinks.linkVerses();
</script>
EOF;
	return ""; // replace with nothing
}

# Display a Verse of the Day from BibleGateway
# See https://www.biblegateway.com/usage/votd/custom_votd/
function BibleVOTD($m) {
	extract($GLOBALS['MarkupToHTML']);
	$args = ParseArgs($m[1]);
	SDVA($args, [ 'version'=>'NIV' ] );
	return Keep(<<<EOF
<script src="https://www.biblegateway.com/votd/votd.write.callback.js"></script>
<script src="https://www.biblegateway.com/votd/get/?format=json&version=$args[version]&callback=BG.votdWriteCallback"></script>
<!-- alternative for no javascript -->
<noscript>
<iframe framespacing="0" frameborder="no" src="https://www.biblegateway.com/votd/get/?format=html&version=$args[version]">View Verse of the Day</iframe>
</noscript>
EOF);
}

# Provide a form to search BibleGateway
# See https://www.biblegateway.com/usage/form/
function BibleSearch($m) {
	extract($GLOBALS['MarkupToHTML']);
	$args = ParseArgs($m[1]);
	SDVA($args, [ 'title'=>'Lookup a word or passage in the Bible', 'buttontext'=>'Search BibleGateway.com', 'logo'=>1 ] );
	$logo = ($args['logo'] ? '<a href="https://www.biblegateway.com/" title="The Bible in multiple languages, versions, and formats"> <img src="https://www.biblegateway.com/assets/images/logos/bglogo_sm.gif?103106" width="146" height="44" alt="BibleGateway.com" border="0" /></a><br />' : '');
	return Keep(<<<EOF
<form action="https://www.biblegateway.com/quicksearch/">
<table border="1" cellspacing="0" cellpadding="2" style="border-color: #600;">
<tr><th style="background-color: #600; color:#fff; text-align: center; padding: 0 10px;">$args[title]</th></tr>
<tr><td style="background-color: #fff; text-align: center; padding: 0 10px;">
<div style="margin-bottom: 0;">
<input style="margin:2px" type="text" name="quicksearch" /><br />
<input style="margin:2px" type="submit" value="$args[buttontext]" /><br />
</div>
$logo
</td></tr>
</table>
</form>
EOF);
}