|
From: <mi...@us...> - 2021-05-20 10:08:13
|
Revision: 8735
http://sourceforge.net/p/docutils/code/8735
Author: milde
Date: 2021-05-20 10:08:11 +0000 (Thu, 20 May 2021)
Log Message:
-----------
html5 writer: support field lists with custom indent.
Review handling of "classes" attribute.
Modified Paths:
--------------
trunk/docutils/docutils/writers/_html_base.py
trunk/docutils/docutils/writers/html5_polyglot/plain.css
trunk/docutils/test/functional/expected/standalone_rst_html5.html
trunk/docutils/test/functional/input/data/html5-features.txt
trunk/docutils/test/test_writers/test_html5_polyglot_parts.py
Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py 2021-05-20 10:07:55 UTC (rev 8734)
+++ trunk/docutils/docutils/writers/_html_base.py 2021-05-20 10:08:11 UTC (rev 8735)
@@ -35,6 +35,7 @@
import docutils
from docutils import frontend, languages, nodes, utils, writers
+from docutils.parsers.rst.directives import length_or_percentage_or_unitless
from docutils.utils.error_reporting import SafeString
from docutils.transforms import writer_aux
from docutils.utils.math import (unichar2tex, pick_math_environment,
@@ -742,9 +743,8 @@
self.body.append('</dd>\n')
def visit_definition_list(self, node):
- classes = node.setdefault('classes', [])
if self.is_compactable(node):
- classes.append('simple')
+ node.setdefault('classes', []).append('simple')
self.body.append(self.starttag(node, 'dl'))
def depart_definition_list(self, node):
@@ -879,12 +879,22 @@
self.body.append('</ol>\n')
def visit_field_list(self, node):
- # Keep simple paragraphs in the field_body to enable CSS
- # rule to start body on new line if the label is too long
- classes = 'field-list'
+ atts = {}
+ classes = node.setdefault('classes', [])
+ for i, cls in enumerate(classes):
+ if cls.startswith('field-indent-'):
+ try:
+ indent_length = length_or_percentage_or_unitless(
+ cls[13:], 'px')
+ except ValueError:
+ break
+ atts['style'] = '--field-indent: %s;' % indent_length
+ classes.pop(i)
+ break
+ classes.append('field-list')
if (self.is_compactable(node)):
- classes += ' simple'
- self.body.append(self.starttag(node, 'dl', CLASS=classes))
+ classes.append('simple')
+ self.body.append(self.starttag(node, 'dl', **atts))
def depart_field_list(self, node):
self.body.append('</dl>\n')
@@ -945,7 +955,6 @@
listnode['ids'] = []
classes = 'footnote ' + self.settings.footnote_references
self.body.append(self.starttag(listnode, 'dl', CLASS=classes))
- # self.body.append('<dl class="%s">\n'%classes)
self.in_footnote_list = True
def depart_footnote(self, node):
@@ -1147,13 +1156,12 @@
classes = node.get('classes', [])
if 'code' in classes:
# filter 'code' from class arguments
- node['classes'] = [cls for cls in classes if cls != 'code']
+ classes.pop(classes.index('code'))
self.body.append(self.starttag(node, 'code', ''))
return
self.body.append(
self.starttag(node, 'span', '', CLASS='docutils literal'))
text = node.astext()
- # remove hard line breaks (except if in a parsed-literal block)
if not isinstance(node.parent, nodes.literal_block):
text = text.replace('\n', ' ')
# Protect text like ``--an-option`` and the regular expression
@@ -1165,8 +1173,7 @@
else:
self.body.append(self.encode(token))
self.body.append('</span>')
- # Content already processed:
- raise nodes.SkipNode
+ raise nodes.SkipNode # content already processed
def depart_literal(self, node):
# skipped unless literal element is from "code" role:
@@ -1563,13 +1570,14 @@
def visit_table(self, node):
atts = {}
- classes = [cls.strip(u' \t\n')
- for cls in self.settings.table_style.split(',')]
+ classes = node.setdefault('classes', [])
+ classes += [cls.strip(u' \t\n')
+ for cls in self.settings.table_style.split(',')]
if 'align' in node:
classes.append('align-%s' % node['align'])
if 'width' in node:
- atts['style'] = 'width: %s' % node['width']
- tag = self.starttag(node, 'table', CLASS=' '.join(classes), **atts)
+ atts['style'] = 'width: %s;' % node['width']
+ tag = self.starttag(node, 'table', **atts)
self.body.append(tag)
def depart_table(self, node):
Modified: trunk/docutils/docutils/writers/html5_polyglot/plain.css
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/plain.css 2021-05-20 10:07:55 UTC (rev 8734)
+++ trunk/docutils/docutils/writers/html5_polyglot/plain.css 2021-05-20 10:08:11 UTC (rev 8735)
@@ -24,6 +24,7 @@
body {
margin: 0;
background-color: #dbdbdb;
+ --field-indent: 9em; /* default indent of fields in field lists */
}
main, footer, header {
line-height:1.3;
@@ -98,9 +99,14 @@
/* Field Lists */
+dl.field-list > dd,
+dl.docinfo > dd {
+ margin-left: var(--field-indent); /* adapted in media queries or HTML */
+}
+
/* example for custom field-name width */
dl.field-list.narrow > dd {
- margin-left: 5em;
+ --field-indent: 5em;
}
/* run-in: start field-body on same line after long field names */
dl.field-list.run-in > dd p {
Modified: trunk/docutils/test/functional/expected/standalone_rst_html5.html
===================================================================
--- trunk/docutils/test/functional/expected/standalone_rst_html5.html 2021-05-20 10:07:55 UTC (rev 8734)
+++ trunk/docutils/test/functional/expected/standalone_rst_html5.html 2021-05-20 10:08:11 UTC (rev 8735)
@@ -1179,7 +1179,7 @@
<section id="list-tables">
<h3><a class="toc-backref" href="#toc-entry-41"><span class="sectnum">2.22</span> List Tables</a></h3>
<p>Here's a list table exercising all features:</p>
-<table class="colwidths-given test" style="width: 95%">
+<table class="colwidths-given test" style="width: 95%;">
<caption>list table with integral header</caption>
<colgroup>
<col style="width: 26%" />
@@ -1350,7 +1350,7 @@
<section id="field-list-variants">
<h4><a class="toc-backref" href="#toc-entry-47"><span class="sectnum">3.2.2</span> Field List Variants</a></h4>
<p>For field lists, the "compact/open", "narrow" and "run-in" styles are defined
-in the style sheet <span class="docutils literal">plain.css</span>.</p>
+in the style sheets <span class="docutils literal">plain.css</span> and <span class="docutils literal">responsive.css</span>.</p>
<dl class="simple">
<dt><em>compact</em></dt>
<dd><dl class="compact field-list simple">
@@ -1394,7 +1394,21 @@
</dd>
</dl>
</dd>
+<dt>custom <em>field-indent</em></dt>
+<dd><dl class="field-list simple" style="--field-indent: 3em;">
+<dt>Feature<span class="colon">:</span></dt>
+<dd><p>Field body indented by custom amount.</p>
+</dd>
+<dt>Use<span class="colon">:</span></dt>
+<dd><p>class value starting with <span class="docutils literal"><span class="pre">field-indent-</span></span> followed by
+a valid lenght, e.g. <span class="docutils literal"><span class="pre">field-indent-3em</span></span>.</p>
+</dd>
+<dt>The writer<span class="colon">:</span></dt>
+<dd><p>will convert this class value to a <span class="docutils literal">style</span> attribute setting.</p>
+</dd>
</dl>
+</dd>
+</dl>
<dl class="simple" id="run-in">
<dt><em>run-in</em></dt>
<dd><dl class="run-in field-list simple">
Modified: trunk/docutils/test/functional/input/data/html5-features.txt
===================================================================
--- trunk/docutils/test/functional/input/data/html5-features.txt 2021-05-20 10:07:55 UTC (rev 8734)
+++ trunk/docutils/test/functional/input/data/html5-features.txt 2021-05-20 10:08:11 UTC (rev 8735)
@@ -101,7 +101,7 @@
```````````````````
For field lists, the "compact/open", "narrow" and "run-in" styles are defined
-in the style sheet ``plain.css``.
+in the style sheets ``plain.css`` and ``responsive.css``.
*compact*
.. class:: compact
@@ -133,6 +133,15 @@
sticks into the field body and the field body starts on a
new line (if not combined with `run-in`_).
+custom *field-indent*
+ .. class:: field-indent-3em
+
+ :Feature: Field body indented by custom amount.
+ :Use: class value starting with ``field-indent-`` followed by
+ a valid lenght, e.g. ``field-indent-3em``.
+ :The writer:
+ will convert this class value to a ``style`` attribute setting.
+
.. _`run-in`:
*run-in*
Modified: trunk/docutils/test/test_writers/test_html5_polyglot_parts.py
===================================================================
--- trunk/docutils/test/test_writers/test_html5_polyglot_parts.py 2021-05-20 10:07:55 UTC (rev 8734)
+++ trunk/docutils/test/test_writers/test_html5_polyglot_parts.py 2021-05-20 10:08:11 UTC (rev 8735)
@@ -451,6 +451,71 @@
'html_head': '''...<title><string></title>\\n'''}
"""],
["""\
+Not a docinfo.
+
+.. class:: field-indent-200
+
+:This: is a
+:simple: field list with custom indent.
+""",
+"""\
+{'fragment': '''<p>Not a docinfo.</p>
+<dl class="field-list simple" style="--field-indent: 200px;">
+<dt>This<span class="colon">:</span></dt>
+<dd><p>is a</p>
+</dd>
+<dt>simple<span class="colon">:</span></dt>
+<dd><p>field list with custom indent.</p>
+</dd>
+</dl>\\n''',
+ 'html_body': '''<main>
+<p>Not a docinfo.</p>
+<dl class="field-list simple" style="--field-indent: 200px;">
+<dt>This<span class="colon">:</span></dt>
+<dd><p>is a</p>
+</dd>
+<dt>simple<span class="colon">:</span></dt>
+<dd><p>field list with custom indent.</p>
+</dd>
+</dl>
+</main>\\n''',
+ 'html_head': '''...<title><string></title>\\n'''}
+"""],
+["""\
+Not a docinfo.
+
+.. class:: field-indent-200uf
+
+:This: is a
+:simple: field list without custom indent,
+ because the unit "uf" is invalid.
+""",
+"""\
+{'fragment': '''<p>Not a docinfo.</p>
+<dl class="field-indent-200uf field-list simple">
+<dt>This<span class="colon">:</span></dt>
+<dd><p>is a</p>
+</dd>
+<dt>simple<span class="colon">:</span></dt>
+<dd><p>field list without custom indent,
+because the unit "uf" is invalid.</p>
+</dd>
+</dl>\\n''',
+ 'html_body': '''<main>
+<p>Not a docinfo.</p>
+<dl class="field-indent-200uf field-list simple">
+<dt>This<span class="colon">:</span></dt>
+<dd><p>is a</p>
+</dd>
+<dt>simple<span class="colon">:</span></dt>
+<dd><p>field list without custom indent,
+because the unit "uf" is invalid.</p>
+</dd>
+</dl>
+</main>\\n''',
+ 'html_head': '''...<title><string></title>\\n'''}
+"""],
+["""\
.. figure:: dummy.png
The figure's caption.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|