Commit fff86752 authored by jhodgdon's avatar jhodgdon

Issue #2859434 by jhodgdon: Refactor addnames script so it is more flexible...

Issue #2859434 by jhodgdon: Refactor addnames script so it is more flexible for future preprocessing
parent 3ab07258
......@@ -22,7 +22,7 @@ do
fi
# Run the preprocessor that puts file names into the files under each header.
php addnames._php ../source/$lang ../output/html_feed/$lang
php preprocess._php ../source/$lang ../output/html_feed/$lang f
cp ../source/$lang/guide-docinfo.xml ../output/html_feed/$lang
# Run the AsciiDoc processor to convert to DocBook format. Syntax:
......@@ -48,7 +48,7 @@ done
mkdir -p ../output/html_feed/guidelines
mkdir -p ../output/html_feed/guidelines/images
php addnames._php ../guidelines ../output/html_feed/guidelines
php preprocess._php ../guidelines ../output/html_feed/guidelines f
asciidoc -d book -b docbook -f std.conf -f guidelines.conf -o ../output/html_feed/guidelines/guidelines.docbook ../output/html_feed/guidelines/guidelines.txt
......
......@@ -21,7 +21,7 @@ do
fi
# Run the preprocessor that puts file names into the files under each header.
php addnames._php ../source/$lang ../output/html/$lang
php preprocess._php ../source/$lang ../output/html/$lang f
cp ../source/$lang/guide-docinfo.xml ../output/html/$lang
# Run the AsciiDoc processor to convert to DocBook format. Syntax:
......@@ -47,7 +47,7 @@ done
mkdir -p ../output/html/guidelines
mkdir -p ../output/html/guidelines/images
php addnames._php ../guidelines ../output/html/guidelines
php preprocess._php ../guidelines ../output/html/guidelines f
asciidoc -d book -b docbook -f std.conf -f guidelines.conf -o ../output/html/guidelines/guidelines.docbook ../output/html/guidelines/guidelines.txt
......
......@@ -2,25 +2,30 @@
/**
* @file
* Adds source file names to AsciiDoc text.
* Preprocesses AsciiDoc text.
*
* Command-line arguments: Source directory, output directory. Use with care --
* things in the output directory will be overwritten.
* Use with care -- things in the output directory will be overwritten!
*
* Processes each .txt file in the source directory, by adding
* @code
* pass:[<remark>Source file: (filename)</remark>]
* @endcode
* after each 2nd-level and 3rd-level header found in the file. The resulting
* files are written to the output directory with the same names.
* Command-line arguments: Source directory, output directory, steps
* where steps is a character string like fi
*
* Depending on the characters in steps, the following processing can happen,
* after which the resulting files are written to the output directory with
* the same names. Possible processing steps -- if steps contains:
* - f: Adds filenames. Specifically, adds:
* @code
* pass:[<remark>Source file: (filename)</remark>]
* @endcode
* after each 2nd-level and 3rd-level header found in the file.
*/
if (count($argv) < 3) {
exit("Usage: php (scriptname) source output");
if (count($argv) < 4) {
exit("Usage: php (scriptname) source output steps, where steps is some combination of characters fi");
}
$source = $argv[1];
$output = $argv[2];
$steps = $argv[3];
if (!is_dir($source) || !is_dir($output)) {
exit("Source or output directories are missing");
......@@ -33,7 +38,14 @@ if ($source == $output) {
if ($dh = opendir($source)) {
while (($file = readdir($dh)) !== FALSE) {
if (preg_match('|^[a-zA-Z_.\-]+\.txt$|', $file)) {
if (!process_file($file, $source, $output)) {
$text = file_get_contents($source . '/' . $file);
if ($text && strpos($steps, 'f') !== FALSE) {
$text = add_filenames($text, $file);
}
if ($text) {
$count = file_put_contents($output . '/' . $file, $text);
}
if (!$text || !$count) {
exit("Could not write to $file");
}
}
......@@ -42,28 +54,20 @@ if ($dh = opendir($source)) {
}
/**
* Processes an AsciiDoc file to add the file name after each header.
* Adds filenames to the text of a file.
*
* @param string $file
* Name of the file to process, without directory.
* @param string $source
* Source directory. Input file is $source . '/' . $file.
* @param string $output
* Output directory. Output file is $output . '/' . $file.
* @param string $text
* Text of the file.
* @param string $filename
* Name of the file being processed, without the directory.
* @param int[] depths
* (optional) Header depths to add file name to.
*
* @return int|FALSE
* The number of bytes written to the output file, or FALSE if there is an
* error.
* @return string
* Processed text of the file.
*/
function process_file($file, $source, $output, $depths = array(2,3)) {
$text = file_get_contents($source . '/' . $file);
if (!$text) {
return $text;
}
$addition = "pass:[<remark>Source file: $file</remark>]\n";
function add_filenames($text, $filename, $depths = array(2,3)) {
$addition = "pass:[<remark>Source file: $filename</remark>]\n";
// Make a regular expression to match level-N headers, which are '==' or
// '===' or whatever at the start of a line.
......@@ -84,5 +88,5 @@ function process_file($file, $source, $output, $depths = array(2,3)) {
$codes[$i] = preg_replace($match, '$0' . "\n" . $addition, $codes[$i]);
}
$text = implode("----\n", $codes);
return file_put_contents($output . '/' . $file, $text);
return $text;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment