Skip to content

Commit

Permalink
Add ability to auto-add a Previous Version line from biblio data. Rel…
Browse files Browse the repository at this point in the history
  • Loading branch information
tabatkins committed Jul 11, 2020
1 parent 0ffac5b commit fc74cad
Show file tree
Hide file tree
Showing 17 changed files with 1,920 additions and 13 deletions.
17 changes: 16 additions & 1 deletion bikeshed/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,21 @@ def printTranslation(tr):
return E.a({"href": url, "hreflang": lang, "rel": "alternate"},
lang)

def printPreviousVersion(v):
if v['type'] == "url":
return E.a({"href":v['value'], "rel":"prev"}, v['value'])
else:
if v['type'] == "from-biblio":
key = v['value']
else: # "from-biblio-implicit"
key = doc.md.vshortname
dated = doc.refs.getLatestBiblioRef(key)
if not dated:
die(f"While trying to generate a Previous Version line, couldn't find a dated biblio reference for {key}.")
return
return E.a({"href":dated.url, "rel":"prev"}, dated.url)


md = DefaultOrderedDict(list)
mac = doc.macros
if 'version' in mac:
Expand All @@ -793,7 +808,7 @@ def printTranslation(tr):
if doc.md.ED and doc.md.status in config.snapshotStatuses:
md["Editor's Draft"].append(E.a({"href": doc.md.ED}, doc.md.ED))
if doc.md.previousVersions:
md["Previous Versions"] = [E.a({"href":ver, "rel":"prev"}, ver) for ver in doc.md.previousVersions]
md["Previous Versions"] = [printPreviousVersion(ver) for ver in doc.md.previousVersions]
if doc.md.versionHistory:
md["Version History"] = [E.a({"href":vh}, vh) for vh in doc.md.versionHistory]
if doc.md.mailingList:
Expand Down
13 changes: 12 additions & 1 deletion bikeshed/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,17 @@ def parseWptDisplay(key, val, lineNum):
return "none"


def parsePreviousVersion(key, val, lineNum):
biblioMatch = re.match(r"from biblio(\s+\S+)?", val.lower())
if biblioMatch:
if biblioMatch.group(1):
return [{"type":"from-biblio", "value":biblioMatch.group(1).strip()}]
else:
return [{"type":"from-biblio-implicit"}]
else:
return [{"type":"url", "value":val}]


