Fixed html escape of code blocks.

This commit is contained in:
Jan Černohorský 2023-09-20 23:58:41 +02:00
parent 9fa0cb2582
commit fa2cf0a5cc

View file

@ -1,4 +1,4 @@
from panflute import Cite, Emph, Image, LineBreak, Link, Math, Note, RawInline, SmallCaps, Str, Strikeout, Subscript, Superscript, Underline from panflute import Cite, Code, Emph, Image, LineBreak, Link, Math, Note, RawInline, SmallCaps, Str, Strikeout, Subscript, Superscript, Underline
from panflute import BulletList, Citation, CodeBlock, Definition, DefinitionItem, DefinitionList, Header, HorizontalRule, LineBlock, LineItem, ListItem, Null, OrderedList, Para, Plain, RawBlock, TableBody, TableFoot, TableHead from panflute import BulletList, Citation, CodeBlock, Definition, DefinitionItem, DefinitionList, Header, HorizontalRule, LineBlock, LineItem, ListItem, Null, OrderedList, Para, Plain, RawBlock, TableBody, TableFoot, TableHead
from panflute import TableRow, TableCell, Caption, Doc from panflute import TableRow, TableCell, Caption, Doc
from panflute import ListContainer, Element from panflute import ListContainer, Element
@ -37,7 +37,7 @@ class HTMLGenerator(OutputGenerator):
def escape_special_chars(self, text: str) -> str: def escape_special_chars(self, text: str) -> str:
text = text.replace("&", "&") text = text.replace("&", "&")
text = text.replace("<", "&lt;") text = text.replace("<", "&lt;")
text = text.replace(">", "&rt;") text = text.replace(">", "&gt;")
text = text.replace("\"", "&quot;") text = text.replace("\"", "&quot;")
text = text.replace("'", "&#39;") text = text.replace("'", "&#39;")
# text = text.replace(" ", "&nbsp;") # Don't replace no-break spaces with HTML escapes, because we trust unicode? # text = text.replace(" ", "&nbsp;") # Don't replace no-break spaces with HTML escapes, because we trust unicode?
@ -125,8 +125,13 @@ class HTMLGenerator(OutputGenerator):
result = highlight(e.text, lexer, formatter) result = highlight(e.text, lexer, formatter)
self.writeraw(result) self.writeraw(result)
else: else:
e.text = self.escape_special_chars(e.text)
self.generate_simple_tag(e, tag="pre") self.generate_simple_tag(e, tag="pre")
def generate_Code(self, e: Code):
e.text = self.escape_special_chars(e.text)
self.generate_simple_tag(e)
def generate_Image(self, e: Image): def generate_Image(self, e: Image):
url = e.url url = e.url