Poem formatting

Converts HTML and wikiML poetry formatting to the standardised <poem> tag implemented on Wikimedia Foundation wikis.

Source code (hide)

Global files are not shown below.

Table of contents
  1. index.php

index.php

back to toc
<?php
/* globals, templates */
$locals = array (
    
'title'       => 'Poem formatting',
    
'description' => 'Converts HTML and wikiML poetry formatting to the standardised &lt;poem&gt; tag implemented on Wikimedia Foundation wikis.',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

/* Known issues:
# Script does not process consecutive colons (temp fix: recheck matches 11 times); this is caused by the start-of-line check, which is necessary to prevent conversion of inline (non-wikiML) colons. Obviously, an alternative method is needed.
*/

/* define variables */
$input['text'] = stripslashes($_POST['text']);

if(!empty(
$input['text'])) {
    
$output['text'] = $input['text'];
    
    
/* colons */
    // distribute colons over linebreaks
    
$output['text'] = preg_replace('/^(:+)(.*)<br ?\/?>( *[^\n\r:])/m','$1$2' "\n" '$1$3',$output['text']);
     
    
// get matches
    
preg_match_all('/^:+/m',$output['text'],$matches);
    
$matches $matches[0];
    
$count count($matches);
    
    
// iterate over matches
    
for($i=0$i<$count$i++) {
        
$match $matches[$i];
    
        
// return spacing of appropriate length
        
$length strlen($match);
        
$string '';
        for(
$x=0$x<$length$x++) {
            
$string .= '      ';
    
        
// replace match
        
$output['text'] = preg_replace('/^:+ */m',$string,$output['text'],1);
        }
    }
    
    
/* indent */
    // get matches
    
preg_match_all('/{{indent\|\d+}}/m',$output['text'],$matches);
    
$matches $matches[0];
    
$count   count($matches);
    
    
// iterate over matches
    
for($i=0$i<$count$i++) {
        
$match  $matches[$i];
    
        
// return spacing of appropriate length
        
preg_match('/\d+/',$match,$length);
        
$length $length[0];
        
$string "\n";
        echo 
$length "\n";
        for(
$x=0$x<$length$x++) {
            
$string .= '   ';
        }
    
        
// replace match
        
$output['text'] = preg_replace('/\n?{{indent\|\d+}} */',$string,$output['text'],1);
    }
    
    
/* simple replacements */
    
$regex['associative'] = array(
        
'/<br ?\/?>\r?\n?/' => "\n",
        
'/--|&mdash;/'      => '—',
        
'/&ndash;/'         => '–'
    
);
    
$regex['search']  = array_keys($regex['associative']);
    
$regex['replace'] = array_values($regex['associative']);
    
$output['text']   = preg_replace($regex['search'],$regex['replace'],$output['text']);
    
    
/* Add <poem> tags if needed */
    
if(!preg_match('/<\/?poem>/'$output['text'])) {
        
$output['text'] = '<poem>' "\n" $output['text'] . "\n" '</poem>';
    }
}

/* Output */
// debug
gDebug(get_defined_vars());

// result
if($output['text']) {
    
?>
    <h2>Filtered result</h2>
    <textarea
        rows="7"
        cols="100"
        readonly="readonly"><?php echo htmlspecialchars($output['text']); ?></textarea>
    <?php
}
?>
<h2>Input</h2>
<form action="index.php" method="post">
    <label for="text">Poem text</label>
    <p class="instruction">Paste the text you wish to convert into this box.</p>
    <textarea
        id="text"
        name="text"
        rows="12"
        cols="100"><?php echo $input['text']; ?></textarea>

    <?php gDebugOption(); ?>
    <input type="submit" value="Convert poem" id="submit" /><br />
    <input type="reset" value="Reset form" id="clear" />
</form>

<?php
/* globals, templates */
makeFooter();
?>

Input

Paste the text you wish to convert into this box.



This tool is written and copyright by Jesse Plamondon-Willard (Pathoschild). You may freely use, distribute, and modify this script in any way and for any purpose, so long as you cite the above name as original author.