HTML.pm 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #------------------------------------------------------------------------------
  2. # File: HTML.pm
  3. #
  4. # Description: Read HTML meta information
  5. #
  6. # Revisions: 01/30/2007 - P. Harvey Created
  7. #
  8. # References: 1) http://www.w3.org/TR/html4/
  9. # 2) http://www.daisy.org/publications/specifications/daisy_202.html
  10. # 3) http://vancouver-webpages.com/META/metatags.detail.html
  11. # 4) http://www.html-reference.com/META.htm
  12. #------------------------------------------------------------------------------
  13. package Image::ExifTool::HTML;
  14. use strict;
  15. use vars qw($VERSION @ISA @EXPORT_OK);
  16. use Image::ExifTool qw(:DataAccess :Utils);
  17. use Image::ExifTool::PostScript;
  18. use Image::ExifTool::XMP qw(EscapeXML UnescapeXML);
  19. require Exporter;
  20. $VERSION = '1.15';
  21. @ISA = qw(Exporter);
  22. @EXPORT_OK = qw(EscapeHTML UnescapeHTML);
  23. sub SetHTMLCharset($$);
  24. # convert HTML charset (lower case) to ExifTool Charset name
  25. my %htmlCharset = (
  26. macintosh => 'MacRoman',
  27. 'iso-8859-1' => 'Latin',
  28. 'utf-8' => 'UTF8',
  29. 'windows-1252' => 'Latin',
  30. );
  31. # HTML 4 character entity references
  32. my %entityNum = (
  33. 'quot' => 34, 'eth' => 240, 'lsquo' => 8216,
  34. 'amp' => 38, 'ntilde' => 241, 'rsquo' => 8217,
  35. 'apos' => 39, 'ograve' => 242, 'sbquo' => 8218,
  36. 'lt' => 60, 'oacute' => 243, 'ldquo' => 8220,
  37. 'gt' => 62, 'ocirc' => 244, 'rdquo' => 8221,
  38. 'nbsp' => 160, 'otilde' => 245, 'bdquo' => 8222,
  39. 'iexcl' => 161, 'ouml' => 246, 'dagger' => 8224,
  40. 'cent' => 162, 'divide' => 247, 'Dagger' => 8225,
  41. 'pound' => 163, 'oslash' => 248, 'bull' => 8226,
  42. 'curren' => 164, 'ugrave' => 249, 'hellip' => 8230,
  43. 'yen' => 165, 'uacute' => 250, 'permil' => 8240,
  44. 'brvbar' => 166, 'ucirc' => 251, 'prime' => 8242,
  45. 'sect' => 167, 'uuml' => 252, 'Prime' => 8243,
  46. 'uml' => 168, 'yacute' => 253, 'lsaquo' => 8249,
  47. 'copy' => 169, 'thorn' => 254, 'rsaquo' => 8250,
  48. 'ordf' => 170, 'yuml' => 255, 'oline' => 8254,
  49. 'laquo' => 171, 'OElig' => 338, 'frasl' => 8260,
  50. 'not' => 172, 'oelig' => 339, 'euro' => 8364,
  51. 'shy' => 173, 'Scaron' => 352, 'image' => 8465,
  52. 'reg' => 174, 'scaron' => 353, 'weierp' => 8472,
  53. 'macr' => 175, 'Yuml' => 376, 'real' => 8476,
  54. 'deg' => 176, 'fnof' => 402, 'trade' => 8482,
  55. 'plusmn' => 177, 'circ' => 710, 'alefsym'=> 8501,
  56. 'sup2' => 178, 'tilde' => 732, 'larr' => 8592,
  57. 'sup3' => 179, 'Alpha' => 913, 'uarr' => 8593,
  58. 'acute' => 180, 'Beta' => 914, 'rarr' => 8594,
  59. 'micro' => 181, 'Gamma' => 915, 'darr' => 8595,
  60. 'para' => 182, 'Delta' => 916, 'harr' => 8596,
  61. 'middot' => 183, 'Epsilon'=> 917, 'crarr' => 8629,
  62. 'cedil' => 184, 'Zeta' => 918, 'lArr' => 8656,
  63. 'sup1' => 185, 'Eta' => 919, 'uArr' => 8657,
  64. 'ordm' => 186, 'Theta' => 920, 'rArr' => 8658,
  65. 'raquo' => 187, 'Iota' => 921, 'dArr' => 8659,
  66. 'frac14' => 188, 'Kappa' => 922, 'hArr' => 8660,
  67. 'frac12' => 189, 'Lambda' => 923, 'forall' => 8704,
  68. 'frac34' => 190, 'Mu' => 924, 'part' => 8706,
  69. 'iquest' => 191, 'Nu' => 925, 'exist' => 8707,
  70. 'Agrave' => 192, 'Xi' => 926, 'empty' => 8709,
  71. 'Aacute' => 193, 'Omicron'=> 927, 'nabla' => 8711,
  72. 'Acirc' => 194, 'Pi' => 928, 'isin' => 8712,
  73. 'Atilde' => 195, 'Rho' => 929, 'notin' => 8713,
  74. 'Auml' => 196, 'Sigma' => 931, 'ni' => 8715,
  75. 'Aring' => 197, 'Tau' => 932, 'prod' => 8719,
  76. 'AElig' => 198, 'Upsilon'=> 933, 'sum' => 8721,
  77. 'Ccedil' => 199, 'Phi' => 934, 'minus' => 8722,
  78. 'Egrave' => 200, 'Chi' => 935, 'lowast' => 8727,
  79. 'Eacute' => 201, 'Psi' => 936, 'radic' => 8730,
  80. 'Ecirc' => 202, 'Omega' => 937, 'prop' => 8733,
  81. 'Euml' => 203, 'alpha' => 945, 'infin' => 8734,
  82. 'Igrave' => 204, 'beta' => 946, 'ang' => 8736,
  83. 'Iacute' => 205, 'gamma' => 947, 'and' => 8743,
  84. 'Icirc' => 206, 'delta' => 948, 'or' => 8744,
  85. 'Iuml' => 207, 'epsilon'=> 949, 'cap' => 8745,
  86. 'ETH' => 208, 'zeta' => 950, 'cup' => 8746,
  87. 'Ntilde' => 209, 'eta' => 951, 'int' => 8747,
  88. 'Ograve' => 210, 'theta' => 952, 'there4' => 8756,
  89. 'Oacute' => 211, 'iota' => 953, 'sim' => 8764,
  90. 'Ocirc' => 212, 'kappa' => 954, 'cong' => 8773,
  91. 'Otilde' => 213, 'lambda' => 955, 'asymp' => 8776,
  92. 'Ouml' => 214, 'mu' => 956, 'ne' => 8800,
  93. 'times' => 215, 'nu' => 957, 'equiv' => 8801,
  94. 'Oslash' => 216, 'xi' => 958, 'le' => 8804,
  95. 'Ugrave' => 217, 'omicron'=> 959, 'ge' => 8805,
  96. 'Uacute' => 218, 'pi' => 960, 'sub' => 8834,
  97. 'Ucirc' => 219, 'rho' => 961, 'sup' => 8835,
  98. 'Uuml' => 220, 'sigmaf' => 962, 'nsub' => 8836,
  99. 'Yacute' => 221, 'sigma' => 963, 'sube' => 8838,
  100. 'THORN' => 222, 'tau' => 964, 'supe' => 8839,
  101. 'szlig' => 223, 'upsilon'=> 965, 'oplus' => 8853,
  102. 'agrave' => 224, 'phi' => 966, 'otimes' => 8855,
  103. 'aacute' => 225, 'chi' => 967, 'perp' => 8869,
  104. 'acirc' => 226, 'psi' => 968, 'sdot' => 8901,
  105. 'atilde' => 227, 'omega' => 969, 'lceil' => 8968,
  106. 'auml' => 228, 'thetasym'=>977, 'rceil' => 8969,
  107. 'aring' => 229, 'upsih' => 978, 'lfloor' => 8970,
  108. 'aelig' => 230, 'piv' => 982, 'rfloor' => 8971,
  109. 'ccedil' => 231, 'ensp' => 8194, 'lang' => 9001,
  110. 'egrave' => 232, 'emsp' => 8195, 'rang' => 9002,
  111. 'eacute' => 233, 'thinsp' => 8201, 'loz' => 9674,
  112. 'ecirc' => 234, 'zwnj' => 8204, 'spades' => 9824,
  113. 'euml' => 235, 'zwj' => 8205, 'clubs' => 9827,
  114. 'igrave' => 236, 'lrm' => 8206, 'hearts' => 9829,
  115. 'iacute' => 237, 'rlm' => 8207, 'diams' => 9830,
  116. 'icirc' => 238, 'ndash' => 8211,
  117. 'iuml' => 239, 'mdash' => 8212,
  118. );
  119. my %entityName; # look up entity names by number (built as necessary)
  120. # HTML info
  121. # (tag ID's are case insensitive and must be all lower case in tables)
  122. %Image::ExifTool::HTML::Main = (
  123. GROUPS => { 2 => 'Document' },
  124. NOTES => q{
  125. Meta information extracted from the header of HTML and XHTML files. This is
  126. a mix of information found in the C<META> elements, C<XML> element, and the
  127. C<TITLE> element.
  128. },
  129. dc => {
  130. Name => 'DC',
  131. SubDirectory => { TagTable => 'Image::ExifTool::HTML::dc' },
  132. },
  133. ncc => {
  134. Name => 'NCC',
  135. SubDirectory => { TagTable => 'Image::ExifTool::HTML::ncc' },
  136. },
  137. prod => {
  138. Name => 'Prod',
  139. SubDirectory => { TagTable => 'Image::ExifTool::HTML::prod' },
  140. },
  141. vw96 => {
  142. Name => 'VW96',
  143. SubDirectory => { TagTable => 'Image::ExifTool::HTML::vw96' },
  144. },
  145. 'http-equiv' => {
  146. Name => 'HTTP-equiv',
  147. SubDirectory => { TagTable => 'Image::ExifTool::HTML::equiv' },
  148. },
  149. o => {
  150. Name => 'Office',
  151. SubDirectory => { TagTable => 'Image::ExifTool::HTML::Office' },
  152. },
  153. abstract => { },
  154. author => { },
  155. classification => { },
  156. 'content-language'=>{ Name => 'ContentLanguage' },
  157. copyright => { },
  158. description => { },
  159. distribution => { },
  160. 'doc-class' => { Name => 'DocClass' },
  161. 'doc-rights' => { Name => 'DocRights' },
  162. 'doc-type' => { Name => 'DocType' },
  163. formatter => { },
  164. generator => { },
  165. generatorversion=> { Name => 'GeneratorVersion' },
  166. googlebot => { Name => 'GoogleBot' },
  167. keywords => { List => 1 },
  168. mssmarttagspreventparsing => { Name => 'NoMSSmartTags' },
  169. originator => { },
  170. owner => { },
  171. progid => { Name => 'ProgID' },
  172. rating => { },
  173. refresh => { },
  174. 'resource-type' => { Name => 'ResourceType' },
  175. 'revisit-after' => { Name => 'RevisitAfter' },
  176. robots => { List => 1 },
  177. title => { Notes => "the only extracted tag which isn't from an HTML META element" },
  178. );
  179. # ref 2
  180. %Image::ExifTool::HTML::dc = (
  181. GROUPS => { 1 => 'HTML-dc', 2 => 'Document' },
  182. NOTES => 'Dublin Core schema tags (also used in XMP).',
  183. contributor => { Groups => { 2 => 'Author' }, List => 'Bag' },
  184. coverage => { },
  185. creator => { Groups => { 2 => 'Author' }, List => 'Seq' },
  186. date => {
  187. Groups => { 2 => 'Time' },
  188. List => 'Seq',
  189. PrintConv => '$self->ConvertDateTime($val)',
  190. },
  191. description => { },
  192. 'format' => { },
  193. identifier => { },
  194. language => { List => 'Bag' },
  195. publisher => { Groups => { 2 => 'Author' }, List => 'Bag' },
  196. relation => { List => 'Bag' },
  197. rights => { Groups => { 2 => 'Author' } },
  198. source => { Groups => { 2 => 'Author' } },
  199. subject => { List => 'Bag' },
  200. title => { },
  201. type => { List => 'Bag' },
  202. );
  203. # ref 2
  204. %Image::ExifTool::HTML::ncc = (
  205. GROUPS => { 1 => 'HTML-ncc', 2 => 'Document' },
  206. charset => { Name => 'CharacterSet' }, # name changed to avoid conflict with -charset option
  207. depth => { },
  208. files => { },
  209. footnotes => { },
  210. generator => { },
  211. kbytesize => { Name => 'KByteSize' },
  212. maxpagenormal => { Name => 'MaxPageNormal' },
  213. multimediatype => { Name => 'MultimediaType' },
  214. narrator => { },
  215. pagefront => { Name => 'PageFront' },
  216. pagenormal => { Name => 'PageNormal' },
  217. pagespecial => { Name => 'PageSpecial' },
  218. prodnotes => { Name => 'ProdNotes' },
  219. producer => { },
  220. produceddate => { Name => 'ProducedDate', Groups => { 2 => 'Time' } }, # YYYY-mm-dd
  221. revision => { },
  222. revisiondate => { Name => 'RevisionDate', Groups => { 2 => 'Time' } },
  223. setinfo => { Name => 'SetInfo' },
  224. sidebars => { },
  225. sourcedate => { Name => 'SourceDate', Groups => { 2 => 'Time' } },
  226. sourceedition => { Name => 'SourceEdition' },
  227. sourcepublisher => { Name => 'SourcePublisher' },
  228. sourcerights => { Name => 'SourceRights' },
  229. sourcetitle => { Name => 'SourceTitle' },
  230. tocitems => { Name => 'TOCItems' },
  231. totaltime => { Name => 'Duration' }, # HH:MM:SS
  232. );
  233. # ref 3
  234. %Image::ExifTool::HTML::vw96 = (
  235. GROUPS => { 1 => 'HTML-vw96', 2 => 'Document' },
  236. objecttype => { Name => 'ObjectType' },
  237. );
  238. # ref 2
  239. %Image::ExifTool::HTML::prod = (
  240. GROUPS => { 1 => 'HTML-prod', 2 => 'Document' },
  241. reclocation => { Name => 'RecLocation' },
  242. recengineer => { Name => 'RecEngineer' },
  243. );
  244. # ref 3/4
  245. %Image::ExifTool::HTML::equiv = (
  246. GROUPS => { 1 => 'HTTP-equiv', 2 => 'Document' },
  247. NOTES => 'These tags have a family 1 group name of "HTTP-equiv".',
  248. 'cache-control' => { Name => 'CacheControl' },
  249. 'content-disposition' => { Name => 'ContentDisposition' },
  250. 'content-language' => { Name => 'ContentLanguage' },
  251. 'content-script-type' => { Name => 'ContentScriptType' },
  252. 'content-style-type' => { Name => 'ContentStyleType' },
  253. # note: setting the HTMLCharset like this will miss any tags which come earlier
  254. 'content-type' => { Name => 'ContentType', RawConv => \&SetHTMLCharset },
  255. 'default-style' => { Name => 'DefaultStyle' },
  256. expires => { },
  257. 'ext-cache' => { Name => 'ExtCache' },
  258. imagetoolbar => { Name => 'ImageToolbar' },
  259. lotus => { },
  260. 'page-enter' => { Name => 'PageEnter' },
  261. 'page-exit' => { Name => 'PageExit' },
  262. 'pics-label' => { Name => 'PicsLabel' },
  263. pragma => { },
  264. refresh => { },
  265. 'reply-to' => { Name => 'ReplyTo' },
  266. 'set-cookie' => { Name => 'SetCookie' },
  267. 'site-enter' => { Name => 'SiteEnter' },
  268. 'site-exit' => { Name => 'SiteExit' },
  269. vary => { },
  270. 'window-target' => { Name => 'WindowTarget' },
  271. );
  272. # MS Office namespace (ref PH)
  273. %Image::ExifTool::HTML::Office = (
  274. GROUPS => { 1 => 'HTML-office', 2 => 'Document' },
  275. NOTES => 'Tags written by Microsoft Office applications.',
  276. Subject => { },
  277. Author => { Groups => { 2 => 'Author' } },
  278. Keywords => { },
  279. Description => { },
  280. Template => { },
  281. LastAuthor => { Groups => { 2 => 'Author' } },
  282. Revision => { Name => 'RevisionNumber' },
  283. TotalTime => { Name => 'TotalEditTime', PrintConv => 'ConvertTimeSpan($val, 60)' },
  284. Created => {
  285. Name => 'CreateDate',
  286. Groups => { 2 => 'Time' },
  287. ValueConv => 'Image::ExifTool::XMP::ConvertXMPDate($val)',
  288. PrintConv => '$self->ConvertDateTime($val)',
  289. },
  290. LastSaved => {
  291. Name => 'ModifyDate',
  292. Groups => { 2 => 'Time' },
  293. ValueConv => 'Image::ExifTool::XMP::ConvertXMPDate($val)',
  294. PrintConv => '$self->ConvertDateTime($val)',
  295. },
  296. LastSaved => {
  297. Name => 'ModifyDate',
  298. Groups => { 2 => 'Time' },
  299. ValueConv => 'Image::ExifTool::XMP::ConvertXMPDate($val)',
  300. PrintConv => '$self->ConvertDateTime($val)',
  301. },
  302. LastPrinted => {
  303. Name => 'LastPrinted',
  304. Groups => { 2 => 'Time' },
  305. ValueConv => 'Image::ExifTool::XMP::ConvertXMPDate($val)',
  306. PrintConv => '$self->ConvertDateTime($val)',
  307. },
  308. Pages => { },
  309. Words => { },
  310. Characters => { },
  311. Category => { },
  312. Manager => { },
  313. Company => { },
  314. Lines => { },
  315. Paragraphs => { },
  316. CharactersWithSpaces => { },
  317. Version => { Name => 'RevisionNumber' },
  318. );
  319. #------------------------------------------------------------------------------
  320. # Set HTMLCharset member based on content type
  321. # Inputs: 0) content type string, 1) ExifTool ref
  322. # Returns: original string
  323. sub SetHTMLCharset($$)
  324. {
  325. my ($val, $et) = @_;
  326. $$et{HTMLCharset} = $htmlCharset{lc $1} if $val =~ /charset=['"]?([-\w]+)/;
  327. return $val;
  328. }
  329. #------------------------------------------------------------------------------
  330. # Convert single UTF-8 character to HTML character reference
  331. # Inputs: 0) UTF-8 character sequence
  332. # Returns: HTML character reference (eg. "&quot;");
  333. # Note: Must be called via EscapeHTML to load name lookup
  334. sub EscapeChar($)
  335. {
  336. my $ch = shift;
  337. my $val;
  338. if ($] < 5.006001) {
  339. ($val) = Image::ExifTool::UnpackUTF8($ch);
  340. } else {
  341. # the meaning of "U0" is reversed as of Perl 5.10.0!
  342. ($val) = unpack($] < 5.010000 ? 'U0U' : 'C0U', $ch);
  343. }
  344. return '?' unless defined $val;
  345. return "&$entityName{$val};" if $entityName{$val};
  346. return sprintf('&#x%x;',$val);
  347. }
  348. #------------------------------------------------------------------------------
  349. # Escape any special characters for HTML
  350. # Inputs: 0) UTF-8 string to be escaped
  351. # Returns: escaped string
  352. sub EscapeHTML($)
  353. {
  354. my $str = shift;
  355. # escape XML characters
  356. $str = EscapeXML($str);
  357. # escape other special characters if they exist
  358. if ($str =~ /[\x80-\xff]/) {
  359. # generate entity name lookup if necessary
  360. unless (%entityName) {
  361. local $_;
  362. foreach (keys %entityNum) {
  363. $entityName{$entityNum{$_}} = $_;
  364. }
  365. delete $entityName{39}; # 'apos' is not valid HTML
  366. }
  367. # suppress warnings
  368. local $SIG{'__WARN__'} = sub { 1 };
  369. # escape any non-ascii characters for HTML
  370. $str =~ s/([\xc2-\xf7][\x80-\xbf]+)/EscapeChar($1)/sge;
  371. }
  372. return $str;
  373. }
  374. #------------------------------------------------------------------------------
  375. # Unescape all HTML character references
  376. # Inputs: 0) string to be unescaped
  377. # Returns: unescaped string
  378. sub UnescapeHTML($)
  379. {
  380. return UnescapeXML(shift, \%entityNum);
  381. }
  382. #------------------------------------------------------------------------------
  383. # Extract information from a HTML file
  384. # Inputs: 0) ExifTool object reference, 1) DirInfo reference
  385. # Returns: 1 on success, 0 if this wasn't a valid HTML file
  386. sub ProcessHTML($$)
  387. {
  388. my ($et, $dirInfo) = @_;
  389. my $raf = $$dirInfo{RAF};
  390. my $buff;
  391. # validate HTML or XHTML file
  392. $raf->Read($buff, 256) or return 0;
  393. $buff =~ /^(\xef\xbb\xbf)?\s*<(!DOCTYPE\s+HTML|HTML|\?xml)/i or return 0;
  394. $buff =~ /<(!DOCTYPE\s+)?HTML/i or return 0 if $2 eq '?xml';
  395. $et->SetFileType();
  396. $raf->Seek(0,0) or $et->Warn('Seek error'), return 1;
  397. local $/ = Image::ExifTool::PostScript::GetInputRecordSeparator($raf);
  398. $/ or $et->Warn('Invalid HTML data'), return 1;
  399. # extract header information
  400. my $doc;
  401. while ($raf->ReadLine($buff)) {
  402. if (not defined $doc) {
  403. # look for 'head' element
  404. next unless $buff =~ /<head\b/ig;
  405. $doc = substr($buff, pos($buff));
  406. next;
  407. }
  408. $doc .= $buff;
  409. last if $buff =~ m{</head>}i;
  410. }
  411. return 1 unless defined $doc;
  412. # process all elements in header
  413. my $tagTablePtr = GetTagTable('Image::ExifTool::HTML::Main');
  414. for (;;) {
  415. last unless $doc =~ m{<([\w:.-]+)(.*?)>}sg;
  416. my ($tagName, $attrs) = ($1, $2);
  417. my $tag = lc($tagName);
  418. my ($val, $grp);
  419. if ($attrs =~ m{/$}) { # self-contained XHTML tags end in '/>'
  420. $val = '';
  421. } else {
  422. # look for element close
  423. my $pos = pos($doc);
  424. my $close = "</$tagName>";
  425. # the following doesn't work on Solaris Perl 5.6.1 due to Perl bug:
  426. # if ($doc =~ m{(.*?)</$tagName>}sg) {
  427. # $val = $1;
  428. if ($doc =~ m{$close}sg) {
  429. $val = substr($doc, $pos, pos($doc)-$pos-length($close));
  430. } else {
  431. pos($doc) = $pos;
  432. next unless $tag eq 'meta'; # META tags don't need to be closed
  433. $val = '';
  434. }
  435. }
  436. my $table = $tagTablePtr;
  437. if ($tag eq 'meta') {
  438. # parse HTML META element
  439. undef $tag;
  440. # tag name is in NAME or HTTP-EQUIV attribute
  441. if ($attrs =~ /\bname\s*=\s*['"]?([\w:.-]+)/si) {
  442. $tagName = $1;
  443. } elsif ($attrs =~ /\bhttp-equiv\s*=\s*['"]?([\w:.-]+)/si) {
  444. $tagName = "HTTP-equiv.$1";
  445. } else {
  446. next; # no name
  447. }
  448. $tag = lc($tagName) or next;
  449. # tag value is in CONTENT attribute
  450. if ($attrs =~ /\bcontent\s*=\s*(['"])(.*?)\1/si or
  451. $attrs =~ /\bcontent\s*=\s*(['"]?)([\w:.-]+)/si)
  452. {
  453. $val = $2;
  454. } else {
  455. next unless length $val;
  456. }
  457. # isolate group name (separator is '.' in HTML, but ':' in ref 2)
  458. if ($tag =~ /^([\w-]+)[:.]([\w-]+)/) {
  459. ($grp, $tag) = ($1, $2);
  460. my $tagInfo = $et->GetTagInfo($tagTablePtr, $grp);
  461. if ($tagInfo and $$tagInfo{SubDirectory}) {
  462. $table = GetTagTable($tagInfo->{SubDirectory}->{TagTable});
  463. } else {
  464. $tag = "$grp.$tag";
  465. }
  466. }
  467. } elsif ($tag eq 'xml') {
  468. $et->VPrint(0, "Parsing XML\n");
  469. # parse XML tags (quick-and-dirty)
  470. my $xml = $val;
  471. while ($xml =~ /<([\w-]+):([\w-]+)(\s.*?)?>([^<]*?)<\/\1:\2>/g) {
  472. ($grp, $tag, $val) = ($1, $2, $4);
  473. my $tagInfo = $et->GetTagInfo($tagTablePtr, $grp);
  474. next unless $tagInfo and $$tagInfo{SubDirectory};
  475. $table = GetTagTable($tagInfo->{SubDirectory}->{TagTable});
  476. unless ($$table{$tag}) {
  477. my $name = ucfirst $tag;
  478. $name =~ s/_x([0-9a-f]{4})_/chr(hex($1))/gie; # convert hex codes
  479. $name =~ s/\s(.)/\U$1/g; # capitalize all words in tag name
  480. $name =~ tr/-_a-zA-Z0-9//dc; # remove illegal characters (also hex code wide chars)
  481. AddTagToTable($table, $tag, { Name => $name });
  482. $et->VPrint(0, " [adding $tag '$name']\n");
  483. }
  484. $val = $et->Decode($val, $$et{HTMLCharset}) if $$et{HTMLCharset};
  485. $et->HandleTag($table, $tag, UnescapeXML($val));
  486. }
  487. next;
  488. } else {
  489. # the only other element we process is TITLE
  490. next unless $tag eq 'title';
  491. }
  492. unless ($$table{$tag}) {
  493. my $name = $tagName;
  494. $name =~ s/\W+(\w)/\u$1/sg;
  495. my $info = { Name => $name, Groups => { 0 => 'HTML' } };
  496. $info->{Groups}->{1} = ($grp eq 'http-equiv' ? 'HTTP-equiv' : "HTML-$grp") if $grp;
  497. AddTagToTable($table, $tag, $info);
  498. $et->VPrint(0, " [adding $tag '$tagName']\n");
  499. }
  500. # recode if necessary
  501. $val = $et->Decode($val, $$et{HTMLCharset}) if $$et{HTMLCharset};
  502. $val =~ s{\s*$/\s*}{ }sg; # replace linefeeds and indenting spaces
  503. $val = UnescapeHTML($val); # unescape HTML character references
  504. $et->HandleTag($table, $tag, $val);
  505. }
  506. return 1;
  507. }
  508. 1; # end
  509. __END__
  510. =head1 NAME
  511. Image::ExifTool::HTML - Read HTML meta information
  512. =head1 SYNOPSIS
  513. This module is used by Image::ExifTool
  514. =head1 DESCRIPTION
  515. This module contains routines required by Image::ExifTool to extract
  516. meta information from HTML documents.
  517. =head1 AUTHOR
  518. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  519. This library is free software; you can redistribute it and/or modify it
  520. under the same terms as Perl itself.
  521. =head1 REFERENCES
  522. =over 4
  523. =item L<http://www.w3.org/TR/html4/>
  524. =item L<http://www.daisy.org/publications/specifications/daisy_202.html>
  525. =item L<http://vancouver-webpages.com/META/metatags.detail.html>
  526. =item L<http://www.html-reference.com/META.htm>
  527. =back
  528. =head1 SEE ALSO
  529. L<Image::ExifTool::TagNames/HTML Tags>,
  530. L<Image::ExifTool(3pm)|Image::ExifTool>
  531. =cut