roff.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package md2man
  2. import (
  3. "bytes"
  4. "fmt"
  5. "html"
  6. "strings"
  7. "github.com/russross/blackfriday"
  8. )
  9. type roffRenderer struct{}
  10. func RoffRenderer(flags int) blackfriday.Renderer {
  11. return &roffRenderer{}
  12. }
  13. func (r *roffRenderer) GetFlags() int {
  14. return 0
  15. }
  16. func (r *roffRenderer) TitleBlock(out *bytes.Buffer, text []byte) {
  17. out.WriteString(".TH ")
  18. splitText := bytes.Split(text, []byte("\n"))
  19. for i, line := range splitText {
  20. line = bytes.TrimPrefix(line, []byte("% "))
  21. if i == 0 {
  22. line = bytes.Replace(line, []byte("("), []byte("\" \""), 1)
  23. line = bytes.Replace(line, []byte(")"), []byte("\" \""), 1)
  24. }
  25. line = append([]byte("\""), line...)
  26. line = append(line, []byte("\" ")...)
  27. out.Write(line)
  28. }
  29. out.WriteString(" \"\"\n")
  30. }
  31. func (r *roffRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
  32. out.WriteString("\n.PP\n.RS\n\n.nf\n")
  33. escapeSpecialChars(out, text)
  34. out.WriteString("\n.fi\n.RE\n")
  35. }
  36. func (r *roffRenderer) BlockQuote(out *bytes.Buffer, text []byte) {
  37. out.WriteString("\n.PP\n.RS\n")
  38. out.Write(text)
  39. out.WriteString("\n.RE\n")
  40. }
  41. func (r *roffRenderer) BlockHtml(out *bytes.Buffer, text []byte) {
  42. out.Write(text)
  43. }
  44. func (r *roffRenderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {
  45. marker := out.Len()
  46. switch {
  47. case marker == 0:
  48. // This is the doc header
  49. out.WriteString(".TH ")
  50. case level == 1:
  51. out.WriteString("\n\n.SH ")
  52. case level == 2:
  53. out.WriteString("\n.SH ")
  54. default:
  55. out.WriteString("\n.SS ")
  56. }
  57. if !text() {
  58. out.Truncate(marker)
  59. return
  60. }
  61. }
  62. func (r *roffRenderer) HRule(out *bytes.Buffer) {
  63. out.WriteString("\n.ti 0\n\\l'\\n(.lu'\n")
  64. }
  65. func (r *roffRenderer) List(out *bytes.Buffer, text func() bool, flags int) {
  66. marker := out.Len()
  67. out.WriteString(".IP ")
  68. if flags&blackfriday.LIST_TYPE_ORDERED != 0 {
  69. out.WriteString("\\(bu 2")
  70. } else {
  71. out.WriteString("\\n+[step" + string(flags) + "]")
  72. }
  73. out.WriteString("\n")
  74. if !text() {
  75. out.Truncate(marker)
  76. return
  77. }
  78. }
  79. func (r *roffRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
  80. out.WriteString("\n\\item ")
  81. out.Write(text)
  82. }
  83. func (r *roffRenderer) Paragraph(out *bytes.Buffer, text func() bool) {
  84. marker := out.Len()
  85. out.WriteString("\n.PP\n")
  86. if !text() {
  87. out.Truncate(marker)
  88. return
  89. }
  90. if marker != 0 {
  91. out.WriteString("\n")
  92. }
  93. }
  94. // TODO: This might now work
  95. func (r *roffRenderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
  96. out.WriteString(".TS\nallbox;\n")
  97. out.Write(header)
  98. out.Write(body)
  99. out.WriteString("\n.TE\n")
  100. }
  101. func (r *roffRenderer) TableRow(out *bytes.Buffer, text []byte) {
  102. if out.Len() > 0 {
  103. out.WriteString("\n")
  104. }
  105. out.Write(text)
  106. out.WriteString("\n")
  107. }
  108. func (r *roffRenderer) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
  109. if out.Len() > 0 {
  110. out.WriteString(" ")
  111. }
  112. out.Write(text)
  113. out.WriteString(" ")
  114. }
  115. // TODO: This is probably broken
  116. func (r *roffRenderer) TableCell(out *bytes.Buffer, text []byte, align int) {
  117. if out.Len() > 0 {
  118. out.WriteString("\t")
  119. }
  120. out.Write(text)
  121. out.WriteString("\t")
  122. }
  123. func (r *roffRenderer) Footnotes(out *bytes.Buffer, text func() bool) {
  124. }
  125. func (r *roffRenderer) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
  126. }
  127. func (r *roffRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
  128. out.WriteString("\n\\[la]")
  129. out.Write(link)
  130. out.WriteString("\\[ra]")
  131. }
  132. func (r *roffRenderer) CodeSpan(out *bytes.Buffer, text []byte) {
  133. out.WriteString("\\fB\\fC")
  134. escapeSpecialChars(out, text)
  135. out.WriteString("\\fR")
  136. }
  137. func (r *roffRenderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {
  138. out.WriteString("\\fB")
  139. out.Write(text)
  140. out.WriteString("\\fP")
  141. }
  142. func (r *roffRenderer) Emphasis(out *bytes.Buffer, text []byte) {
  143. out.WriteString("\\fI")
  144. out.Write(text)
  145. out.WriteString("\\fP")
  146. }
  147. func (r *roffRenderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
  148. }
  149. func (r *roffRenderer) LineBreak(out *bytes.Buffer) {
  150. out.WriteString("\n.br\n")
  151. }
  152. func (r *roffRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
  153. r.AutoLink(out, link, 0)
  154. }
  155. func (r *roffRenderer) RawHtmlTag(out *bytes.Buffer, tag []byte) {
  156. out.Write(tag)
  157. }
  158. func (r *roffRenderer) TripleEmphasis(out *bytes.Buffer, text []byte) {
  159. out.WriteString("\\s+2")
  160. out.Write(text)
  161. out.WriteString("\\s-2")
  162. }
  163. func (r *roffRenderer) StrikeThrough(out *bytes.Buffer, text []byte) {
  164. }
  165. func (r *roffRenderer) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
  166. }
  167. func (r *roffRenderer) Entity(out *bytes.Buffer, entity []byte) {
  168. out.WriteString(html.UnescapeString(string(entity)))
  169. }
  170. func processFooterText(text []byte) []byte {
  171. text = bytes.TrimPrefix(text, []byte("% "))
  172. newText := []byte{}
  173. textArr := strings.Split(string(text), ") ")
  174. for i, w := range textArr {
  175. if i == 0 {
  176. w = strings.Replace(w, "(", "\" \"", 1)
  177. w = fmt.Sprintf("\"%s\"", w)
  178. } else {
  179. w = fmt.Sprintf(" \"%s\"", w)
  180. }
  181. newText = append(newText, []byte(w)...)
  182. }
  183. newText = append(newText, []byte(" \"\"")...)
  184. return newText
  185. }
  186. func (r *roffRenderer) NormalText(out *bytes.Buffer, text []byte) {
  187. escapeSpecialChars(out, text)
  188. }
  189. func (r *roffRenderer) DocumentHeader(out *bytes.Buffer) {
  190. }
  191. func (r *roffRenderer) DocumentFooter(out *bytes.Buffer) {
  192. }
  193. func needsBackslash(c byte) bool {
  194. for _, r := range []byte("-_&\\~") {
  195. if c == r {
  196. return true
  197. }
  198. }
  199. return false
  200. }
  201. func escapeSpecialChars(out *bytes.Buffer, text []byte) {
  202. for i := 0; i < len(text); i++ {
  203. // directly copy normal characters
  204. org := i
  205. for i < len(text) && !needsBackslash(text[i]) {
  206. i++
  207. }
  208. if i > org {
  209. out.Write(text[org:i])
  210. }
  211. // escape a character
  212. if i >= len(text) {
  213. break
  214. }
  215. out.WriteByte('\\')
  216. out.WriteByte(text[i])
  217. }
  218. }