Skip to content

Commit

Permalink
Allow STD and BCP to be easily referenced (#211)
Browse files Browse the repository at this point in the history
* Allow STD and BCP to be easily referenced

Signed-off-by: Miek Gieben <miek@miek.nl>

* Make bibliography aware of RFC in std syntax

Signed-off-by: Miek Gieben <miek@miek.nl>

* Implement the @rfc@RFC syntax

Closes: #209

This implements the final bits and adds a testcase. What is not tested
is the generation of xi-includes.

Signed-off-by: Miek Gieben <miek@miek.nl>

---------

Signed-off-by: Miek Gieben <miek@miek.nl>
  • Loading branch information
miekg committed Nov 4, 2023
1 parent f11e061 commit c762292
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
14 changes: 10 additions & 4 deletions Syntax.md
Expand Up @@ -575,11 +575,17 @@ citation to the references, but does not show up in the document as a citation.
The first seen modifier determines the type (suppressed, normative or informative). Multiple
citation can separated with a semicolon: `[@RFC1034;@RFC1035]`.

If you reference an RFC, I-D or W3C document the reference will be added automatically (no need to
muck about with an `<reference>` block). This is to say:
If you reference an RFC, I-D, BCP or STD or W3C document the reference will be added automatically
(no need to muck about with an `<reference>` block). This is to say:

Any reference starting with *RFC*, *I-D.* or *W3C.* will be automatically added to the correct
reference section.
Any reference starting with *RFC*, *BCP*, *STD*, *I-D.* or *W3C.* will be automatically added to the
correct reference section.

Referring to a specific RFC of an STD/BCP can be done using the following syntax:
`[@RFCxxx@STDyy]`, this expands `<xref target="RFCxxx"></xref> of <xref target="STDxx"></xref>`
where both will be added to the automatically generated references. The word "of" will be
translated according to the document language. Note: no attempt is made to validate if the RFC is
actually part of the STD or BCP, i.e. `[@RFCxxx@RFCyyy]` will be happily accepted.

For I-Ds you may want to add a draft sequence number, which can be done as such: `[@?I-D.blah#06]`.
If you reference an I-D *without* a sequence number it will create a reference to the *last* I-D in
Expand Down
14 changes: 14 additions & 0 deletions lang/lang.go
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
)

// TODO(miek): functions below getting a bit long in the tooth.

// New returns a new and initialized Lang.
func New(language string) Lang {
l := Lang{language: strings.ToLower(language)} // case insensitivity
Expand All @@ -13,6 +15,7 @@ func New(language string) Lang {
l.m = map[string]Term{
"en": {
And: "and",
Of: "of",
Authors: "Authors",
Bibliography: "Bibliography",
Footnotes: "Footnotes",
Expand All @@ -25,6 +28,7 @@ func New(language string) Lang {
},
"nl": {
And: "en",
Of: "of",
Bibliography: "Bibliografie",
Footnotes: "Voetnoten",
Index: "Index",
Expand All @@ -35,6 +39,7 @@ func New(language string) Lang {
},
"de": {
And: "und",
Of: "von",
Bibliography: "Literaturverzeichnis",
Footnotes: "Fußnoten",
Index: "Index",
Expand Down Expand Up @@ -71,6 +76,7 @@ type Lang struct {
// Term contains the specific terms for translation.
type Term struct {
And string
Of string
Authors string
Bibliography string
Footnotes string
Expand Down Expand Up @@ -124,6 +130,14 @@ func (l Lang) And() string {
return t.And
}

func (l Lang) Of() string {
t, ok := l.m[l.language]
if !ok {
return l.m["en"].Of
}
return t.Of
}

func (l Lang) WrittenBy() string {
t, ok := l.m[l.language]
if !ok {
Expand Down
13 changes: 12 additions & 1 deletion mparser/bibliography.go
Expand Up @@ -39,11 +39,22 @@ func CitationToBibliography(doc ast.Node) (normative ast.Node, informative ast.N
// author/contact ref -> exclude
continue Destination
}

}
if _, ok := seen[string(bytes.ToLower(d))]; ok {
continue Destination
}
// if the anchor contains another '@' this is a reference to a specific RFC in a STD
// or BCP. If so we need to a 2 bib items.
if n := bytes.Index(d, []byte("@")); n > 0 && len(d[n+1:]) > 2 {
second := d[n+1:]
ref2 := &mast.BibliographyItem{}
ref2.Anchor = second
ref2.Type = c.Type[i]
seen[string(second)] = ref2

d = d[:n]
}

ref := &mast.BibliographyItem{}
ref.Anchor = d
ref.Type = c.Type[i]
Expand Down
8 changes: 8 additions & 0 deletions render/xml/bibliography.go
Expand Up @@ -68,6 +68,12 @@ func (r *Renderer) bibliographyItem(w io.Writer, node *mast.BibliographyItem) {
case bytes.HasPrefix(node.Anchor, []byte("W3C.")):
tag = makeXiInclude(BibW3C, fmt.Sprintf("reference.W3C.%s.xml", node.Anchor[4:]))

case bytes.HasPrefix(node.Anchor, []byte("BCP")):
tag = makeXiInclude(BibBCP, fmt.Sprintf("reference.BCP.%s.xml", node.Anchor[3:]))

case bytes.HasPrefix(node.Anchor, []byte("STD")):
tag = makeXiInclude(BibSTD, fmt.Sprintf("reference.STD.%s.xml", node.Anchor[3:]))

case bytes.HasPrefix(node.Anchor, []byte("I-D.")):
hash := bytes.Index(node.Anchor, []byte("#"))
draft := ""
Expand Down Expand Up @@ -100,4 +106,6 @@ var (
BibRFC = "https://bib.ietf.org/public/rfc/bibxml"
BibID = "https://bib.ietf.org/public/rfc/bibxml3"
BibW3C = "https://bib.ietf.org/public/rfc/bibxml4"
BibBCP = "https://bib.ietf.org/public/rfc/bibxml9"
BibSTD = "https://bib.ietf.org/public/rfc/bibxml9"
)
15 changes: 15 additions & 0 deletions render/xml/renderer.go
Expand Up @@ -263,6 +263,13 @@ func (r *Renderer) citation(w io.Writer, node *ast.Citation, entering bool) {
c = c[:hash]
}
}
var stdattr []string
// Detect and parse RFC@STD14, make 'c' contain the first dest and then add a second xref that we
// prepare with the (translated) word of 'of' 'RFC xxxx' <xref ...>
if n := bytes.Index(c, []byte("@")); n > 0 && len(c[n+1:]) > 2 {
stdattr = []string{fmt.Sprintf(`target="%s"`, c[n+1:])}
c = c[:n]
}

attr := []string{fmt.Sprintf(`target="%s"`, c)}

Expand Down Expand Up @@ -298,6 +305,14 @@ func (r *Renderer) citation(w io.Writer, node *ast.Citation, entering bool) {

r.outTag(w, "<xref", attr)
r.outs(w, "</xref>")

if stdattr != nil {
r.outs(w, " ")
r.outs(w, r.opts.Language.Of())
r.outs(w, " ")
r.outTag(w, "<xref", stdattr)
r.outs(w, "</xref>")
}
}
}

Expand Down
1 change: 1 addition & 0 deletions testdata/citation-std.md
@@ -0,0 +1 @@
See [@RFC8948@STD14]
2 changes: 2 additions & 0 deletions testdata/citation-std.xml
@@ -0,0 +1,2 @@
<t>See <xref target="RFC8948"></xref> of <xref target="STD14"></xref></t>

0 comments on commit c762292

Please sign in to comment.