教堂更新/bible-linker

在 https://bible.com 上查找并替换所有圣经引用为对应章节的链接

v1.0.1 2023-10-11 21:23 UTC

This package is not auto-updated.

Last update: 2024-09-26 02:24:05 UTC


README

解析HTML编码的字符串,查找圣经引用并替换为Bible.com的链接。

特性

  1. 链接到单个经文
     BibleLinker::linkify("Psalm 119:105", "en");
     /*
      * <a href="https://www.bible.com/bible/111/PSA.119.105.NIV">Psalm 119:105</a>
      */
    
  2. 链接到整个章节
     BibleLinker::linkify("John 3", "en");
     /*
      * <a href="https://www.bible.com/bible/111/JHN.3..NIV">John 3</a>
      */
    
  3. 链接到经文范围
     BibleLinker::linkify("John 3:15-17", "en");
     /*
      * <a href="https://www.bible.com/bible/111/JHN.3.15-17.NIV">John 3:15-17</a>
      */
    
  4. 链接到多个经文
     BibleLinker::linkify("John 3:16, 18", "en");
     /*
      * <a href="https://www.bible.com/bible/111/JHN.3.16.NIV">John 3:16</a>,
      * <a href="https://www.bible.com/bible/111/JHN.3.18.NIV">18</a>
      */
    
  5. 指定版本
     BibleLinker::linkify("John 3:16 (ESV)", "en");
     /*
      * <a href="https://www.bible.com/bible/59/JHN.3.16.ESV">John 3:16</a> (ESV)
      */
    
  6. 使用常见缩写缩写书名
     BibleLinker::linkify("Genesis 1:1", "en");
     BibleLinker::linkify("Gen 1:1", "en");
     BibleLinker::linkify("Ge 1:1", "en");
     BibleLinker::linkify("Gn 1:1", "en");
     /*
      * <a href="https://www.bible.com/bible/111/GEN.1.1.NIV">Genesis 1:1</a>
      * <a href="https://www.bible.com/bible/111/GEN.1.1.NIV">Gen 1:1</a>
      * <a href="https://www.bible.com/bible/111/GEN.1.1.NIV">Ge 1:1</a>
      * <a href="https://www.bible.com/bible/111/GEN.1.1.NIV">Gn 1:1</a>
      */
    
  7. 以上所有组合
     BibleLinker::linkify("Ge 1:1,2-3; 4; 5:6,7,8; 9:10 (NLT)", "en");
     /*
      * <a href="https://www.bible.com/bible/116/GEN.1.1.NLT">Ge 1:1</a>,
      * <a href="https://www.bible.com/bible/116/GEN.1.2-3.NLT">2-3</a>;
      * <a href="https://www.bible.com/bible/116/GEN.4..NLT">4</a>;
      * <a href="https://www.bible.com/bible/116/GEN.5.6.NLT">5:6</a>,
      * <a href="https://www.bible.com/bible/116/GEN.5.7.NLT">7</a>,
      * <a href="https://www.bible.com/bible/116/GEN.5.8.NLT">8</a>;
      * <a href="https://www.bible.com/bible/116/GEN.9.10.NLT">9:10</a> (NLT)
      */
    
  8. 单章节书籍不需要章节
     BibleLinker::linkify("3 John 4", "en");
     BibleLinker::linkify("3 John 1:4", "en");
     /*
      * <a href="https://www.bible.com/bible/111/3JN.1.4.NIV">3 John 4</a>
      * <a href="https://www.bible.com/bible/111/3JN.1.4.NIV">3 John 1:4</a>
      */
    
  9. 不存在的章节和经文不会链接
     BibleLinker::linkify("Psalm 150; 151", "en"); // Psalm 151 doesn't exist
     BibleLinker::linkify("3 John 1-16", "en"); // 3 John 15-16 don't exist
     /*
      * <a href="https://www.bible.com/bible/111/PSA.150..NIV">Psalm 150</a>; 151
      * <a href="https://www.bible.com/bible/111/3JN.1.1-14.NIV">3 John 1-16</a>
      */
    
  10. 允许内联标签
     BibleLinker::linkify("<span>John</span> 15:<span style='color:blue'>5</span>-6", "en"); // John 15:5-6
     /*
      * <span><a href="https://www.bible.com/bible/111/JHN.15.5-6.NIV">John 15:5-6</a></span><span style="color:blue"></span>
      */
    
  11. 块级标签不被尊重,就像内联标签一样
     BibleLinker::linkify("<p>Review John 15</p><p>:5</p>", "en"); // John 15 (not John 15:5)
     /*
      * <p>Review <a href="https://www.bible.com/bible/111/JHN.15..NIV">John 15</a></p><p>:5</p>
      */
    

支持的语言

  1. 英语(默认翻译:NIV)
  2. 西班牙语(默认翻译:RVR1960)
  3. 法语(默认翻译:LSG)
  4. 葡萄牙语(默认翻译:NVI)

在项目中包含

  1. 运行 composer require church-renewal/bible-linker

使用示例

use ChurchRenewal\BibleLinker\BibleLinker;

/**
 * @param string - a two character language ID
 * @return bool - true if language is supported by Bible Linker
 */
BibleLinker::isLanguageSupported('es')
/*
 * true
 */

/**
 * @param string - must be HTML encoded
 * @param string - a two character language ID
 * @param array - optional: associative array of attributes to be added to the A tag
 * @return array - assocative array with keys:
 *     'new':                   int - number of new links inserted into content
 *     'alternative':           int - number of translations found in content which couldn't be found, but have known alternatives
 *     'unknown':               int - number translations found in content which couldn't be found on Bible.com
 *     'unknownTranslations':   array - an array of strings of the unknown translations
 *     'html':                  string - the content with links inserted
 *     'errors' :               array - an associative array of parsing errors; each key is the string that failed, each value is the error code
 */
BibleLinker::linkify('Psalm 119:105', 'en', ['class' => 'verse', 'target' => '_blank']);
/*
 * [
 *     'new' => 1
 *     'alternative' => 0
 *     'unknown' => 0
 *     'unknownTranslations' => []
 *     'html' => '<a href="https://www.bible.com/bible/111/PSA.119.105.NIV" class="verse" target="_blank">Psalm 119:105</a>',
 *     'errors' => []
 * ]
 */

已知限制

  1. HTML内容必须是HTML编码的。例如,TinyMCE的输出将正常工作。

     // good
     BibleLinker::linkify(htmlentities("Hébreux 12:2"), "fr");
     BibleLinker::linkify("H&eacute;breux 12:2", "fr");
    
     // bad
     BibleLinker::linkify("Hébreux 12:2", "fr");
    
  2. 经文必须用逗号分隔

     // good
     BibleLinker::linkify("Psalm 119:105, 106", "en");
    
     // bad
     BibleLinker::linkify("Psalm 119:105 & 106", "en");
    
  3. 章节必须用分号分隔

     // good
     BibleLinker::linkify("Psalm 119; 120", "en");
    
     // bad
     BibleLinker::linkify("Psalm 119, 120", "en");
    
  4. 翻译必须用括号包围

     // good
     BibleLinker::linkify("Psalm 119:105 (ESV)", "en");
    
     // bad
     BibleLinker::linkify("Psalm 119:105 ESV", "en");
    
  5. 不能指定多个翻译

     // good
     BibleLinker::linkify("Psalm 118 (NASB); Psalm 119:105 (ESV)", "en");
    
     // bad
     BibleLinker::linkify("Psalm 118 (NASB); 119:105 (ESV)", "en");
    

在开发中运行测试

安装Docker

./runtests.sh