Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env php
- <?php
- array_shift( $argv );
- if( $argc < 2 )
- {
- $argv = glob( __DIR__ . "/M*.html" );
- //echo "defaulting to everything " . implode( ', ', $argv );
- }
- $index = "<html><head><title>Liturgy for ";
- $links = array();
- foreach( $argv as $file )
- {
- $f = f( $file );
- if( !$f ) continue;
- $links[] = $f;
- run( $f );
- }
- $index .= implode( ', ', $links ) . '</title></head><body>';
- foreach( $links as $d )
- {
- $index .= "<div><a href=\"Horas-{$d}.html\">Horas-{$d}.html</a></div>";
- }
- $index .= '</body></html>';
- file_put_contents( 'index.html', $index );
- function f( $arg )
- {
- if( preg_match("#([0-9]{1,2}-[0-9]{1,2})#", $arg, $matches ) )
- return $matches[ 0 ];
- return false;
- }
- function run( $targ )
- {
- $d = __DIR__ . DIRECTORY_SEPARATOR;
- $dir = glob( $d . $targ . DIRECTORY_SEPARATOR . '*' );
- @mkdir( "{$d}{$targ}_clean" );
- foreach( $dir as $fl )
- {
- process( $fl, dirname( $fl ) . '_clean' .
- DIRECTORY_SEPARATOR . basename( $fl ), $targ );
- }
- process( "${d}Master-$targ.html", "${d}Horas-$targ.html", $targ );
- }
- function process( $fl, $out, $targ )
- {
- $cont = file_get_contents( $fl );
- $dom = new DomDocument();
- @$dom->loadHTML( $cont );
- remove( $dom, "font", "td", "tr", "tbody", "table" );
- clean_references( $dom, $targ );
- $cont = @$dom->saveXML();
- file_put_contents( $out, $cont );
- }
- function remove( $dom )
- {
- $args = array_slice( func_get_args(), 1 );
- foreach( $args as $tag )
- {
- while( $dom->getElementsByTagName( $tag )->length )
- {
- foreach( $dom->getElementsByTagName( $tag ) as $node )
- {
- $parent = $node->parentNode;
- $nodes = array();
- foreach( $node->childNodes as $child )
- {
- $nodes[] = $child;
- }
- graduate( $nodes, $node, $parent );
- $parent->removeChild( $node );
- }
- }
- }
- }
- function graduate( $list, $parent, $grandparent )
- {
- foreach( $list as $node )
- $grandparent->insertBefore( $node, $parent );
- }
- function clean_references( $data, $targ )
- {
- foreach( $data->getElementsByTagName( "a" ) as $a )
- {
- $href = $a->getAttribute( "href" );
- $href = str_replace( $targ, "{$targ}_clean", $href );
- $a->setAttribute( "href", $href );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement