Skip to content

Commit

Permalink
Refactored extract_date() into one extraction function and one augmen…
Browse files Browse the repository at this point in the history
…tation function, in order to render references with missing date info correctly. Also updated renderers to handle this case appropriately.

 - Legacy-Id: 3438
  • Loading branch information
levkowetz committed Oct 28, 2019
1 parent 7137457 commit c01225d
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 31 deletions.
8 changes: 8 additions & 0 deletions cli/tests/input/elements.xml
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ the Cherokee block look similar to the ASCII characters "STPETER".]]></artwork>
</front>
</reference>

<reference anchor="NO-DATE">
<front>
<title>Document with no date</title>
<author fullname="No Mae"/>
<date/>
</front>
</reference>

</references>

</references>
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/valid/elements.bom.text
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018
[FUZZY-DATE]
Mae, J., "Document with fuzzy date", Second quarter 2010.

[NO-DATE] Mae, N., "Document with no date".

[RFC0952] Harrenstien, K., Stahl, M., and E. Feinler, "DoD Internet
host table specification", RFC 952, DOI 10.17487/RFC0952,
October 1985, <https://www.rfc-editor.org/info/rfc952>.
Expand All @@ -1059,8 +1061,6 @@ Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018





Author, et al. Expires 13 January 2019 [Page 19]

Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/valid/elements.pages.text
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018
[FUZZY-DATE]
Mae, J., "Document with fuzzy date", Second quarter 2010.

[NO-DATE] Mae, N., "Document with no date".

[RFC0952] Harrenstien, K., Stahl, M., and E. Feinler, "DoD Internet
host table specification", RFC 952, DOI 10.17487/RFC0952,
October 1985, <https://www.rfc-editor.org/info/rfc952>.
Expand All @@ -1059,8 +1061,6 @@ Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018





Author, et al. Expires January 13, 2019 [Page 19]

Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018
Expand Down
9 changes: 8 additions & 1 deletion cli/tests/valid/elements.prepped.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" version="3" category="info" docName="elements-00" indexInclude="true" ipr="trust200902" obsoletes="1234,5678,9012,3456,7890" prepTime="2019-10-22T17:58:01" scripts="Cherokee,Common,Greek,Han,Hebrew,Latin" sortRefs="false" submissionType="independent" symRefs="true" tocDepth="3" tocInclude="true">
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" version="3" category="info" docName="elements-00" indexInclude="true" ipr="trust200902" obsoletes="1234,5678,9012,3456,7890" prepTime="2019-10-28T15:23:19" scripts="Cherokee,Common,Greek,Han,Hebrew,Latin" sortRefs="true" submissionType="independent" symRefs="true" tocDepth="3" tocInclude="true">



Expand Down Expand Up @@ -1257,6 +1257,13 @@ the Cherokee block look similar to the ASCII characters "STPETER".</artwork>
<date>Second quarter 2010</date>
</front>
</reference>
<reference anchor="NO-DATE" quoteTitle="true" derivedAnchor="NO-DATE">
<front>
<title>Document with no date</title>
<author fullname="No Mae"/>
<date/>
</front>
</reference>
<reference anchor="RFC0952" target="https://www.rfc-editor.org/info/rfc952" xml:base="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.0952.xml" quoteTitle="true" derivedAnchor="RFC0952">
<front>
<title>DoD Internet host table specification</title>
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/valid/elements.text
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ Table of Contents
[FUZZY-DATE]
Mae, J., "Document with fuzzy date", Second quarter 2010.

[NO-DATE] Mae, N., "Document with no date".

