aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

mdoc.go (6800B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"io"
      6 	"strings"
      7 )
      8 
      9 type MdocRenderer struct {
     10 	page *Page
     11 	w    *trackingWriter
     12 }
     13 
     14 func NewMdocRenderer(page *Page, w io.Writer) *MdocRenderer {
     15 	return &MdocRenderer{page: page, w: newTrackingWriter(w)}
     16 }
     17 
     18 func (r *MdocRenderer) Render() error {
     19 	if err := r.page.validate(); err != nil {
     20 		return err
     21 	}
     22 
     23 	fmt.Fprintf(r.w, ".Dd %s\n", troffEscape(r.page.Date))
     24 	fmt.Fprintf(r.w, ".Dt %s %d\n", strings.ToUpper(r.page.Name), r.page.Section)
     25 	fmt.Fprintf(r.w, ".Os %s\n", "aruu")
     26 
     27 	r.renderName()
     28 	if len(r.page.Synopsis) != 0 {
     29 		r.renderSynopsis()
     30 	}
     31 	if len(r.page.Description) != 0 {
     32 		r.renderDescription()
     33 	}
     34 	if len(r.page.Options) != 0 {
     35 		r.renderOptions()
     36 	}
     37 
     38 	for _, section := range r.page.Sections {
     39 		r.renderSection(section.Title, section.Blocks)
     40 	}
     41 
     42 	return r.w.err
     43 }
     44 
     45 func (r *MdocRenderer) renderName() {
     46 	fmt.Fprintln(r.w, ".Sh NAME")
     47 	fmt.Fprintf(r.w, ".Nm %s\n", troffEscape(r.page.Name))
     48 	fmt.Fprintf(r.w, ".Nd %s\n", renderInlineSeq(parseInlines(r.page.Summary)))
     49 }
     50 
     51 func (r *MdocRenderer) renderDescription() {
     52 	fmt.Fprintln(r.w, ".Sh DESCRIPTION")
     53 	for _, block := range r.page.Description {
     54 		r.renderBlock(block)
     55 	}
     56 }
     57 
     58 func (r *MdocRenderer) renderSynopsis() {
     59 	fmt.Fprintln(r.w, ".Sh SYNOPSIS")
     60 	for _, form := range r.page.Synopsis {
     61 		// synopsis macros are emitted one phrase per line
     62 		fmt.Fprintf(r.w, ".Nm %s\n", troffEscape(r.page.Name))
     63 		for i := 0; i < len(form.Items); {
     64 			item := form.Items[i]
     65 			if item.Kind == SynOptional {
     66 				fmt.Fprintf(r.w, ".Op %s\n", renderSynopsisPhrase(item.Children, false))
     67 				i++
     68 				continue
     69 			}
     70 			if item.Kind == SynRequiredGroup {
     71 				fmt.Fprintf(r.w, ".Brq %s\n", renderSynopsisPhrase(item.Children, false))
     72 				i++
     73 				continue
     74 			}
     75 
     76 			phrase, next := consumeSynopsisPhrase(form.Items, i)
     77 			fmt.Fprintln(r.w, renderSynopsisPhrase(phrase, true))
     78 			i = next
     79 		}
     80 	}
     81 }
     82 
     83 func consumeSynopsisPhrase(items []SynopsisItem, start int) ([]SynopsisItem, int) {
     84 	phrase := []SynopsisItem{items[start]}
     85 	i := start + 1
     86 
     87 	if items[start].Kind == SynFlag && i < len(items) && items[i].Kind == SynArg {
     88 		phrase = append(phrase, items[i])
     89 		i++
     90 	}
     91 
     92 	for i+1 < len(items) && items[i].Kind == SynPipe {
     93 		phrase = append(phrase, items[i], items[i+1])
     94 		i += 2
     95 	}
     96 
     97 	return phrase, i
     98 }
     99 
    100 func renderSynopsisPhrase(items []SynopsisItem, topLevel bool) string {
    101 	var parts []string
    102 	for _, item := range items {
    103 		switch item.Kind {
    104 		case SynFlag:
    105 			parts = append(parts, renderMacro("Fl", troffEscape(strings.TrimPrefix(item.Text, "-")), topLevel && len(parts) == 0))
    106 		case SynArg:
    107 			parts = append(parts, renderMacro("Ar", troffEscape(strings.Trim(item.Text, "<>")), topLevel && len(parts) == 0))
    108 		case SynCommand:
    109 			parts = append(parts, renderMacro("Cm", troffEscape(item.Text), topLevel && len(parts) == 0))
    110 		case SynPipe:
    111 			parts = append(parts, "|")
    112 		case SynLiteral:
    113 			parts = append(parts, troffEscape(item.Text))
    114 		case SynOptional:
    115 			parts = append(parts, renderMacro("Op", renderSynopsisPhrase(item.Children, false), topLevel && len(parts) == 0))
    116 		case SynRequiredGroup:
    117 			parts = append(parts, renderMacro("Brq", renderSynopsisPhrase(item.Children, false), topLevel && len(parts) == 0))
    118 		}
    119 	}
    120 	return strings.Join(parts, " ")
    121 }
    122 
    123 func renderMacro(name, body string, topLevel bool) string {
    124 	if body == "" {
    125 		if topLevel {
    126 			return "." + name
    127 		}
    128 
    129 		return name
    130 	}
    131 	if topLevel {
    132 		return "." + name + " " + body
    133 	}
    134 
    135 	return name + " " + body
    136 }
    137 
    138 func (r *MdocRenderer) renderOptions() {
    139 	fmt.Fprintln(r.w, ".Sh OPTIONS")
    140 	fmt.Fprintln(r.w, ".Bl -tag -width Ds")
    141 	for _, opt := range r.page.Options {
    142 		fmt.Fprintf(r.w, ".It %s\n", renderSynopsisPhrase(opt.Spec, false))
    143 		for _, block := range opt.Body {
    144 			r.renderBlock(block)
    145 		}
    146 	}
    147 	fmt.Fprintln(r.w, ".El")
    148 }
    149 
    150 func (r *MdocRenderer) renderSection(title string, blocks []Block) {
    151 	fmt.Fprintf(r.w, ".Sh %s\n", troffEscape(title))
    152 	for _, block := range blocks {
    153 		r.renderBlock(block)
    154 	}
    155 }
    156 
    157 func (r *MdocRenderer) renderBlock(block Block) {
    158 	switch block.Kind {
    159 	case BlockParagraph:
    160 		fmt.Fprintln(r.w, ".Pp")
    161 		fmt.Fprintf(r.w, ".No %s\n", renderInlineSeq(block.Inlines))
    162 	case BlockTaggedList:
    163 		fmt.Fprintln(r.w, ".Bl -tag -width Ds")
    164 		for _, item := range block.Items {
    165 			fmt.Fprintf(r.w, ".It %s\n", renderInlineSeq(item.Label))
    166 			if len(item.Body) != 0 {
    167 				fmt.Fprintln(r.w, ".Pp")
    168 				fmt.Fprintf(r.w, ".No %s\n", renderInlineSeq(item.Body))
    169 			}
    170 		}
    171 		fmt.Fprintln(r.w, ".El")
    172 	case BlockSeeAlso:
    173 		for i, ref := range block.Refs {
    174 			line := fmt.Sprintf(".Xr %s %s", troffEscape(ref.Name), troffEscape(ref.Section))
    175 			if i != len(block.Refs)-1 {
    176 				line += " ,"
    177 			}
    178 			fmt.Fprintln(r.w, line)
    179 		}
    180 	case BlockSubsection:
    181 		fmt.Fprintf(r.w, ".Ss %s\n", troffEscape(block.Title))
    182 		for _, child := range block.Blocks {
    183 			r.renderBlock(child)
    184 		}
    185 	}
    186 }
    187 
    188 func troffEscape(s string) string {
    189 	s = strings.ReplaceAll(s, "\\", "\\\\")
    190 	s = strings.ReplaceAll(s, "-", "\\-")
    191 	if len(s) > 0 && s[0] == '.' {
    192 		s = "\\&" + s
    193 	}
    194 	return s
    195 }
    196 
    197 func renderInlineSeq(inlines []Inline) string {
    198 	var b strings.Builder
    199 	// adjacent semantic nodes sometimes need to join without an intervening
    200 	// space, like some mixed path/emphasis/path forms in FILES
    201 	for i, inline := range inlines {
    202 		if i > 0 && needsNoSpace(inlines[i-1], inline) {
    203 			b.WriteString(" Ns ")
    204 		}
    205 		b.WriteString(renderInline(inline))
    206 	}
    207 	return b.String()
    208 }
    209 
    210 func renderInline(inline Inline) string {
    211 	switch inline.Kind {
    212 	case InlineText:
    213 		return troffEscape(inline.Text)
    214 	case InlineNm:
    215 		return renderMacro("Nm", "", false)
    216 	case InlineEmph:
    217 		return renderMacro("Em", renderInlineSeq(inline.Children), false)
    218 	case InlineLiteral:
    219 		return renderMacro("Ql", troffEscape(inline.Text), false)
    220 	case InlinePath:
    221 		return renderMacro("Pa", troffEscape(inline.Text), false)
    222 	case InlineXRef:
    223 		return renderMacro("Xr", troffEscape(inline.Text)+" "+troffEscape(inline.Section), false)
    224 	case InlineFlag:
    225 		return renderMacro("Fl", troffEscape(inline.Text), false)
    226 	default:
    227 		return ""
    228 	}
    229 }
    230 
    231 func needsNoSpace(prev, curr Inline) bool {
    232 	if trailingSpace(prev) || leadingSpace(curr) {
    233 		return false
    234 	}
    235 	return true
    236 }
    237 
    238 func leadingSpace(inline Inline) bool {
    239 	switch inline.Kind {
    240 	case InlineText:
    241 		return len(inline.Text) != 0 && isSpaceByte(inline.Text[0])
    242 	case InlineEmph:
    243 		return len(inline.Children) != 0 && leadingSpace(inline.Children[0])
    244 	default:
    245 		return false
    246 	}
    247 }
    248 
    249 func trailingSpace(inline Inline) bool {
    250 	switch inline.Kind {
    251 	case InlineText:
    252 		return len(inline.Text) != 0 && isSpaceByte(inline.Text[len(inline.Text)-1])
    253 	case InlineEmph:
    254 		return len(inline.Children) != 0 && trailingSpace(inline.Children[len(inline.Children)-1])
    255 	default:
    256 		return false
    257 	}
    258 }
    259 
    260 func isSpaceByte(b byte) bool { return b == ' ' || b == '\t' || b == '\n' }