|
From: <mi...@us...> - 2020-01-30 00:44:53
|
Revision: 8475
http://sourceforge.net/p/docutils/code/8475
Author: milde
Date: 2020-01-30 00:44:51 +0000 (Thu, 30 Jan 2020)
Log Message:
-----------
html5: use semantic text-level tags
if matching class value found in "inline"
and "inline-literal" emements.
Modified Paths:
--------------
trunk/docutils/docutils/writers/html5_polyglot/__init__.py
trunk/docutils/test/functional/expected/standalone_rst_html5.html
Modified: trunk/docutils/docutils/writers/html5_polyglot/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/__init__.py 2020-01-30 00:44:40 UTC (rev 8474)
+++ trunk/docutils/docutils/writers/html5_polyglot/__init__.py 2020-01-30 00:44:51 UTC (rev 8475)
@@ -173,7 +173,7 @@
def depart_authors(self, node):
self.depart_docinfo_item()
-
+
# don't add 'caption' class value
def visit_caption(self, node):
self.body.append('<figcaption>\n')
@@ -270,7 +270,26 @@
self.body_prefix.extend(header)
self.header.extend(header)
del self.body[start:]
-
+
+ # use HTML text-level tags if matching class value found
+ supported_inline_tags = set(('small', 's', 'q', 'dfn', 'var', 'samp',
+ 'kbd', 'i', 'b', 'u', 'mark', 'bdi'))
+ def visit_inline(self, node):
+ # If there is exactly one of the "supported inline tags" in
+ # the list of class values, use it as tag name:
+ classes = node.get('classes', [])
+ tags = [cls for cls in classes
+ if cls in self.supported_inline_tags]
+ if len(tags) == 1:
+ node.html5tagname = tags[0]
+ classes.remove(tags[0])
+ else:
+ node.html5tagname = 'span'
+ self.body.append(self.starttag(node, node.html5tagname, ''))
+
+ def depart_inline(self, node):
+ self.body.append('</%s>' % node.html5tagname)
+
# place inside HTML5 <figcaption> element (together with caption)
def visit_legend(self, node):
if not isinstance(node.parent[1], nodes.caption):
@@ -280,7 +299,47 @@
def depart_legend(self, node):
self.body.append('</div>\n')
# <figcaption> closed in visit_figure()
-
+
+ # use HTML text-level tags if matching class value found
+ def visit_literal(self, node):
+ # special case: "code" role
+ classes = node.get('classes', [])
+ if 'code' in classes:
+ # filter 'code' from class arguments
+ node['classes'] = [cls for cls in classes if cls != 'code']
+ self.body.append(self.starttag(node, 'code', ''))
+ return
+
+ classes = node.get('classes', [])
+ tags = [cls for cls in classes
+ if cls in self.supported_inline_tags]
+ if len(tags) == 1:
+ tagname = tags[0]
+ classes.remove(tags[0])
+ else:
+ tagname = 'span'
+ self.body.append(
+ self.starttag(node, tagname, '', 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
+ # ``[+]?(\d+(\.\d*)?|\.\d+)`` from bad line wrapping
+ for token in self.words_and_spaces.findall(text):
+ if token.strip() and self.in_word_wrap_point.search(token):
+ self.body.append('<span class="pre">%s</span>'
+ % self.encode(token))
+ else:
+ self.body.append(self.encode(token))
+ self.body.append('</%s>' % tagname)
+ # Content already processed:
+ raise nodes.SkipNode
+
+ def depart_literal(self, node):
+ # skipped unless literal element is from "code" role:
+ self.body.append('</code>')
+
# Meta tags: 'lang' attribute replaced by 'xml:lang' in XHTML 1.1
# HTML5/polyglot recommends using both
def visit_meta(self, node):
Modified: trunk/docutils/test/functional/expected/standalone_rst_html5.html
===================================================================
--- trunk/docutils/test/functional/expected/standalone_rst_html5.html 2020-01-30 00:44:40 UTC (rev 8474)
+++ trunk/docutils/test/functional/expected/standalone_rst_html5.html 2020-01-30 00:44:51 UTC (rev 8475)
@@ -1362,13 +1362,13 @@
<dd><p>Side comments</p>
<blockquote>
<p>These grapes are made into wine.
-<span class="small">Alcohol is addictive.</span></p>
+<small>Alcohol is addictive.</small></p>
</blockquote>
</dd>
<dt>s</dt>
<dd><p>Inaccurate text</p>
<blockquote>
-<p>Price: <span class="s">£4.50</span> £2.00!</p>
+<p>Price: <s>£4.50</s> £2.00!</p>
</blockquote>
</dd>
<dt>cite</dt>
@@ -1381,7 +1381,7 @@
<dt>q</dt>
<dd><p>Quotations</p>
<blockquote>
-<p>The judge said <span class="q">You can drink water from the fish tank</span>
+<p>The judge said <q>You can drink water from the fish tank</q>
but advised against it.</p>
</blockquote>
</dd>
@@ -1388,7 +1388,7 @@
<dt>dfn</dt>
<dd><p>Defining instance</p>
<blockquote>
-<p>The term <span class="dfn">organic food</span> refers to food produced without synthetic
+<p>The term <dfn>organic food</dfn> refers to food produced without synthetic
chemicals.</p>
</blockquote>
</dd>
@@ -1436,7 +1436,7 @@
<dt>var</dt>
<dd><p>Variables</p>
<blockquote>
-<p>If there are <span class="var">n</span> fruit in the bowl, at least <span class="var">n</span>÷2 will be
+<p>If there are <var>n</var> fruit in the bowl, at least <var>n</var>÷2 will be
ripe.</p>
</blockquote>
</dd>
@@ -1443,13 +1443,13 @@
<dt>samp</dt>
<dd><p>Computer output</p>
<blockquote>
-<p>The computer said <span class="samp docutils literal">Unknown error <span class="pre">-3</span></span>.</p>
+<p>The computer said <samp class="docutils literal">Unknown error <span class="pre">-3</span></samp>.</p>
</blockquote>
</dd>
<dt>kbd</dt>
<dd><p>User input</p>
<blockquote>
-<p>Hit <span class="kbd">F1</span> to continue.</p>
+<p>Hit <kbd>F1</kbd> to continue.</p>
</blockquote>
</dd>
<dt>sub</dt>
@@ -1467,19 +1467,19 @@
<dt>i</dt>
<dd><p>Alternative voice</p>
<blockquote>
-<p>Lemonade consists primarily of <span class="i" lang="la">Citrus limon</span>.</p>
+<p>Lemonade consists primarily of <i lang="la">Citrus limon</i>.</p>
</blockquote>
</dd>
<dt>b</dt>
<dd><p>Keywords</p>
<blockquote>
-<p>Take a <span class="b">lemon</span> and squeeze it with a <span class="b">juicer</span>.</p>
+<p>Take a <b>lemon</b> and squeeze it with a <b>juicer</b>.</p>
</blockquote>
</dd>
<dt>u</dt>
<dd><p>Annotations</p>
<blockquote>
-<p>The mixture of apple juice and <span class="u spelling">eldeflower</span> juice is very
+<p>The mixture of apple juice and <u class="spelling">eldeflower</u> juice is very
pleasant.</p>
</blockquote>
</dd>
@@ -1486,14 +1486,14 @@
<dt>mark</dt>
<dd><p>Highlight</p>
<blockquote>
-<p>Elderflower cordial, with one <span class="mark">part</span> cordial to ten
-<span class="mark">part</span>s water, stands a<span class="mark">part</span> from the rest.</p>
+<p>Elderflower cordial, with one <mark>part</mark> cordial to ten
+<mark>part</mark>s water, stands a<mark>part</mark> from the rest.</p>
</blockquote>
</dd>
<dt>bdi</dt>
<dd><p>Text directionality isolation <a class="footnote-reference brackets" href="#attribute-optional" id="footnote-reference-14">5</a></p>
<blockquote>
-<p>The recommended restaurant is <span class="bdi">My Juice Café (At The Beach)</span>.</p>
+<p>The recommended restaurant is <bdi>My Juice Café (At The Beach)</bdi>.</p>
</blockquote>
<p>The <cite>dir</cite> global attribute defaults to "auto" (not "inherit") on this
element.</p>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|