Commits

Georg Brandl  committed f69ec6d

#114: Added an ``abbr`` role to markup abbreviations and acronyms.

  • Participants
  • Parent commits ff4b97a

Comments (0)

Files changed (9)

   - #10: Added HTML section numbers, enabled by giving a
     ``:numbered:`` flag to the ``toctree`` directive.
 
+  - #114: Added an ``abbr`` role to markup abbreviations and
+    acronyms.
+
   - The ``literalinclude`` directive now supports several more
     options, to include only parts of a file.
 

File doc/concepts.rst

 Examples for document names are ``index``, ``library/zipfile``, or
 ``reference/datamodel/types``.  Note that there is no leading slash.
 
+This is a :abbr:`LIFO (last-in, first-out)` and a :abbr:`LIFO (last-in, first-out)`.
+
 
 The TOC tree
 ------------

File doc/markup/inline.rst

 The following roles don't do anything special except formatting the text
 in a different style:
 
+.. role:: abbr
+
+   An abbreviation.  If the role content contains a parenthesized explanation,
+   it will be treated specially: it will be shown in a tool-tip in HTML, and
+   output only once in LaTeX.
+
+   Example: ``:abbr:`LIFO (last-in, first-out)```.
+
+   .. versionadded:: 0.6
+
 .. role:: command
 
    The name of an OS-level command, such as ``rm``.

File sphinx/addnodes.py

 # like emphasis, but doesn't apply further text processors, e.g. smartypants
 class literal_emphasis(nodes.emphasis): pass
 
+# for abbreviations (with explanations)
+class abbreviation(nodes.Inline, nodes.TextElement): pass
+
 # glossary
 class glossary(nodes.Element): pass
 
       desc_parameter desc_optional download_reference hlist hlistcol
       centered versionmodified seealso productionlist production toctree
       pending_xref compact_paragraph highlightlang literal_emphasis
-      glossary acks module start_of_file tabular_col_spec meta""".split())
+      abbreviation glossary acks module start_of_file tabular_col_spec
+      meta""".split())

File sphinx/roles.py

     return [retnode], []
 
 
+_abbr_re = re.compile('\((.*)\)$')
+
+def abbr_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
+    text = utils.unescape(text)
+    m = _abbr_re.search(text)
+    if m is None:
+        return [addnodes.abbreviation(text, text)], []
+    abbr = text[:m.start()].strip()
+    expl = m.group(1)
+    return [addnodes.abbreviation(abbr, abbr, explanation=expl)], []
+
+
 specific_docroles = {
     'data': xfileref_role,
     'exc': xfileref_role,
     'menuselection': menusel_role,
     'file': emph_literal_role,
     'samp': emph_literal_role,
+    'abbr': abbr_role,
 }
 
 for rolename, func in specific_docroles.iteritems():

File sphinx/writers/html.py

     def depart_literal_emphasis(self, node):
         return self.depart_emphasis(node)
 
+    def visit_abbreviation(self, node):
+        attrs = {}
+        if node.hasattr('explanation'):
+            attrs['title'] = node['explanation']
+        self.body.append(self.starttag(node, 'abbr', **attrs))
+    def depart_abbreviation(self, node):
+        self.body.append('</abbr>')
+
     def depart_title(self, node):
         close_tag = self.context[-1]
         if self.add_permalinks and self.builder.add_permalinks and \

File sphinx/writers/latex.py

         self.written_ids = set()
         self.footnotestack = []
         self.curfilestack = []
+        self.handled_abbrs = set()
         if self.elements['docclass'] == 'manual':
             if builder.config.latex_use_parts:
                 self.top_sectionlevel = 0
     def depart_strong(self, node):
         self.body.append('}')
 
+    def visit_abbreviation(self, node):
+        abbr = node.astext()
+        self.body.append(r'\textsc{')
+        # spell out the explanation once
+        if node.hasattr('explanation') and abbr not in self.handled_abbrs:
+            self.context.append('} (%s)' % self.encode(node['explanation']))
+            self.handled_abbrs.add(abbr)
+        else:
+            self.context.append('}')
+    def depart_abbreviation(self, node):
+        self.body.append(self.context.pop())
+
     def visit_title_reference(self, node):
         self.body.append(r'\emph{')
     def depart_title_reference(self, node):

File sphinx/writers/text.py

     def depart_strong(self, node):
         self.add_text('**')
 
+    def visit_abbreviation(self, node):
+        self.add_text('')
+    def depart_abbreviation(self, node):
+        if node.hasattr('explanation'):
+            self.add_text(' (%s)' % node['explanation'])
+
     def visit_title_reference(self, node):
         self.add_text('*')
     def depart_title_reference(self, node):

File tests/root/markup.txt

    try2_stmt: "try" ":" `suite`
             : "finally" ":" `suite`
 
+Test :abbr:`abbr (abbreviation)` and another :abbr:`abbr (abbreviation)`.
 
 Index markup
 ------------