[RFC0952] Harrenstien, K., Stahl, M., and E. Feinler, "DoD Internet
host table specification", RFC 952, DOI 10.17487/RFC0952,
October 1985, <https://www.rfc-editor.org/info/rfc952>.
Expand Down
5 changes: 4 additions & 1 deletion cli/tests/valid/elements.v3.py27.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta content="田中花子 様 (Hanako Tanaka)" name="author">
<meta content="No Org" name="author">
<meta content="Bon Aventure" name="author">
<meta content="xml2rfc 2.33.0" name="generator">
<meta content="xml2rfc 2.34.0" name="generator">
<meta content="elements-00" name="ietf.draft">
<link href="tests/input/elements.xml" type="application/rfc+xml" rel="alternate">
<link href="#copyright" rel="license">
Expand Down Expand Up @@ -1179,6 +1179,9 @@ <h4 id="name-other-references">
<dt id="FUZZY-DATE">[FUZZY-DATE]</dt>
<dd>
<span class="refAuthor">Mae, J.</span>, <span class="refTitle">"Document with fuzzy date"</span>, <time datetime="Second quarter 2010">Second quarter 2010</time>. </dd>
<dt id="NO-DATE">[NO-DATE]</dt>
<dd>
<span class="refAuthor">Mae, N.</span>, <span class="refTitle">"Document with no date"</span>, <time></time>. </dd>
<dt id="RFC0952">[RFC0952]</dt>
<dd>
<span class="refAuthor">Harrenstien, K.</span><span class="refAuthor">, Stahl, M.</span><span class="refAuthor">, and E. Feinler</span>, <span class="refTitle">"DoD Internet host table specification"</span>, <span class="seriesInfo">RFC 952</span>, <span class="seriesInfo">DOI 10.17487/RFC0952</span>, <time datetime="1985-10">October 1985</time>, <span>&lt;<a href="https://www.rfc-editor.org/info/rfc952">https://www.rfc-editor.org/info/rfc952</a>&gt;</span>. </dd>
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/valid/elements.wip.text
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018
[FUZZY-DATE]
Mae, J., "Document with fuzzy date", Second quarter 2010.

[NO-DATE] Mae, N., "Document with no date".

[RFC0952] Harrenstien, K., Stahl, M., and E. Feinler, "DoD Internet
host table specification", RFC 952, DOI 10.17487/RFC0952,
October 1985, <https://www.rfc-editor.org/info/rfc952>.
Expand All @@ -1059,8 +1061,6 @@ Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018





Author, et al. Expires 13 January 2019 [Page 19]

Internet-Draft Xml2rfc Vocabulary V3 Elements July 2018
Expand Down
32 changes: 20 additions & 12 deletions cli/xml2rfc/util/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ def normalize_month(month):
def extract_date(e, today):
day = e.get('day')
month = e.get('month')
year = e.get('year', str(today.year))
year = e.get('year')
#
if not year.isdigit():
xml2rfc.log.warn("Expected a numeric year, but found '%s'" % year)
year = today.year
year = int(year)
if not month:
if year == today.year:
month = today.month
else:
if year:
if not year.isdigit():
xml2rfc.log.warn("Expected a numeric year, but found '%s'" % year)
year = today.year
year = int(year)
if month:
if not month.isdigit():
month = normalize_month(month)
month = int(month)
Expand All @@ -44,6 +42,15 @@ def extract_date(e, today):
day = int(day)
return year, month, day

def augment_date(year, month, day, today):
if not year:
year = today.year
if not month:
if year == today.year:
month = today.month
return year, month, day


def format_date_iso(year, month, day):
if year and month and day:
return '%4d-%02d-%02d' % (year, month, day)
Expand All @@ -62,15 +69,16 @@ def format_date(year, month, day, legacy=False):
date = '%s %s %s' % (day, month, year)
else:
date = '%s %s' % (month, year)
else:
elif year:
date = '%s' % year
else:
date = ''
return date


def get_expiry_date(tree, today):
year, month, day = extract_date(tree.find('./front/date'), today)
if not day:
day = today.day
year, month, day = augment_date(year, month, day, today)
exp = datetime.date(year=year, month=month, day=day) + datetime.timedelta(days=185)
return exp

7 changes: 4 additions & 3 deletions cli/xml2rfc/writers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
pass

from xml2rfc import strings, log
from xml2rfc.util.date import extract_date, format_date, get_expiry_date
from xml2rfc.util.date import extract_date, augment_date, format_date, get_expiry_date
from xml2rfc.util.name import short_author_ascii_name_parts, full_author_name_expansion, short_author_name_parts
from xml2rfc.util.unicode import punctuation, unicode_replacements, unicode_content_tags, downcode
from xml2rfc.utils import namespaces, find_duplicate_ids
Expand Down Expand Up @@ -1805,8 +1805,9 @@ def page_top_center(self):

def page_top_right(self):
date = self.root.find('./front/date')
parts = extract_date(date, self.options.date)
text = format_date(parts[0], parts[1], None, legacy=True)
year, month, day = extract_date(date, self.options.date)
year, month, day = augment_date(year, month, day, self.options.date)
text = format_date(year, month, None, legacy=True)
return text

