All files / src/pipeline/rfcxml document.js

95.28% Statements 101/106
75.28% Branches 67/89
100% Functions 7/7
98.96% Lines 96/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208        2x 2x 2x 2x 2x 2x   2x 2x     5x 5x 5x 5x   5x 5x 1x 1x 5x 2x     1x 1x     5x 3x     5x 5x 4x   5x 3x   5x       4x 4x 4x 3x   4x 4x 4x 4x   4x 4x 4x 4x 4x 2x   4x 2x   4x 2x   4x         4x 4x       3x 3x 2x 2x     3x       3x 2x 2x 2x 2x                       4x 4x           4x 2x         4x 2x         4x       3x 3x                     3x 3x 3x 3x 3x   3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x       3x 3x     3x                       3x 3x   3x               3x     2x                  
/**
 * Assemble a complete RFCXML v3 Internet-Draft from Spec-Up-T markdown.
 */
 
const { el, escapeXml } = require('./xml-builder');
const { normalizeIetfConfig } = require('./ietf-config');
const { collectTermsFromMarkdown, buildTermAnchorMap, renderTerminologyDl } = require('./terminology');
const { collectCitations, renderBibliography, sanitizeBibliographyAnchor } = require('./bibliography');
const { collectFlow, nestSections, renderSectionTree } = require('./body-walker');
const { createRfcxmlParser } = require('./create-rfcxml-parser');
 
const SECURITY_STUB = 'This document specifies terminology and has no direct effect on the security of implementations or deployments.';
const IANA_STUB = 'This document has no IANA actions.';
 
function renderAuthor(author) {
    const attrs = { fullname: author.fullname };
    if (author.initials) attrs.initials = author.initials;
    Eif (author.surname) attrs.surname = author.surname;
    if (author.role) attrs.role = author.role;
 
    const addressParts = [];
    if (author.postal) {
        const postalKids = [];
        for (const key of ['street', 'city', 'region', 'code', 'country']) {
            if (author.postal[key]) {
                postalKids.push(el(key, escapeXml(String(author.postal[key]))));
            }
        }
        Eif (postalKids.length) {
            addressParts.push(el('postal', postalKids));
        }
    }
    if (author.email) {
        addressParts.push(el('email', escapeXml(author.email)));
    }
 
    const kids = [];
    if (author.organization) {
        kids.push(el('organization', escapeXml(author.organization)));
    }
    if (addressParts.length) {
        kids.push(el('address', addressParts));
    }
    return el('author', attrs, kids.length ? kids : undefined);
}
 
function renderFront(ietf, abstractXml) {
    const parts = [];
    const titleAttrs = {};
    if (ietf.abbrev) {
        titleAttrs.abbrev = ietf.abbrev;
    }
    parts.push(el('title', titleAttrs, escapeXml(ietf.title)));
    parts.push(el('seriesInfo', { name: 'Internet-Draft', value: ietf.docName }));
    for (const author of ietf.authors) {
        parts.push(renderAuthor(author));
    }
    const dateAttrs = { year: String(ietf.date.year) };
    Eif (ietf.date.month) dateAttrs.month = String(ietf.date.month);
    Eif (ietf.date.day) dateAttrs.day = String(ietf.date.day);
    parts.push(el('date', dateAttrs));
    if (ietf.area) {
        parts.push(el('area', escapeXml(ietf.area)));
    }
    if (ietf.workgroup) {
        parts.push(el('workgroup', escapeXml(ietf.workgroup)));
    }
    for (const keyword of ietf.keyword) {
        parts.push(el('keyword', escapeXml(keyword)));
    }
    const abstractInner = ietf.abstract
        ? [el('t', escapeXml(ietf.abstract))]
        : (abstractXml && abstractXml.length
            ? abstractXml
            : [el('t', 'No abstract provided.')]);
    parts.push(el('abstract', abstractInner));
    return el('front', parts);
}
 
function buildExternalSpecAnchors(spec) {
    const map = new Map();
    for (const entry of spec.external_specs || []) {
        Eif (entry && entry.external_spec) {
            map.set(entry.external_spec, sanitizeBibliographyAnchor(entry.external_spec));
        }
    }
    return map;
}
 
