# shlomi-fish-homepage / bin / preproc-optimizing-docbook.pl

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 #!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $parser = XML::LibXML->new(); my$doc = $parser->parse_file("lib/docbook/from-mediawiki/optimizing-code-for-speed.docbook.xml"); sub empty { my$node = shift; $node->removeChildNodes(); return$node; } sub stringify { my $list = shift; return [ map { (ref($_) eq "") ? XML::LibXML::Text->new($_) :$_ } @$list ]; } sub replace { my$node = shift; my $childs = shift; empty($node); foreach my $c (@{ stringify((ref($childs) eq "ARRAY") ? $childs : [$childs]) } ) { $node->appendChild($c); } return $node; } =begin Code { my ($title) = $doc->findnodes("/book/bookinfo/title"); replace($title, "Optimizing Code for Speed"); } { my ($legal) =$doc->findnodes("/book/bookinfo/legalnotice/para"); replace($legal, "Permission to use, copy, modify and distribute this document under version 2.5 (or later) of the Creative Commons' Attribution License or version 1.2 (or later) of the GNU Free Documentation License "); } =end Code =cut my$book = $doc->findnodes("/book"); my$bookinfo = $book->findnodes("bookinfo");$book->removeChild($bookinfo); my$article = $book->findnodes("article"); replace($book, [$article->childNodes()]);$article = $book;$article->setNodeName("article"); print \$doc->toString();