/** * Imports BibleWorks $data as USFM code. * This is $data fit for the BibleWorks Version Database Compiler. * It a string of USFM code. * $keepTags: Whether to keep the grammatical tags in the BibleWorks text. * These are the tags between () or <>. */ public static function import($data, $keepTags) { // Databases. $database_books = Database_Books::getInstance(); // Storage for the generated USFM. $usfm = array(); // The data comes as one string. Make it an array. $data = explode("\n", $data); // Book / chapter trackers. $currentBibleWorksBookAbbreviation = ""; $currentChapter = 0; // Go through each line of data to be imported. foreach ($data as $line) { $line = trim($line); // Get the name of the book and remove the text fragment. // Upon encountering a new book, it generates USFM code for it. $bookAbbreviation = substr($line, 0, 3); $line = substr($line, 3, 10000); $line = trim($line); if ($bookAbbreviation != $currentBibleWorksBookAbbreviation) { $currentBibleWorksBookAbbreviation = $bookAbbreviation; $currentChapter = 0; $bookID = Filter_Books::interpretBook($bookAbbreviation); $book = $database_books->getUsfmFromId($bookID); $usfm[] = '\\id ' . $book; } // Get the chapter number and remove the text fragment. // Upon encountering a new chapter, it generates USFM code for it. $chapter = (int) $line; $line = substr($line, strlen($chapter) + 1, 10000); $line = trim($line); if ($chapter != $currentChapter) { $currentChapter = $chapter; $usfm[] = '\\c ' . $currentChapter; $usfm[] = '\\p'; } // Get the verse number and remove the text fragment and whitespace. $verse = (int) $line; $line = substr($line, strlen($verse), 10000); $line = trim($line); // Convert markup for italics and footnotes. $line = Filter_Bibleworks::italics($line); $line = Filter_Bibleworks::notes($line); // Deal with the grammatical tags. if (!$keepTags) { $malformed = array(); $line = Filter_Bibleworks::parenthesis($line, $malformed); $line = Filter_Bibleworks::chevrons($line, $malformed); } // Output the verse. $usfm[] = '\\v ' . $verse . ' ' . $line; } $usfm = implode("\n", $usfm); return $usfm; }
$success_message = ""; $error_message = ""; // The name of the Bible. $bible = Access_Bible::clamp($_GET['bible']); $view->view->bible = Filter_Html::sanitize($bible); // Data submission. if (isset($_POST['submit'])) { // Whether to keep the grammatical tags. $tags = isset($_POST['tags']); // The BibleWorks text. $data = $_POST['data']; $data = trim($data); if ($data != "") { if (Validate_Utf8::valid($data)) { // Convert the BibleWorks text to USFM. $usfm = Filter_Bibleworks::import($data, $tags); // Import the USFM. $datafile = tempnam(sys_get_temp_dir(), ''); file_put_contents($datafile, $usfm); $success_message = Locale_Translate::_("Import has started. See Journal for progress."); $workingdirectory = dirname(__FILE__); Tasks_Logic::queue(Tasks_Logic::PHP, array("{$workingdirectory}/importcli.php", $datafile, $bible)); } else { $error_message = Locale_Translate::_("Please supply valid Unicode UTF-8 text."); } } else { $success_message = Locale_Translate::_("Nothing was imported."); } } @($view->view->success_message = $success_message); @($view->view->error_message = $error_message);
public function testChevrons5() { $malformed = array(); $data = Filter_Bibleworks::chevrons("Text in chevrons<06030; abc>.", $malformed); $this->assertEquals($data, 'Text in chevrons<06030; abc>.'); $this->assertEquals($malformed, array('<06030; abc>')); }