User:Bookofjude/Verse conversion

From Wikisource
Jump to navigation Jump to search
PHP Bible standardisation script

Run locally. Change "bible.txt" to whatever text file has the contents of the page you wish to edit, or change the content of "bible.txt" to contain the content of the Bible book you wish to update.

40274PHP Bible standardisation script
<?php
// set $verbose to True for detailed output
$verbose = False;

// get the input, line by line
$handle = fopen("bible.txt", "r");

// regexp for matching the pattern
$chapter_pattern = "@==[\s]?[Cc]hapter[\s]?([0-9]+)[\s]?==@";

// There are multiple verse formats. Comment out the one that '''isn't''' used.

// regexp 1 for matching the verse number 
$verse_pattern = "@<small><font color=#0000FF>([0-9]+)</font color></small>@";

// regexp 2
$verse_pattern = "@<small style=\"color:blue\">([0-9]+)</small>@";

// if verbose, note that the corrected text can be found at the end of the likely long information
($verbose) ? print("Verbose output enabled. Scroll to the end of this output for the corrected text.<br>"): print("");

// begin while look
while (!feof($handle)):
	// get line from $handle
	$buffer = fgets($handle, 1024);
	// check for chapter match
	if (preg_match($chapter_pattern, $buffer, $chapter_match)):
		// ->chapter match, set chapter to $1 "([0-9]+)"
		$chapter = $chapter_match[1]; 
		// if verbose, detail output
		($verbose) ? print("Found chapter: " . $chapter . "<br>"): print("");
	// not a chapter match
	else:
		// check for verse
		if (preg_match($verse_pattern, $buffer, $verse_match)):
			// set verse for verbose
			$verse = $verse_match[1];
			// if verbose, detailed output
			($verbose) ? print("Found verse: " . $verse . ". Applying fix. (" . $chapter . ":" . $verse . ")<br>"): print("");
			// make the new verse with the current chapter
			$verse_new = "{{verse|chapter=" . $chapter . "|verse=$1}}";
			// s/$verse_pattern/$verse_new
			$buffer = preg_replace($verse_pattern, $verse_new, $buffer);
		else:
			// if verbose, detailed output
			($verbose) ? print("Not a verse. Skipping...<br>"): print("");
		endif;
	endif;
	// append buffer to data
	$data .= $buffer;
endwhile;
// if verbose, detailed output
($verbose) ? print("End of file. Display corrected text:<br><br>"): print("");

// output data
echo "<textarea cols=100 rows=40>" . $data . "</textarea>";
?>