roff.go 6.0 KB

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