EPubCheck and Funky HTML code with Code Block

There is a known bug in the software that will cause spans to be added to block level elements, when the block element is styled (as it would be in this case, to Code Block). The trigger is having the style save “All formatting” instead of just “Save Paragraph Style”, at the top of the style configuration panel. The font checkboxes are fine to leave as they were, which is the main thing you would want to change for code anyway, at the character level.

You really only need to fix this in the compile format, so if you do have some purpose for character formatting in these, that you would like to retain for other formats, you can just address it in the compile settings. You can’t change the style type, in the Styles compile format design window, so delete it, and then add a new paragraph-only style. Important: after doing that, go into the HTML Options section, and make sure the newly created style is assigned to function as a proper code block—otherwise you’ll get <p class="code-block">...</p>.

Hopefully that resolves the lack of termination on the first line.

The other thing that is a little odd about your output is that it is not merging styled lines together into one block. When I put five test lines into a document and set them to code block, I get:

<pre><code>Line one.
Line two.
Line three.
Line four.
Line five.
</code></pre>

And that is how it should be. So there may be something additional going on in the original text that is causing such a result. Empty lines that aren’t also styled maybe?

So for some reason when you do a line break within the Code block style, it adds new “pre” and “code” tags for each line.

Do you mean the Insert ▸ Break ▸ Line Break? If so, that shouldn’t necessarily cause a problem like this. It wouldn’t be desirable in this case, as Scrivener should be interpreting those to <br/>, which is the closest HTML analogue to that kind of line break, but in my experience that shouldn’t break most HTML readers. It will still make a newline, it just makes your code less pretty for no reason (as the <pre> already lets normal newlines be newlines).

1 Like