| 1 | package App::optex::textconv::ooxml::xslt; | ||
| 2 | |||
| 3 | our $VERSION = '1.08'; | ||
| 4 | |||
| 5 | 1 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 1 | ▶ | use App::optex::textconv::Converter 'import'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | our @EXPORT_OK = qw(to_text get_list); | ||
| 15 | |||
| 16 | our @CONVERTER = ( | ||
| 17 | [ qr/\.doc[xm]$/ => \&to_text ], | ||
| 18 | [ qr/\.ppt[xm]$/ => \&to_text ], | ||
| 19 | # [ qr/\.xls[xm]$/ => \&to_text ], | ||
| 20 | ); | ||
| 21 | |||
| 22 | my %styles = ( | ||
| 23 | |||
| 24 | ## | ||
| 25 | ## Extract text from docx data. | ||
| 26 | ## | ||
| 27 | ## s/w:tabs/ /g; | ||
| 28 | ## for //w:p { | ||
| 29 | ## next unless .//w:t | ||
| 30 | ## for .//w:r { | ||
| 31 | ## for w:t { | ||
| 32 | ## s/w:tab/ /g; | ||
| 33 | ## s/w:br/\n/g; | ||
| 34 | ## print value; | ||
| 35 | ## } | ||
| 36 | ## } | ||
| 37 | ## print "\n\n"; | ||
| 38 | ## } | ||
| 39 | ## | ||
| 40 | docx => q{ | ||
| 41 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" | ||
| 42 | xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> | ||
| 43 | <xsl:output method="text" /> | ||
| 44 | <xsl:template match="/"> | ||
| 45 | <xsl:apply-templates select="//w:p" /> | ||
| 46 | </xsl:template> | ||
| 47 | <xsl:template match="w:p"> | ||
| 48 | <xsl:if test=".//w:t"> | ||
| 49 | <xsl:apply-templates/> | ||
| 50 | <xsl:text> </xsl:text> | ||
| 51 | </xsl:if> | ||
| 52 | </xsl:template> | ||
| 53 | <xsl:template match="w:tabs"> | ||
| 54 | <xsl:text> </xsl:text> | ||
| 55 | </xsl:template> | ||
| 56 | <xsl:template match="w:r"> | ||
| 57 | <xsl:for-each select="w:t"> | ||
| 58 | <xsl:value-of select="." /> | ||
| 59 | </xsl:for-each> | ||
| 60 | <xsl:for-each select="w:tab"> | ||
| 61 | <xsl:text> </xsl:text> | ||
| 62 | </xsl:for-each> | ||
| 63 | <xsl:for-each select="w:br"> | ||
| 64 | <xsl:text> </xsl:text> | ||
| 65 | </xsl:for-each> | ||
| 66 | </xsl:template> | ||
| 67 | </xsl:stylesheet> | ||
| 68 | }, | ||
| 69 | |||
| 70 | ## | ||
| 71 | ## Extract text from pptx data. | ||
| 72 | ## | ||
| 73 | ## for //a:p { | ||
| 74 | ## next unless .//a:t | ||
| 75 | ## for a:r { | ||
| 76 | ## for a:t { | ||
| 77 | ## print value; | ||
| 78 | ## } | ||
| 79 | ## } | ||
| 80 | ## print "\n"; | ||
| 81 | ## } | ||
| 82 | ## | ||
| 83 | pptx => q{ | ||
| 84 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" | ||
| 85 | xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" | ||
| 86 | xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" | ||
| 87 | xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"> | ||
| 88 | <xsl:output method="text" /> | ||
| 89 | <xsl:template match="/"> | ||
| 90 | <xsl:apply-templates select="//a:p" /> | ||
| 91 | </xsl:template> | ||
| 92 | <xsl:template match="a:p"> | ||
| 93 | <xsl:if test=".//a:t"> | ||
| 94 | <xsl:apply-templates/> | ||
| 95 | <xsl:text> </xsl:text> | ||
| 96 | </xsl:if> | ||
| 97 | </xsl:template> | ||
| 98 | <xsl:template match="a:r"> | ||
| 99 | <xsl:for-each select="a:t"> | ||
| 100 | <xsl:value-of select="." /> | ||
| 101 | </xsl:for-each> | ||
| 102 | </xsl:template> | ||
| 103 | </xsl:stylesheet> | ||
| 104 | }, | ||
| 105 | |||
| 106 | ## | ||
| 107 | ## This code just extract all text with no space. | ||
| 108 | ## | ||
| 109 | xlsx => q{ | ||
| 110 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" | ||
| 111 | xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> | ||
| 112 | <xsl:output method="text" /> | ||
| 113 | </xsl:stylesheet> | ||
| 114 | }, | ||
| 115 | |||
| 116 | ## | ||
| 117 | ## This an experimental code for docx to extract table/picture information. | ||
| 118 | ## | ||
| 119 | descriptive_docx => q{ | ||
| 120 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" | ||
| 121 | xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> | ||
| 122 | <xsl:output method="text" /> | ||
| 123 | <xsl:template match="/"> | ||
| 124 | <xsl:apply-templates select="//w:body | //w:endnotes | //w:footnotes" /> | ||
| 125 | </xsl:template> | ||
| 126 | <xsl:template match="w:tbl"> | ||
| 127 | <xsl:text>[ TABLE START ] </xsl:text> | ||
| 128 | <xsl:apply-templates select=".//w:p" /> | ||
| 129 | <xsl:text>[ TABLE END ] </xsl:text> | ||
| 130 | </xsl:template> | ||
| 131 | <xsl:template match="w:p"> | ||
| 132 | <xsl:if test=".//w:t"> | ||
| 133 | <xsl:apply-templates/> | ||
| 134 | <xsl:text> </xsl:text> | ||
| 135 | </xsl:if> | ||
| 136 | </xsl:template> | ||
| 137 | <xsl:template match="w:tabs"> | ||
| 138 | <xsl:text> </xsl:text> | ||
| 139 | </xsl:template> | ||
| 140 | <xsl:template match="w:r"> | ||
| 141 | <xsl:for-each select="w:t"> | ||
| 142 | <xsl:value-of select="." /> | ||
| 143 | </xsl:for-each> | ||
| 144 | <xsl:for-each select="w:tab"> | ||
| 145 | <xsl:text> </xsl:text> | ||
| 146 | </xsl:for-each> | ||
| 147 | <xsl:for-each select="w:pict"> | ||
| 148 | <xsl:text>[ PICTURE START ] </xsl:text> | ||
| 149 | <xsl:apply-templates select=".//w:p" /> | ||
| 150 | <xsl:text>[ PICTURE END ] </xsl:text> | ||
| 151 | </xsl:for-each> | ||
| 152 | </xsl:template> | ||
| 153 | </xsl:stylesheet> | ||
| 154 | }, | ||
| 155 | |||
| 156 | ## | ||
| 157 | ## This is an original code included in Text::Distill module. | ||
| 158 | ## | ||
| 159 | Text_Distill_docx => q{ | ||
| 160 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" | ||
| 161 | xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> | ||
| 162 | <xsl:output method="text" /> | ||
| 163 | <xsl:template match="/"> | ||
| 164 | <xsl:apply-templates select="//w:body" /> | ||
| 165 | </xsl:template> | ||
| 166 | <xsl:template match="w:body"> | ||
| 167 | <xsl:apply-templates /> | ||
| 168 | </xsl:template> | ||
| 169 | <xsl:template match="w:p"> | ||
| 170 | <xsl:if test="w:pPr/w:spacing/@w:after=0"><xsl:text> </xsl:text></xsl:if> | ||
| 171 | <xsl:apply-templates/><xsl:if test="position()!=last()"><xsl:text> </xsl:text></xsl:if> | ||
| 172 | </xsl:template> | ||
| 173 | <xsl:template match="w:r"> | ||
| 174 | <xsl:for-each select="w:t"> | ||
| 175 | <xsl:value-of select="." /> | ||
| 176 | </xsl:for-each> | ||
| 177 | </xsl:template> | ||
| 178 | </xsl:stylesheet> | ||
| 179 | }, | ||
| 180 | ); | ||
| 181 | |||
| 182 | for (keys %styles) { | ||
| 183 | if (/^(...)x/) { | ||
| 184 | $styles{$1."m"} //= $styles{$_}; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | 0 | ▶ | use XML::LibXML; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 189 | use XML::LibXSLT; | ||
| 190 | |||
| 191 | my %LibXMLParserOptions = ( | ||
| 192 | 'no_network' => 1, | ||
| 193 | 'expand_entities' => 0, | ||
| 194 | 'load_ext_dtd' => 0, | ||
| 195 | ); | ||
| 196 | |||
| 197 | use Archive::Zip 1.37 qw( :ERROR_CODES :CONSTANTS ); | ||
| 198 | |||
| 199 | sub xml2text { | ||
| 200 | local $_ = shift; | ||
| 201 | my $type = shift; | ||
| 202 | my $xml_re = qr/(?=<\?xml\b[^>]*\?>\s*)/; | ||
| 203 | return $_ unless /$xml_re/; | ||
| 204 | |||
| 205 | my @xml = grep { length } split /$xml_re/; | ||
| 206 | my @text = map { _xml2text($_, $type) } @xml; | ||
| 207 | join "\n", @text; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _xml2text { | ||
| 211 | my $xml_document = shift; | ||
| 212 | my $type = shift; | ||
| 213 | |||
| 214 | my $xml = XML::LibXML->new(%LibXMLParserOptions); | ||
| 215 | my $xslt = XML::LibXSLT->new(); | ||
| 216 | |||
| 217 | my $document = eval { $xml->parse_string($xml_document) }; | ||
| 218 | if ($@) { | ||
| 219 | confess "[libxml2 error ". $@->code() ."] ". $@->message(); | ||
| 220 | } | ||
| 221 | |||
| 222 | my $style_doc = $xml->load_xml(string => $styles{$type}); | ||
| 223 | my $style_sheet = $xslt->parse_stylesheet($style_doc); | ||
| 224 | my $transform = $style_sheet->transform($document); | ||
| 225 | my $result = $style_sheet->output_string($transform); | ||
| 226 | |||
| 227 | $result; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub to_text { | ||
| 231 | my $file = shift; | ||
| 232 | my $type = ($file =~ /\.((?:doc|xls|ppt)[xm])$/)[0] or return; | ||
| 233 | my $zip = Archive::Zip->new($file) or die; | ||
| 234 | my @contents; | ||
| 235 | for my $entry (get_list($zip, $type)) { | ||
| 236 | my $member = $zip->memberNamed($entry) or next; | ||
| 237 | my $xml = $member->contents or next; | ||
| 238 | my $text = xml2text $xml, $type or next; | ||
| 239 | $file = encode 'utf8', $file if utf8::is_utf8($file); | ||
| 240 | push @contents, "[ \"$file\" $entry ]\n\n$text"; | ||
| 241 | } | ||
| 242 | join "\n", @contents; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub get_list { | ||
| 246 | my($zip, $type) = @_; | ||
| 247 | if ($type =~ /^doc[xm]$/) { | ||
| 248 | map { "word/$_.xml" } qw(document endnotes footnotes); | ||
| 249 | } | ||
| 250 | elsif ($type =~ /^xls[xm]$/) { | ||
| 251 | map { "xl/$_.xml" } qw(sharedStrings); | ||
| 252 | } | ||
| 253 | elsif ($type =~ /^ppt[xm]$/) { | ||
| 254 | map { $_->[0] } | ||
| 255 | sort { $a->[1] <=> $b->[1] } | ||
| 256 | map { m{(ppt/slides/slide(\d+)\.xml)$} ? [ $1, $2 ] : () } | ||
| 257 | $zip->memberNames; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | 1; | ||