diff mbox

[ovs-dev,1/3] nroff: Support inline XML inside <pre> blocks.

Message ID 1442509883-3992-1-git-send-email-blp@nicira.com
State Accepted
Headers show

Commit Message

Ben Pfaff Sept. 17, 2015, 5:11 p.m. UTC
This is useful so that one can write, e.g.

<p>The following shows how to add 1 to variable <var>x</var>:</p>
<pre>
<var>x</var> = <var>x</var + 1;
</pre>

Signed-off-by: Ben Pfaff <blp@nicira.com>
---
 python/build/nroff.py | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

Comments

Justin Pettit Sept. 21, 2015, 11:04 p.m. UTC | #1
> On Sep 17, 2015, at 10:11 AM, Ben Pfaff <blp@nicira.com> wrote:
> 
> This is useful so that one can write, e.g.
> 
> <p>The following shows how to add 1 to variable <var>x</var>:</p>
> <pre>
> <var>x</var> = <var>x</var + 1;

I think you're example could use a closing ">".

> def pre_to_nroff(nodes, para, font):
> -    s = para + '\n.nf\n'
> +    # This puts 'font' at the beginning of each line so that leading and
> +    # trailing whitespace stripping later doesn't removed leading spaces
> +    # from preformatted text.
> +    s = para + '\n.nf\n' + font
>     for node in nodes:
> -        if node.nodeType == node.TEXT_NODE:
> -            for line in node.data.split('\n'):
> -                s += escape_nroff_literal(line, font) + '\n.br\n'
> -        elif node.nodeType != node.COMMENT_NODE:
> -            fatal("<pre> element may only have text children")
> -    s += '.fi\n'
> +        s += inline_xml_to_nroff(node, font, False, '\n.br\n' + font)
> +    s += '\n.fi\n'

I'm by no means an expert on this code, but does this still handle comments properly?  inline_xml_to_nroff() looks like it would complain about a comment node.  In any case, superficially, it seems different than the previous behavior.

Acked-by: Justin Pettit <jpettit@nicira.com>

--Justin
Ben Pfaff Sept. 29, 2015, 11:56 p.m. UTC | #2
On Mon, Sep 21, 2015 at 04:04:52PM -0700, Justin Pettit wrote:
> 
> > On Sep 17, 2015, at 10:11 AM, Ben Pfaff <blp@nicira.com> wrote:
> > 
> > This is useful so that one can write, e.g.
> > 
> > <p>The following shows how to add 1 to variable <var>x</var>:</p>
> > <pre>
> > <var>x</var> = <var>x</var + 1;
> 
> I think you're example could use a closing ">".

Thanks, fixed.

> > def pre_to_nroff(nodes, para, font):
> > -    s = para + '\n.nf\n'
> > +    # This puts 'font' at the beginning of each line so that leading and
> > +    # trailing whitespace stripping later doesn't removed leading spaces
> > +    # from preformatted text.
> > +    s = para + '\n.nf\n' + font
> >     for node in nodes:
> > -        if node.nodeType == node.TEXT_NODE:
> > -            for line in node.data.split('\n'):
> > -                s += escape_nroff_literal(line, font) + '\n.br\n'
> > -        elif node.nodeType != node.COMMENT_NODE:
> > -            fatal("<pre> element may only have text children")
> > -    s += '.fi\n'
> > +        s += inline_xml_to_nroff(node, font, False, '\n.br\n' + font)
> > +    s += '\n.fi\n'
> 
> I'm by no means an expert on this code, but does this still handle
> comments properly?  inline_xml_to_nroff() looks like it would complain
> about a comment node.  In any case, superficially, it seems different
> than the previous behavior.

That's true.  I changed inline_xml_to_nroff() to return '' for a comment
node.

> Acked-by: Justin Pettit <jpettit@nicira.com>

Thanks, I applied this to master.
diff mbox

Patch

diff --git a/python/build/nroff.py b/python/build/nroff.py
index 537bfd5..04fc3e8 100644
--- a/python/build/nroff.py
+++ b/python/build/nroff.py
@@ -58,17 +58,18 @@  def text_to_nroff(s, font=r'\fR'):
 def escape_nroff_literal(s, font=r'\fB'):
     return font + r'%s\fR' % text_to_nroff(s, font)
 
-def inline_xml_to_nroff(node, font, to_upper=False):
+def inline_xml_to_nroff(node, font, to_upper=False, newline='\n'):
     if node.nodeType == node.TEXT_NODE:
         if to_upper:
-            return text_to_nroff(node.data.upper(), font)
+            s = text_to_nroff(node.data.upper(), font)
         else:
-            return text_to_nroff(node.data, font)
+            s = text_to_nroff(node.data, font)
+        return s.replace('\n', newline)
     elif node.nodeType == node.ELEMENT_NODE:
         if node.tagName in ['code', 'em', 'option', 'env']:
             s = r'\fB'
             for child in node.childNodes:
-                s += inline_xml_to_nroff(child, r'\fB')
+                s += inline_xml_to_nroff(child, r'\fB', to_upper, newline)
             return s + font
         elif node.tagName == 'ref':
             s = r'\fB'
@@ -88,7 +89,7 @@  def inline_xml_to_nroff(node, font, to_upper=False):
         elif node.tagName == 'var' or node.tagName == 'dfn':
             s = r'\fI'
             for child in node.childNodes:
-                s += inline_xml_to_nroff(child, r'\fI')
+                s += inline_xml_to_nroff(child, r'\fI', to_upper, newline)
             return s + font
         else:
             raise error.Error("element <%s> unknown or invalid here" % node.tagName)
@@ -96,14 +97,13 @@  def inline_xml_to_nroff(node, font, to_upper=False):
         raise error.Error("unknown node %s in inline xml" % node)
 
 def pre_to_nroff(nodes, para, font):
-    s = para + '\n.nf\n'
+    # This puts 'font' at the beginning of each line so that leading and
+    # trailing whitespace stripping later doesn't removed leading spaces
+    # from preformatted text.
+    s = para + '\n.nf\n' + font
     for node in nodes:
-        if node.nodeType == node.TEXT_NODE:
-            for line in node.data.split('\n'):
-                s += escape_nroff_literal(line, font) + '\n.br\n'
-        elif node.nodeType != node.COMMENT_NODE:
-            fatal("<pre> element may only have text children")
-    s += '.fi\n'
+        s += inline_xml_to_nroff(node, font, False, '\n.br\n' + font)
+    s += '\n.fi\n'
     return s
 
 def diagram_header_to_nroff(header_node):