def parseInlineTagCommand(key, val, lineNum):
tag,_,command = val.strip().partition(" ")
command = command.strip()
Expand Down Expand Up @@ -1073,7 +1084,7 @@ def parseLiteralList(key, val, lineNum):
"Note Class": Metadata("Note Class", "noteClass", joinValue, parseLiteral),
"Opaque Elements": Metadata("Opaque Elements", "opaqueElements", joinList, parseCommaSeparated),
"Prepare For Tr": Metadata("Prepare For Tr", "prepTR", joinValue, parseBoolean),
"Previous Version": Metadata("Previous Version", "previousVersions", joinList, parseLiteralList),
"Previous Version": Metadata("Previous Version", "previousVersions", joinList, parsePreviousVersion),
"Remove Multiple Links": Metadata("Remove Multiple Links", "removeMultipleLinks", joinValue, parseBoolean),
"Repository": Metadata("Repository", "repository", joinValue, parseRepository),
"Required Ids": Metadata("Required Ids", "requiredIDs", joinList, parseIdList),
Expand Down
58 changes: 47 additions & 11 deletions bikeshed/refs/ReferenceManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,16 +482,8 @@ def getBiblioRef(self, text, status=None, generateFakeRef=False, el=None, quiet=
return
key = text.lower()
while True:
if key not in self.biblios:
# Try to load the group up, if necessary
group = key[0:2]
if group not in self.loadedBiblioGroups:
with self.dataFile.fetch("biblio", "biblio-{0}.data".format(group), okayToFail=True) as lines:
biblio.loadBiblioDataFile(lines, self.biblios)
self.loadedBiblioGroups.add(group)
# Check if it's there
if key in self.biblios:
candidates = self.biblios[key]
candidates = self.bibliosFromKey(key)
if candidates:
break

# Did it fail because SpecRef *only* has the *un*versioned name?
Expand Down Expand Up @@ -523,7 +515,7 @@ def getBiblioRef(self, text, status=None, generateFakeRef=False, el=None, quiet=
die("A biblio link references {0}, but only {1} exists in SpecRef.", text, config.englishFromList(numericSuffixes))
return None

candidate = stripLineBreaks(sorted(candidates, key=itemgetter('order'))[0])
candidate = self._bestCandidateBiblio(candidates)
# TODO: When SpecRef definitely has all the CSS specs, turn on this code.
# if candidates[0]['order'] > 3: # 3 is SpecRef level
# warn("Bibliography term '{0}' wasn't found in SpecRef.\n Please find the equivalent key in SpecRef, or submit a PR to SpecRef.", text)
Expand All @@ -549,6 +541,50 @@ def getBiblioRef(self, text, status=None, generateFakeRef=False, el=None, quiet=

return bib


def bibliosFromKey(self, key):
# Load up the biblio data necessary to fetch the given key
# and then actually fetch it.
# If you don't call this,
# the current data might not be reliable.
if key not in self.biblios:
# Try to load the group up, if necessary
group = key[0:2]
if group not in self.loadedBiblioGroups:
with self.dataFile.fetch("biblio", "biblio-{0}.data".format(group), okayToFail=True) as lines:
biblio.loadBiblioDataFile(lines, self.biblios)
self.loadedBiblioGroups.add(group)
return self.biblios.get(key, [])


def _bestCandidateBiblio(self, candidates):
return stripLineBreaks(sorted(candidates, key=itemgetter('order'))[0])


def getLatestBiblioRef(self, key, el=None, quiet=False):
# Takes a biblio reference name,
# returns the latest dated variant of that name
# (names in the form FOO-19700101)
candidates = self.bibliosFromKey(key)
if not candidates:
return None
latestDate = None
latestRefs = None
for k,biblios in self.biblios.items():
if not k.startswith(key):
continue
match = re.search(r"(\d{8})$", k)
if not match:
continue
date = match.group(1)
if latestDate is None or date > latestDate:
latestDate = date
latestRefs = biblios
if latestRefs is None:
return None
return biblio.BiblioEntry(**self._bestCandidateBiblio(latestRefs))


def vNamesFromSpecNames(self, specName):
# Takes an unversioned specName,
# returns the versioned names that Shepherd knows about.
Expand Down
83 changes: 83 additions & 0 deletions tests/previous-versions-001.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<pre class=metadata>
Title: Foo
Group: test
Shortname: foo
Level: 1
Status: LS
ED: http://example.com/foo
Abstract: An unlevelled 'foo-bar' should *not* generate a Previous Version.
Previous Version: from biblio foo-bar
Editor: Example Editor
Date: 1970-01-01
</pre>


<pre class=biblio>
{
"foo-bar-1": {
"authors": [
"Tab Atkins",
"Dirk Schultze"
],
"href": "http://www.w3.org/TR/foo-bar-1/",
"title": "Foo Bar Level 1",
"status": "CR",
"publisher": "W3C",
"deliveredBy": [
"http://www.w3.org/html/wg/"
]
},
"foo-bar-1-20100102": {
"authors": [
"Tab Atkins",
"Dirk Schultze"
],
"href": "http://www.w3.org/TR/CR-foo-bar-1-20100102/",
"title": "Foo Bar Level 1",
"status": "CR",
"publisher": "W3C",
"deliveredBy": [
"http://www.w3.org/html/wg/"
]
},
"foo-bar-1-20200202": {
"authors": [
"Tab Atkins",
"Dirk Schultze"
],
"href": "http://www.w3.org/TR/REC-foo-bar-1-20200202/",
"title": "Foo Bar Level 1",
"status": "REC",
"publisher": "W3C",
"deliveredBy": [
"http://www.w3.org/html/wg/"
]
},
"foo-bar-2": {
"authors": [
"Tab Atkins",
"Dirk Schultze"
],
"href": "http://www.w3.org/TR/foo-bar-2/",
"title": "Foo Bar Level 2",
"status": "CR",
"publisher": "W3C",
"deliveredBy": [
"http://www.w3.org/html/wg/"
]
},
"foo-bar-2-20100102": {
"authors": [
"Tab Atkins",
"Dirk Schultze"
],
"href": "http://www.w3.org/TR/CR-foo-bar-2-20100102/",
"title": "Foo Bar Level 2",
"status": "CR",
"publisher": "W3C",
"deliveredBy": [
"http://www.w3.org/html/wg/"
]
}
}
</pre>

0 comments on commit fc74cad

Please sign in to comment.