Html#
Formatter for HTML output.
This module contains two formatters that can create HTML output:
HtmlFormatter
and SimpleHtmlFormatter
. The first converts
format settings from a theme to inline CSS style attributes; the latter just
puts the class names in the HTML and you should link to a CSS file yourself.
Usage example:
>>> from parce import Cursor, Document, find, theme_by_name
>>> from parce.out.html import HtmlFormatter
>>> d = Document(find("xml"), '''<xml attr="value">text</xml>\n''')
>>> f = HtmlFormatter(theme_by_name('dark'))
>>> print(f.full_html(Cursor(d, 0, None)))
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<div class="parce">
<pre style="white-space: pre; background-color: #000000; color: #fffff0; fon
t-family: monospace; font-size: 12pt;"><<span style="color: #87cefa; font
-weight: bold;">xml</span> <span style="color: #1e90ff;">attr</span>=<span s
tyle="color: #cd5c5c;">"value"</span>>text</<span style="color: #87cef
a; font-weight: bold;">xml</span>>
</pre>
</div>
</body>
</html>
- class HtmlMixin[source]#
Bases:
object
Helper class containing extra methods to generate HTML output.
- class HtmlFormatter(theme=None, factory=None)[source]#
-
A Formatter to output HTML.
- html(cursor)[source]#
Return HTML output for the selected range of the cursor.
The text pieces that have some textformat are wrapped in
<span style="">...</span>
tags with inline CSS attributes. The returned HTML expects to be wrapped in a <pre> tag.Example:
>>> from parce.out.html import HtmlFormatter >>> from parce import Cursor, Document, find, theme_by_name >>> d = Document(find('css'), "h1 { color: red; }") >>> f = HtmlFormatter(theme_by_name()) >>> f.html(Cursor(d, 0, None)) '<span style="color: #00008b; font-weight: bold;">h1</span> <span sty le="font-weight: bold;">{</span> <span style="color: #4169e1; font-we ight: bold;">color</span>: <span style="color: #2e8b57;">red</span>; <span style="font-weight: bold;">}</span>'
- class SimpleHtmlFormatter[source]#
Bases:
HtmlMixin
,SimpleFormatter
A simple Formatter that produces HTML with classes.
This HTML should then be used with a css file to see the highlighting.
- html(cursor)[source]#
Return HTML output for the selected range of the cursor.
The text pieces that have a standard action are wrapped in
<span class="xxx">..</span>
tags, where the class names correspond with the standard actions.The returned HTML expects to be wrapped in a <div> or <pre> with class
"parce"
, so that the CSS rules match with the contents.An example:
>>> from parce.out.html import SimpleHtmlFormatter >>> from parce import Cursor, Document, find, theme_by_name >>> d = Document(find('css'), "h2 { color: blue; }") >>> f = SimpleHtmlFormatter() >>> f.html(Cursor(d, 0, None)) '<span class="name tag">h2</span> <span class="delimiter bracket">{</ span> <span class="name property definition">color</span><span class= "delimiter">:</span> <span class="literal color">blue</span><span cla ss="delimiter">;</span> <span class="delimiter bracket">}</span>'
- inline_css(textformat)[source]#
Convert a
TextFormat
to an inline CSS string.The resulting string can be used in a Html
style
attribute.