def page_bottom_left(self):
Expand Down
12 changes: 8 additions & 4 deletions cli/xml2rfc/writers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from xml2rfc import log, strings
from xml2rfc.writers.base import default_options, BaseV3Writer
from xml2rfc.uniscripts import is_script
from xml2rfc.util.date import extract_date, format_date, format_date_iso, get_expiry_date
from xml2rfc.util.date import extract_date, augment_date, format_date, format_date_iso, get_expiry_date
from xml2rfc.util.name import ( full_author_name_expansion, short_author_role,
ref_author_name_first, ref_author_name_last,
short_author_name_set, full_author_name_set,
Expand Down Expand Up @@ -1311,9 +1311,13 @@ def render_cref(self, h, x):
# <time datetime="2014-10" class="published">October 2014</time>
def render_date(self, h, x):
have_date = x.get('day') or x.get('month') or x.get('year')
parts = extract_date(x, self.options.date)
text = format_date(*parts, legacy=self.options.legacy_date_format)
datetime = format_date_iso(*parts) if have_date else x.text
year, month, day = extract_date(x, self.options.date)
p = x.getparent()
if p==None or p.getparent().tag != 'reference':
# don't touch the given date if we're rendering a reference
year, month, day = augment_date(year, month, day, self.options.date)
text = format_date(year, month, day, legacy=self.options.legacy_date_format)
datetime = format_date_iso(year, month, day) if have_date else x.text
if x.text and have_date:
text = "%s (%s)" % (x.text, text)
elif x.text:
Expand Down
5 changes: 2 additions & 3 deletions cli/xml2rfc/writers/preptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from xml2rfc.boilerplate_tlp import boilerplate_tlp
from xml2rfc.scripts import get_scripts
from xml2rfc.uniscripts import is_script
from xml2rfc.util.date import extract_date, format_date, normalize_month
from xml2rfc.util.date import extract_date, augment_date, format_date, normalize_month
from xml2rfc.util.name import full_author_name_expansion
from xml2rfc.util.num import ol_style_formatter
from xml2rfc.util.unicode import unicode_content_tags, bare_unicode_tags, expand_unicode_element, isascii, downcode
Expand Down Expand Up @@ -1096,8 +1096,7 @@ def boilerplate_insert_status_of_memo(self, e, p):

else:
year, month, day = extract_date(self.root.find('./front/date'), self.date)
if not day:
day = self.date.day
year, month, day = augment_date(year, month, day, self.date)
exp = datetime.date(year=year, month=month, day=day) + datetime.timedelta(days=185)
format_dict['expiration_date'] = format_date(exp.year, exp.month, exp.day, legacy=self.options.legacy_date_format)
for para in boilerplate_draft_status_of_memo:
Expand Down
9 changes: 8 additions & 1 deletion cli/xml2rfc/writers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from xml2rfc.writers.base import default_options, BaseV3Writer
from xml2rfc import utils
from xml2rfc.uniscripts import is_script
from xml2rfc.util.date import extract_date, get_expiry_date, format_date
from xml2rfc.util.date import extract_date, augment_date, get_expiry_date, format_date
from xml2rfc.util.name import short_author_name, short_author_ascii_name, short_author_name_parts, short_org_name_set

from xml2rfc.util.num import ol_style_formatter, num_width
Expand Down Expand Up @@ -434,6 +434,9 @@ def tjoin(self, text, e, width, **kwargs):
if text:
if '\n' in j.join:
text += j.join + itext
elif j.join.strip() and not itext.strip():
# don't use non-empty joiners with empty content
pass
else:
text += j.join + itext.lstrip()
else:
Expand Down Expand Up @@ -1217,6 +1220,10 @@ def render_date(self, e, width, **kwargs):
#if pp.tag == 'rfc':
have_date = e.get('day') or e.get('month') or e.get('year')
year, month, day = extract_date(e, self.date)
p = e.getparent()
if p==None or p.getparent().tag != 'reference':
# don't touch the given date if we're rendering a reference
year, month, day = augment_date(year, month, day, self.date)
date = format_date(year, month, day, self.options.legacy_date_format)
if e.text and have_date:
date = "%s (%s)" % (e.text, date)
Expand Down

0 comments on commit c01225d

Please sign in to comment.