Changeset 25052


Ignore:
Timestamp:
2012-02-07T11:08:26+13:00 (12 years ago)
Author:
jmt12
Message:

Several changes to Metadata, Table and Figure parsing as issues were encountered in languages other than English. I'd argue most of these were bogus XML, but I better import the pages anyway.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • documentation/trunk/php/gs-manual-import.php

    r25026 r25052  
    155155     addMetadata($looking_for_metadata, $text);
    156156   }
     157   // - bogus metadata found in French version
     158   elseif (!empty($looking_for_metadata) && preg_match('/<Text id="([^"]+)"\/>/', $line, $matches))
     159   {
     160   }
    157161   // - any text we encounter outside of both sections and chapters also
    158162   //   belongs on the cover
     
    232236   }
    233237   // - section, subsection and part titles within chapter
    234    elseif ($in_chapter && preg_match('/<(Section|Subsection|Part)\sid="([^"]+)">/', $line, $matches))
     238   elseif ($in_chapter && preg_match('/<(Section|Subsection|Part)\sid="([^"]*)">/', $line, $matches))
    235239   {
    236240     $title_type = $matches[1];
    237241     $section_id = $matches[2];
     242     if (empty($section_id))
     243     {
     244       $section_id = generateID(strtolower($title_type));
     245     }
     246     echo '[adding ' . strtolower($title_type) . ': ' . $section_id . '] ';
    238247     $header_fix = '';
    239248     $title = getTitle($xml_in, 'heading: ' . $title_type);
     
    352361         $row_txt = '|';
    353362         $line = getLine($xml_in);
    354          // - now we read in multiple cells
    355          while (preg_match('/<th width="(\d+)"\/?>/', $line, $matches))
     363         // - now we read in multiple cells (line starting <th
     364         while (strpos($line, '<th') === 0)
    356365         {
    357            $cell_width = $matches[1];
    358            if (!$have_output_widths)
     366           if (preg_match('/<th width="(\d+)"\/?>/', $line, $matches))
    359367           {
    360              array_push($column_widths, $cell_width);
     368             $cell_width = $matches[1];
     369             if (!$have_output_widths)
     370             {
     371               array_push($column_widths, $cell_width);
     372             }
    361373           }
    362374           // Ignore empty cells
     
    378390               }
    379391               // - we can have images or text in our tables
    380                if (preg_match('/<File width="(\d+)" height="(\d+)" url="images\/([^"]+)"\/>/', $line, $matches))
     392               if (preg_match('/<File.*url="images\/([^"]+)".*\/>/', $line, $matches))
    381393               {
    382                  $image_txt = handleImage($matches[3], $matches[1], $matches[2]);
     394                 $payload = $matches[0];
     395                 $filename = $matches[1];
     396                 $width = 0;
     397                 if (preg_match('/width="(\d+)"/', $payload, $matches))
     398                 {
     399                   $width = $matches[1];
     400                 }
     401                 $height = 0;
     402                 if (preg_match('/height="(\d+)"/', $payload, $matches))
     403                 {
     404                   $height = $matches[1];
     405                 }
     406                 $image_txt = handleImage($filename, $width, $height);
    383407                 $row_txt .= ' ' . $image_txt . ' ';
    384408               }
    385                elseif (preg_match('/<Text id="([^"]+)">(.+?)<\/Text>/', $line, $matches))
     409               elseif (preg_match('/<Text id="([^"]+)">(.*)/', $line, $matches))
    386410               {
    387                  $row_txt .= ' <!-- id:' . $matches[1] . ' -->' . translateText($matches[2]) . ' ';
     411                 $tid = $matches[1];
     412                 $txt = $matches[2];
     413                 // - multiple line text block
     414                 while (strpos($txt, '</Text>') === false)
     415                 {
     416                   $txt .= getLine($xml_in);
     417                 }
     418                 $txt = str_replace('</Text>','',$txt);
     419                 $row_txt .= ' <!-- id:' . $tid . ' -->' . translateText($txt) . ' ';
    388420               }
    389421               elseif (preg_match('/<CodeLine>(.*?)<\/CodeLine>/',$line,$matches))
     
    391423                 $row_txt .= ' \'\'' . translateTableCodeLine($matches[1]) . '\'\' ';
    392424               }
     425               elseif (preg_match('/<CodeLine>(.*)/',$line,$matches))
     426               {
     427                 $row_txt .= ' \'\'' . translateTableCodeLine($matches[1]) . '\'\' ';
     428               }
     429               elseif (preg_match('/(.*)<\/CodeLine>/',$line,$matches))
     430               {
     431                 if (!empty($matches[1]))
     432                 {
     433                   $row_txt .= ' \'\'' . translateTableCodeLine($matches[1]). '\'\' ';
     434                 }
     435                 else
     436                 {
     437                   $row_txt .= ' ';
     438                 }
     439               }
     440               // we'll add (bogus) linebreaks
     441               elseif (preg_match('/^\s*<br\s*\/?>\s*$/', $line))
     442               {
     443                 $row_txt = ' ';
     444               }
    393445               else
    394446               {
    395                  printError('Warning! Unrecognized element in table: ' . $line);
     447                 printError('Warning! Unrecognized element in table: ' . htmlspecialchars($line));
    396448               }
    397449               $first = false;
     
    774826   }
    775827   // - pattern of lines to ignore
    776    elseif (preg_match('/^(<\?xml version="1.0" encoding="UTF-8"\?>|<\!DOCTYPE Manual \[|\]>|<Bullet>|<\/?Content>|<\/?Footnote|<Manual id=".+?" lang=".+?">|<\/Manual>)/', $line))
     828   else if (preg_match('/^(<\?xml version="1.0" encoding="UTF-8"\?>|<\!DOCTYPE Manual \[|\]>|<Bullet>|<\/?Content>|<\/?Footnote|<Manual id=".+?" lang=".+?">|<\/Manual>)/', $line))
    777829   {
    778830   }
    779831   // - we ignore anything else in footnotes too, as they were handled in the
    780832   //   preprocessing pass
    781    elseif ($in_footnotes)
    782    {
     833   else if ($in_footnotes)
     834   {
     835   }
     836   // - ignore empty lines
     837   else if (preg_match('/^\s*$/', $line))
     838   {
     839   }
     840   // - meh. French versions have random, non-text element, linebreaks floating
     841   //   around. Guess I'll honor their formatting even though it's bogus
     842   else if (preg_match('/^\s*<br\s*\/?>\s*$/', $line))
     843   {
     844     if ($in_chapter)
     845     {
     846       fwrite($chapter_txt_out, ' \\\\');
     847     }
     848     else
     849     {
     850       $frontmatter_text .= ' \\\\';
     851     }
    783852   }
    784853   // - danger Will Robinson!
     
    927996{
    928997  $title = '';
     998  $in_title_element = false;
    929999  // - the first thing in a chapter will be it's title
    9301000  $title_line = getLine($xml_in);
     1001  // - super special case: some language versions don't wrap titles in title
     1002  //   element, so if the first thing we see is a text, we treat that as the
     1003  //   title
     1004  if (strpos($title_line, '<text') !== false)
     1005  {
     1006
     1007  }
    9311008  // - super special case: a table with an empty title
    9321009  if (strpos($title_line, '<Title/>') !== false)
     
    9341011    return '';
    9351012  }
    936   if (strpos($title_line, '<Title>') === false)
    937   {
    938     printError('Failed to find opening title for: ' . $element);
    939   }
    940   $title_line = getLine($xml_in);
     1013  if (strpos($title_line, '<Title>') !== false)
     1014  {
     1015    $in_title_element = true;
     1016    $title_line = getLine($xml_in);
     1017  }
     1018  // - some horribly formed entries have the subtitle first within the title
     1019  //   element
     1020  if (strpos($title_line, '<SubTitle>') !== false)
     1021  {
     1022    $title_line = getLine($xml_in);
     1023    if (preg_match('/<Text id="([^"]+)">(.+?)<\/Text>/', $title_line, $matches))
     1024    {
     1025      $title = '<!-- id:' . $matches[1] . ' -->' . $matches[2] . ' ' . $title;
     1026    }
     1027    $title_line = getLine($xml_in);
     1028    if (strpos($title_line, '</SubTitle>') === false)
     1029    {
     1030      printError('Failed to find closing title for: ' . $element);
     1031    }
     1032    $title_line = getLine($xml_in);
     1033  }
    9411034  // - grab the chapter title now so we can store it in the page ordering
    9421035  if (preg_match('/<Text id="([^"]+)">(.*?)$/', $title_line, $matches))
     
    9531046    // - now remove </Text>
    9541047    $str = preg_replace('/<\/Text>\s*/', '', $str);
    955     $title = '<!-- id:' . $id . ' -->' . $str;
     1048    $title = '<!-- id:' . $id . ' -->' . $str . $title;
    9561049  }
    9571050  // - special case for (stoopid) empty titles that use up a text id
    9581051  elseif (preg_match('/<Text id="([^"]+)"\s*\/>/', $title_line, $matches))
    9591052  {
    960     $title = '<!-- id:' . $matches[1] . ' -->';
     1053    $title = '<!-- id:' . $matches[1] . ' -->' . $title;
    9611054  }
    9621055  else
     
    9651058  }
    9661059  // - watch for subtitle elements
    967   $title_line = getLine($xml_in);
    968   if (strpos($title_line, '<SubTitle>') !== false)
     1060  if ($in_title_element)
    9691061  {
    9701062    $title_line = getLine($xml_in);
    971     if (preg_match('/<Text id="([^"]+)">(.+?)<\/Text>/', $title_line, $matches))
     1063    if (strpos($title_line, '<SubTitle>') !== false)
    9721064    {
    973       $title = '<!-- id:' . $matches[1] . ' -->' . $matches[2] . ' ' . $title;
     1065      $title_line = getLine($xml_in);
     1066      if (preg_match('/<Text id="([^"]+)">(.+?)<\/Text>/', $title_line, $matches))
     1067      {
     1068        $title = '<!-- id:' . $matches[1] . ' -->' . $matches[2] . ' ' . $title;
     1069      }
     1070      $title_line = getLine($xml_in);
     1071      if (strpos($title_line, '</SubTitle>') === false)
     1072      {
     1073        printError('Failed to find closing title for: ' . $element);
     1074      }
     1075      $title_line = getLine($xml_in);
    9741076    }
    975     $title_line = getLine($xml_in);
    976     if (strpos($title_line, '</SubTitle>') === false)
     1077    if (strpos($title_line, '</Title>') === false)
    9771078    {
    9781079      printError('Failed to find closing title for: ' . $element);
    9791080    }
    980     $title_line = getLine($xml_in);
    981   }
    982   if (strpos($title_line, '</Title>') === false)
    983   {
    984     printError('Failed to find closing title for: ' . $element);
    9851081  }
    9861082  return $title;
     
    9921088  $text = str_replace('<!--', '%!--', $text);
    9931089  $text = str_replace('-->', '--%', $text);
     1090  // remove any lurking crossrefs while we are at it
     1091  $text = preg_replace('/<CrossRef.*?ref="([^"]+)".*?>/', '\\1', $text);
    9941092  return $text;
    9951093}
     
    11491247  // - copy file into place
    11501248  $source_path = $xml_source_path . '/' . $_REQUEST['l'] . '/images/' . $filename;
    1151   $destination_path = $dokuwiki_path . '/data/media/' . $_REQUEST['l'] . '/manuals/images/' . strtolower($filename);
     1249  $destination_dir = $dokuwiki_path . '/data/media/' . $_REQUEST['l'] . '/manuals/images/';
     1250  if (!file_exists($destination_dir))
     1251  {
     1252    mkAllDir($destination_dir, 0755);
     1253  }
     1254  $destination_path = $destination_dir . strtolower($filename);
    11521255  copy($source_path, $destination_path);
    11531256  if (!file_exists($destination_path))
Note: See TracChangeset for help on using the changeset viewer.