Pregunta

I'm trying to format the PHP code sections of a 700+ page book for Epub conversion. If I use soft returns at the end of the code lines, they get eaten. If I use hard returns (making each line a paragraph), I either get too much space between the lines, or not enough before and after the code section. If I add an empty line before and after the code section, it gets eaten.

There are thousands of lines of code in the book. Is there some way to handle this without manually editing the html file?

¿Fue útil?

Solución 2

I did try some fairly complex regex transforms, but I've found an easier method that actually works fairly well.

The secret is to create a style based on Word's "HTML Preformatted" style, or if you don't have that style, a style based on Normal that specifies Arial Unicode MS or Courier New as the font, with no proofing, left justified.

Indent with spaces and use soft returns (shift-enter) at the end of each line.

Calibre will produce acceptable Epub and Mobi versions of this. Courier is a crap font for code, but at least it's monospaced so the indents will line up, and people are used to seeing it as a code font.

Otros consejos

Is there some common format for these code section like being wrapped with PHP tags?

If they are PHP tags you can use this, which will wrap each tag set with a :

function fixPHPcode($matches)
{
  return '<p class="php_code">' . $matches[0] . '</p>';
}
$data = preg_replace_callback('/<\?php(.|\s)+?\?>/i', 'fixPHPcode', $data);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top