function collectExternalSpecReferences(spec, citations) {
    for (const entry of spec.external_specs || []) {
        Iif (!entry || !entry.external_spec) continue;
        const anchor = sanitizeBibliographyAnchor(entry.external_spec);
        Iif (citations.has(anchor)) continue;
        citations.set(anchor, {
            kind: 'other',
            group: 'informative',
            anchor,
            title: entry.title || entry.external_spec,
            target: entry.gh_page || entry.url,
            authors: [{ organization: spec.author || ' ' }]
        });
    }
}
 
function ensureRequiredSections(middleXmlParts, flags, terminologyDl) {
    const parts = [...middleXmlParts];
    Iif (!flags.terminology && terminologyDl) {
        parts.push(el('section', { anchor: 'terminology' }, [
            el('name', 'Terminology'),
            terminologyDl
        ]));
    }
    if (!flags.security) {
        parts.push(el('section', { anchor: 'security' }, [
            el('name', 'Security Considerations'),
            el('t', escapeXml(SECURITY_STUB))
        ]));
    }
    if (!flags.iana) {
        parts.push(el('section', { anchor: 'iana' }, [
            el('name', 'IANA Considerations'),
            el('t', escapeXml(IANA_STUB))
        ]));
    }
    return parts;
}
 
function createExportContext(spec, ietf, terms, citations) {
    const warnings = [];
    return {
        warnings,
        terms,
        termAnchors: buildTermAnchorMap(terms),
        citations,
        externalSpecAnchors: buildExternalSpecAnchors(spec),
        ietf
    };
}
 
function buildRfcXml(spec, markdown, options = {}) {
    const ietf = normalizeIetfConfig(spec, options.now);
    const md = options.md || createRfcxmlParser();
    const tokens = md.parse(markdown || '', {});
    const citations = collectCitations(tokens, ietf);
    collectExternalSpecReferences(spec, citations);
 
    const terms = collectTermsFromMarkdown(markdown, { xtrefs: options.xtrefs });
    const ctx = createExportContext(spec, ietf, terms, citations);
    const terminologyDl = terms.length ? renderTerminologyDl(terms, md, ctx) : '';
    ctx.terminologyDl = terminologyDl;
 
    const flow = collectFlow(tokens, ctx);
    const nested = nestSections(flow, { title: ietf.title });
    const middleRendered = renderSectionTree(nested.middle, ctx);
    const middle = ensureRequiredSections(middleRendered, nested.flags, terminologyDl);
    const backRendered = renderSectionTree(nested.back, ctx);
    const bibliography = renderBibliography(citations);
    const backParts = [...backRendered];
    Eif (bibliography) {
        backParts.unshift(bibliography);
    }
 
    const abstractXml = nested.abstractParts.length ? nested.abstractParts : null;
    const rfcInner = [
        renderFront(ietf, abstractXml),
        el('middle', middle)
    ];
    Eif (backParts.length) {
        rfcInner.push(el('back', backParts));
    }
 
    const rfcOpenAttrs = [
        'xmlns:xi="http://www.w3.org/2001/XInclude"',
        `ipr="${escapeXml(ietf.ipr)}"`,
        `docName="${escapeXml(ietf.docName)}"`,
        `category="${escapeXml(ietf.category)}"`,
        `submissionType="${escapeXml(ietf.submissionType)}"`,
        `consensus="${ietf.consensus ? 'true' : 'false'}"`,
        `tocInclude="${ietf.tocInclude ? 'true' : 'false'}"`,
        `sortRefs="${ietf.sortRefs ? 'true' : 'false'}"`,
        `symRefs="${ietf.symRefs ? 'true' : 'false'}"`,
        'version="3"'
    ];
    Iif (ietf.obsoletes) rfcOpenAttrs.push(`obsoletes="${escapeXml(ietf.obsoletes)}"`);
    Iif (ietf.updates) rfcOpenAttrs.push(`updates="${escapeXml(ietf.updates)}"`);
 
    const xml = [
        '<?xml version="1.0" encoding="UTF-8"?>',
        `<rfc ${rfcOpenAttrs.join(' ')}>`,
        rfcInner.join('\n'),
        '</rfc>',
        ''
    ].join('\n');
 
    return { xml, warnings: ctx.warnings, ietf, terms, citations };
}
 
module.exports = {
    SECURITY_STUB,
    IANA_STUB,
    renderAuthor,
    renderFront,
    buildExternalSpecAnchors,
    ensureRequiredSections,
    buildRfcXml
};