Commits

ms2ger  committed 214954a Merge

Merged in zcorpan/anolis/xref_use_a_2 (pull request #11)

Enable usage of <a> instead of <span> for xrefs. Also fix instance_elements in xspecxref.py to match xref.py. (second try)

  • Participants
  • Parent commits 51fba0e, 7359371

Comments (0)

Files changed (6)

     parser.add_argument("--use-strict", action="store_true",
                         dest="use_strict", help=SUPPRESS)
 
+    parser.add_argument("--xref-use-a", action="store_true",
+                        dest="xref_use_a", help=SUPPRESS)
+
     parser.add_argument("--pubdate", action="store", type=str,
                         dest="publication_date", metavar="1 Sep 2022",
                         help="Set the date this document will be published.")

File anolislib/processes/xref.py

 from anolislib import utils
 
 instance_elements = frozenset(["span", "code"])
+instance_elements_a = frozenset(["a", "code"])
 w3c_instance_elements = frozenset(["abbr", "acronym", "b", "bdo", "big",
                                    "code", "del", "em", "i", "ins",
                                    "kbd", "label", "legend", "q", "samp",
     def addReferences(self, ElementTree, w3c_compat=False,
                       w3c_compat_xref_elements=False,
                       w3c_compat_xref_a_placement=False,
+                      xref_use_a=False,
                       use_strict=False,
                       dump_backrefs=False,
                       **kwargs):
         for element in ElementTree.iter(tag=etree.Element):
-            if element.tag in instance_elements or \
+            if (not xref_use_a and element.tag in instance_elements) or \
+               (xref_use_a and element.tag in instance_elements_a and element.get("href") is None) or \
                (w3c_compat or w3c_compat_xref_elements) and \
                element.tag in w3c_instance_elements:
                 term = self.getTerm(element, w3c_compat=w3c_compat, **kwargs)
                                 break
 
                     if goodParentingAndChildren and element.get("data-anolis-spec") is None:
-                        if element.tag == "span":
+                        if element.tag == "span" or element.tag == "a":
                             element.tag = "a"
                             element.set("href", "#" + self.dfns[term])
                             link = element

File anolislib/processes/xspecxref.py

 
 from anolislib import utils
 
-instance_elements = frozenset(["span", "abbr", "code", "var", "i"])
+instance_elements = frozenset(["span", "code"])
+instance_elements_a = frozenset(["a", "code"])
 w3c_instance_elements = frozenset(["abbr", "acronym", "b", "bdo", "big",
                    "code", "del", "em", "i", "ins",
                    "kbd", "label", "legend", "q", "samp",
     self.buildReferences(ElementTree, **kwargs)
     self.addReferences(ElementTree, **kwargs)
 
-  def buildReferences(self, ElementTree, xref, allow_duplicate_dfns=False, **kwargs):
+  def buildReferences(self, ElementTree, xref="data", allow_duplicate_dfns=False, **kwargs):
     manifest = open(xref + "/specs.json", "r")
     specs = json.load(manifest)
     manifest.close()
   def addReferences(self, ElementTree, w3c_compat=False,
                     w3c_compat_xref_elements=False,
                     w3c_compat_xref_a_placement=False,
+                    xref_use_a=False,
                     use_strict=False,
                     **kwargs):
     for element in ElementTree.iter(tag=etree.Element):
-      if ((element.tag in instance_elements
+      if (((not xref_use_a and element.tag in instance_elements)
+          or (xref_use_a and element.tag in instance_elements_a and element.get("href") is None)
           or (w3c_compat or w3c_compat_xref_elements)
           and element.tag in w3c_instance_elements)
           and (element.get("data-anolis-spec") is not None)):
               break
 
         if goodParentingAndChildren:
-          if element.tag == "span":
+          if element.tag == "span" or element.tag == "a":
             element.tag = "a"
             element.set("href", obj["url"] + obj["values"][term])
           else:

File tests/xref-use-a.html

+<!DOCTYPE html><meta charset=utf-8><p><dfn id=foobar>foobar</dfn></p>
+<p><dfn id=lol>lol</dfn></p>
+<p><a href=#foobar><code>foobar</code></a></p>
+<p><span>lol</span> (no link)</p>
+<p><span title=lol>x</span> (no link)</p>
+<p><span data-anolis-xref=lol>x</span> (no link)</p>
+<p><a href=#lol>lol</a></p>
+<p><a href=#>lol</a> (link to #)</p>
+<p><a href=# title=lol>lol</a> (link to #)</p>
+<p><a data-anolis-xref=lol href=#>lol</a> (link to #)</p>
+<p><a href=#lol title=lol>x</a></p>
+<p><a data-anolis-xref=lol href=#lol>x</a></p>
+<p><a data-anolis-xref=lol href=#lol title=foobar>x</a></p>
+<p><a href=http://example.org/#foo><code class=external data-anolis-spec=foobar>foo</code></a></p>
+<p><span data-anolis-spec=foobar>foo</span> (no link)</p>
+<p><span data-anolis-spec=foobar title=foo>x</span> (no link)</p>
+<p><span data-anolis-spec=foobar data-anolis-xref=foo>x</span> (no link)</p>
+<p><a class=external data-anolis-spec=foobar href=http://example.org/#foo>foo</a></p>
+<p><a data-anolis-spec=foobar href=#>foo</a> (link to #)</p>
+<p><a data-anolis-spec=foobar href=# title=foo>x</a> (link to #)</p>
+<p><a data-anolis-spec=foobar data-anolis-xref=foo href=#>x</a> (link to #)</p>
+<p><a class=external data-anolis-spec=foobar href=http://example.org/#foo title=foo>x</a></p>
+<p><a class=external data-anolis-spec=foobar data-anolis-xref=foo href=http://example.org/#foo>x</a></p>
+<p><a class=external data-anolis-spec=foobar data-anolis-xref=foo href=http://example.org/#foo title=foobar>x</a></p>

File tests/xref-use-a.options

+{
+  "processes": ["xspecxref"],
+  "xref_use_a": true,
+  "xref": "tests/xref"
+}

File tests/xref-use-a.src.html

+<!doctype html>
+<p><dfn>foobar</dfn></p>
+<p><dfn>lol</dfn></p>
+<p><code>foobar</code></p>
+<p><span>lol</span> (no link)</p>
+<p><span title=lol>x</span> (no link)</p>
+<p><span data-anolis-xref=lol>x</span> (no link)</p>
+<p><a>lol</a></p>
+<p><a href=#>lol</a> (link to #)</p>
+<p><a href=# title=lol>lol</a> (link to #)</p>
+<p><a href=# data-anolis-xref=lol>lol</a> (link to #)</p>
+<p><a title=lol>x</a></p>
+<p><a data-anolis-xref=lol>x</a></p>
+<p><a data-anolis-xref=lol title=foobar>x</a></p>
+<p><code data-anolis-spec=foobar>foo</code></p>
+<p><span data-anolis-spec=foobar>foo</span> (no link)</p>
+<p><span data-anolis-spec=foobar title=foo>x</span> (no link)</p>
+<p><span data-anolis-spec=foobar data-anolis-xref=foo>x</span> (no link)</p>
+<p><a data-anolis-spec=foobar>foo</a></p>
+<p><a href=# data-anolis-spec=foobar>foo</a> (link to #)</p>
+<p><a href=# data-anolis-spec=foobar title=foo>x</a> (link to #)</p>
+<p><a href=# data-anolis-spec=foobar data-anolis-xref=foo>x</a> (link to #)</p>
+<p><a data-anolis-spec=foobar title=foo>x</a></p>
+<p><a data-anolis-spec=foobar data-anolis-xref=foo>x</a></p>
+<p><a data-anolis-spec=foobar data-anolis-xref=foo title=foobar>x</a></p>