Skip to content

Commit

Permalink
Implement the @rfc@RFC syntax
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
miekg committed Nov 4, 2023
1 parent 36ee9e8 commit 09460d7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
6 changes: 6 additions & 0 deletions Syntax.md
Expand Up @@ -581,6 +581,12 @@ If you reference an RFC, I-D, BCP or STD or W3C document the reference will be a
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
citation index. I.e. a draft named "draft-gieben-pandoc2rfc", the I-D reference becomes:
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
1 change: 0 additions & 1 deletion mparser/bibliography.go
Expand Up @@ -56,7 +56,6 @@ func CitationToBibliography(doc ast.Node) (normative ast.Node, informative ast.N
}

ref := &mast.BibliographyItem{}
println("ANCHOR", string(d))
ref.Anchor = d
ref.Type = c.Type[i]

Expand Down
25 changes: 5 additions & 20 deletions render/xml/bibliography.go
Expand Up @@ -63,16 +63,16 @@ func (r *Renderer) bibliographyItem(w io.Writer, node *mast.BibliographyItem) {
tag := ""
switch {
case bytes.HasPrefix(node.Anchor, []byte("RFC")):
tag = makeXiInclude(BibRFC, fmt.Sprintf("reference.RFC.%s.xml", prefixWithZero(node.Anchor[3:])))
tag = makeXiInclude(BibRFC, fmt.Sprintf("reference.RFC.%s.xml", node.Anchor[3:]))

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", prefixWithZero(node.Anchor[3:])))
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", prefixWithZero(node.Anchor[3:])))
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("#"))
Expand Down Expand Up @@ -102,25 +102,10 @@ func makeXiInclude(url, reference string) string {
return fmt.Sprintf("<xi:include href=\"%s/%s\"/>", url, reference)
}

func prefixWithZero(num []byte) []byte {
switch len(num) {
case 0:
return num
case 1:
return append([]byte("000"), num...)
case 2:
return append([]byte("00"), num...)
case 3:
return append([]byte("00"), num...)
default:
return num
}
}

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" // reference.BCP.0014.xml
BibSTD = "https://bib.ietf.org/public/rfc/bibxml9" // reference.STD.0094.xml
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 09460d7

Please sign in to comment.