ox-man.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. ;; ox-man.el --- Man Back-End for Org Export Engine -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2011-2018 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Luis R Anaya <papoanaya aroba hot mail punto com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This library implements a Man back-end for Org generic exporter.
  20. ;;
  21. ;; To test it, run
  22. ;;
  23. ;; M-: (org-export-to-buffer 'man "*Test Man*") RET
  24. ;;
  25. ;; in an Org buffer then switch to the buffer to see the Man export.
  26. ;; See ox.el for more details on how this exporter works.
  27. ;;
  28. ;; It introduces one new buffer keywords:
  29. ;; "MAN_CLASS_OPTIONS".
  30. ;;; Code:
  31. (require 'cl-lib)
  32. (require 'ox)
  33. (defvar org-export-man-default-packages-alist)
  34. (defvar org-export-man-packages-alist)
  35. (defvar orgtbl-exp-regexp)
  36. ;;; Define Back-End
  37. (org-export-define-backend 'man
  38. '((babel-call . org-man-babel-call)
  39. (bold . org-man-bold)
  40. (center-block . org-man-center-block)
  41. (code . org-man-code)
  42. (drawer . org-man-drawer)
  43. (dynamic-block . org-man-dynamic-block)
  44. (entity . org-man-entity)
  45. (example-block . org-man-example-block)
  46. (export-block . org-man-export-block)
  47. (export-snippet . org-man-export-snippet)
  48. (fixed-width . org-man-fixed-width)
  49. (footnote-definition . org-man-footnote-definition)
  50. (footnote-reference . org-man-footnote-reference)
  51. (headline . org-man-headline)
  52. (horizontal-rule . org-man-horizontal-rule)
  53. (inline-babel-call . org-man-inline-babel-call)
  54. (inline-src-block . org-man-inline-src-block)
  55. (inlinetask . org-man-inlinetask)
  56. (italic . org-man-italic)
  57. (item . org-man-item)
  58. (keyword . org-man-keyword)
  59. (line-break . org-man-line-break)
  60. (link . org-man-link)
  61. (node-property . org-man-node-property)
  62. (paragraph . org-man-paragraph)
  63. (plain-list . org-man-plain-list)
  64. (plain-text . org-man-plain-text)
  65. (planning . org-man-planning)
  66. (property-drawer . org-man-property-drawer)
  67. (quote-block . org-man-quote-block)
  68. (radio-target . org-man-radio-target)
  69. (section . org-man-section)
  70. (special-block . org-man-special-block)
  71. (src-block . org-man-src-block)
  72. (statistics-cookie . org-man-statistics-cookie)
  73. (strike-through . org-man-strike-through)
  74. (subscript . org-man-subscript)
  75. (superscript . org-man-superscript)
  76. (table . org-man-table)
  77. (table-cell . org-man-table-cell)
  78. (table-row . org-man-table-row)
  79. (target . org-man-target)
  80. (template . org-man-template)
  81. (timestamp . org-man-timestamp)
  82. (underline . org-man-underline)
  83. (verbatim . org-man-verbatim)
  84. (verse-block . org-man-verse-block))
  85. :menu-entry
  86. '(?M "Export to MAN"
  87. ((?m "As MAN file" org-man-export-to-man)
  88. (?p "As PDF file" org-man-export-to-pdf)
  89. (?o "As PDF file and open"
  90. (lambda (a s v b)
  91. (if a (org-man-export-to-pdf t s v b)
  92. (org-open-file (org-man-export-to-pdf nil s v b)))))))
  93. :options-alist
  94. '((:man-class "MAN_CLASS" nil nil t)
  95. (:man-class-options "MAN_CLASS_OPTIONS" nil nil t)
  96. (:man-header-extra "MAN_HEADER" nil nil newline)
  97. ;; Other variables.
  98. (:man-tables-centered nil nil org-man-tables-centered)
  99. (:man-tables-verbatim nil nil org-man-tables-verbatim)
  100. (:man-table-scientific-notation nil nil org-man-table-scientific-notation)
  101. (:man-source-highlight nil nil org-man-source-highlight)
  102. (:man-source-highlight-langs nil nil org-man-source-highlight-langs)))
  103. ;;; User Configurable Variables
  104. (defgroup org-export-man nil
  105. "Options for exporting Org mode files to Man."
  106. :tag "Org Export Man"
  107. :group 'org-export)
  108. ;;; Tables
  109. (defcustom org-man-tables-centered t
  110. "When non-nil, tables are exported in a center environment."
  111. :group 'org-export-man
  112. :version "24.4"
  113. :package-version '(Org . "8.0")
  114. :type 'boolean)
  115. (defcustom org-man-tables-verbatim nil
  116. "When non-nil, tables are exported verbatim."
  117. :group 'org-export-man
  118. :version "24.4"
  119. :package-version '(Org . "8.0")
  120. :type 'boolean)
  121. (defcustom org-man-table-scientific-notation "%sE%s"
  122. "Format string to display numbers in scientific notation.
  123. The format should have \"%s\" twice, for mantissa and exponent
  124. \(i.e. \"%s\\\\times10^{%s}\").
  125. When nil, no transformation is made."
  126. :group 'org-export-man
  127. :version "24.4"
  128. :package-version '(Org . "8.0")
  129. :type '(choice
  130. (string :tag "Format string")
  131. (const :tag "No formatting")))
  132. ;;; Inlinetasks
  133. ;; Src blocks
  134. (defcustom org-man-source-highlight nil
  135. "Use GNU source highlight to embellish source blocks "
  136. :group 'org-export-man
  137. :version "24.4"
  138. :package-version '(Org . "8.0")
  139. :type 'boolean)
  140. (defcustom org-man-source-highlight-langs
  141. '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
  142. (scheme "scheme")
  143. (c "c") (cc "cpp") (csharp "csharp") (d "d")
  144. (fortran "fortran") (cobol "cobol") (pascal "pascal")
  145. (ada "ada") (asm "asm")
  146. (perl "perl") (cperl "perl")
  147. (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
  148. (java "java") (javascript "javascript")
  149. (tex "latex")
  150. (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
  151. (ocaml "caml") (caml "caml")
  152. (sql "sql") (sqlite "sql")
  153. (html "html") (css "css") (xml "xml")
  154. (bat "bat") (bison "bison") (clipper "clipper")
  155. (ldap "ldap") (opa "opa")
  156. (php "php") (postscript "postscript") (prolog "prolog")
  157. (properties "properties") (makefile "makefile")
  158. (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
  159. "Alist mapping languages to their listing language counterpart.
  160. The key is a symbol, the major mode symbol without the \"-mode\".
  161. The value is the string that should be inserted as the language
  162. parameter for the listings package. If the mode name and the
  163. listings name are the same, the language does not need an entry
  164. in this list - but it does not hurt if it is present."
  165. :group 'org-export-man
  166. :version "24.4"
  167. :package-version '(Org . "8.0")
  168. :type '(repeat
  169. (list
  170. (symbol :tag "Major mode ")
  171. (string :tag "Listings language"))))
  172. ;;; Compilation
  173. (defcustom org-man-pdf-process
  174. '("tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
  175. "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf"
  176. "tbl %f | eqn | groff -man | ps2pdf - > %b.pdf")
  177. "Commands to process a Man file to a PDF file.
  178. This is a list of strings, each of them will be given to the
  179. shell as a command. %f in the command will be replaced by the
  180. relative file name, %F by the absolute file name, %b by the file
  181. base name (i.e. without directory and extension parts), %o by the
  182. base directory of the file and %O by the absolute file name of
  183. the output file.
  184. By default, Org uses 3 runs of to do the processing.
  185. Alternatively, this may be a Lisp function that does the
  186. processing. This function should accept the file name as
  187. its single argument."
  188. :group 'org-export-pdf
  189. :group 'org-export-man
  190. :version "24.4"
  191. :package-version '(Org . "8.0")
  192. :type '(choice
  193. (repeat :tag "Shell command sequence"
  194. (string :tag "Shell command"))
  195. (const :tag "2 runs of pdfgroff"
  196. ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  197. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf" ))
  198. (const :tag "3 runs of pdfgroff"
  199. ("tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  200. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"
  201. "tbl %f | eqn | groff -mm | ps2pdf - > %b.pdf"))
  202. (function)))
  203. (defcustom org-man-logfiles-extensions
  204. '("log" "out" "toc")
  205. "The list of file extensions to consider as Man logfiles."
  206. :group 'org-export-man
  207. :version "24.4"
  208. :package-version '(Org . "8.0")
  209. :type '(repeat (string :tag "Extension")))
  210. (defcustom org-man-remove-logfiles t
  211. "Non-nil means remove the logfiles produced by PDF production.
  212. These are the .aux, .log, .out, and .toc files."
  213. :group 'org-export-man
  214. :version "24.4"
  215. :package-version '(Org . "8.0")
  216. :type 'boolean)
  217. ;;; Internal Functions
  218. (defun org-man--caption/label-string (element info)
  219. "Return caption and label Man string for ELEMENT.
  220. INFO is a plist holding contextual information. If there's no
  221. caption nor label, return the empty string.
  222. For non-floats, see `org-man--wrap-label'."
  223. (let ((label (org-element-property :label element))
  224. (main (org-export-get-caption element))
  225. (short (org-export-get-caption element t)))
  226. (cond ((and (not main) (not label)) "")
  227. ((not main) (format "\\fI%s\\fP" label))
  228. ;; Option caption format with short name.
  229. (short (format "\\fR%s\\fP - \\fI\\P - %s\n"
  230. (org-export-data short info)
  231. (org-export-data main info)))
  232. ;; Standard caption format.
  233. (t (format "\\fR%s\\fP" (org-export-data main info))))))
  234. (defun org-man--wrap-label (element output)
  235. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  236. This function shouldn't be used for floats. See
  237. `org-man--caption/label-string'."
  238. (let ((label (org-element-property :name element)))
  239. (if (or (not output) (not label) (string= output "") (string= label ""))
  240. output
  241. (concat (format "%s\n.br\n" label) output))))
  242. (defun org-man--protect-text (text)
  243. "Protect minus and backslash characters in string TEXT."
  244. (replace-regexp-in-string "-" "\\-" text nil t))
  245. ;;; Template
  246. (defun org-man-template (contents info)
  247. "Return complete document string after Man conversion.
  248. CONTENTS is the transcoded contents string. INFO is a plist
  249. holding export options."
  250. (let* ((title (when (plist-get info :with-title)
  251. (org-export-data (plist-get info :title) info)))
  252. (attr (read (format "(%s)"
  253. (mapconcat
  254. #'identity
  255. (list (plist-get info :man-class-options))
  256. " "))))
  257. (section-item (plist-get attr :section-id)))
  258. (concat
  259. (cond
  260. ((and title (stringp section-item))
  261. (format ".TH \"%s\" \"%s\" \n" title section-item))
  262. ((and (string= "" title) (stringp section-item))
  263. (format ".TH \"%s\" \"%s\" \n" " " section-item))
  264. (title
  265. (format ".TH \"%s\" \"1\" \n" title))
  266. (t
  267. ".TH \" \" \"1\" "))
  268. contents)))
  269. ;;; Transcode Functions
  270. ;;; Babel Call
  271. ;;
  272. ;; Babel Calls are ignored.
  273. ;;; Bold
  274. (defun org-man-bold (_bold contents _info)
  275. "Transcode BOLD from Org to Man.
  276. CONTENTS is the text with bold markup. INFO is a plist holding
  277. contextual information."
  278. (format "\\fB%s\\fP" contents))
  279. ;;; Center Block
  280. (defun org-man-center-block (center-block contents _info)
  281. "Transcode a CENTER-BLOCK element from Org to Man.
  282. CONTENTS holds the contents of the center block. INFO is a plist
  283. holding contextual information."
  284. (org-man--wrap-label
  285. center-block
  286. (format ".ce %d\n.nf\n%s\n.fi"
  287. (- (length (split-string contents "\n")) 1 )
  288. contents)))
  289. ;;; Code
  290. (defun org-man-code (code _contents _info)
  291. "Transcode a CODE object from Org to Man."
  292. (format "\\fC%s\\fP"
  293. (org-man--protect-text (org-element-property :value code))))
  294. ;;; Drawer
  295. (defun org-man-drawer (_drawer contents _info)
  296. "Transcode a DRAWER element from Org to Man.
  297. DRAWER holds the drawer information
  298. CONTENTS holds the contents of the block.
  299. INFO is a plist holding contextual information. "
  300. contents)
  301. ;;; Dynamic Block
  302. (defun org-man-dynamic-block (dynamic-block contents _info)
  303. "Transcode a DYNAMIC-BLOCK element from Org to Man.
  304. CONTENTS holds the contents of the block. INFO is a plist
  305. holding contextual information. See `org-export-data'."
  306. (org-man--wrap-label dynamic-block contents))
  307. ;;; Entity
  308. (defun org-man-entity (entity _contents _info)
  309. "Transcode an ENTITY object from Org to Man.
  310. CONTENTS are the definition itself. INFO is a plist holding
  311. contextual information."
  312. (org-element-property :utf-8 entity))
  313. ;;; Example Block
  314. (defun org-man-example-block (example-block _contents info)
  315. "Transcode an EXAMPLE-BLOCK element from Org to Man.
  316. CONTENTS is nil. INFO is a plist holding contextual
  317. information."
  318. (org-man--wrap-label
  319. example-block
  320. (format ".RS\n.nf\n%s\n.fi\n.RE"
  321. (org-export-format-code-default example-block info))))
  322. ;;; Export Block
  323. (defun org-man-export-block (export-block _contents _info)
  324. "Transcode a EXPORT-BLOCK element from Org to Man.
  325. CONTENTS is nil. INFO is a plist holding contextual information."
  326. (when (string= (org-element-property :type export-block) "MAN")
  327. (org-remove-indentation (org-element-property :value export-block))))
  328. ;;; Export Snippet
  329. (defun org-man-export-snippet (export-snippet _contents _info)
  330. "Transcode a EXPORT-SNIPPET object from Org to Man.
  331. CONTENTS is nil. INFO is a plist holding contextual information."
  332. (when (eq (org-export-snippet-backend export-snippet) 'man)
  333. (org-element-property :value export-snippet)))
  334. ;;; Fixed Width
  335. (defun org-man-fixed-width (fixed-width _contents _info)
  336. "Transcode a FIXED-WIDTH element from Org to Man.
  337. CONTENTS is nil. INFO is a plist holding contextual information."
  338. (org-man--wrap-label
  339. fixed-width
  340. (format "\\fC\n%s\\fP"
  341. (org-remove-indentation
  342. (org-element-property :value fixed-width)))))
  343. ;;; Footnote Definition
  344. ;;
  345. ;; Footnote Definitions are ignored.
  346. ;;; Footnote References
  347. ;;
  348. ;; Footnote References are Ignored
  349. ;;; Headline
  350. (defun org-man-headline (headline contents info)
  351. "Transcode a HEADLINE element from Org to Man.
  352. CONTENTS holds the contents of the headline. INFO is a plist
  353. holding contextual information."
  354. (let* ((level (org-export-get-relative-level headline info))
  355. ;; Section formatting will set two placeholders: one for the
  356. ;; title and the other for the contents.
  357. (section-fmt
  358. (pcase level
  359. (1 ".SH \"%s\"\n%s")
  360. (2 ".SS \"%s\"\n%s")
  361. (3 ".SS \"%s\"\n%s")
  362. (_ nil)))
  363. (text (org-export-data (org-element-property :title headline) info)))
  364. (cond
  365. ;; Case 1: This is a footnote section: ignore it.
  366. ((org-element-property :footnote-section-p headline) nil)
  367. ;; Case 2. This is a deep sub-tree: export it as a list item.
  368. ;; Also export as items headlines for which no section
  369. ;; format has been found.
  370. ((or (not section-fmt) (org-export-low-level-p headline info))
  371. ;; Build the real contents of the sub-tree.
  372. (let ((low-level-body
  373. (concat
  374. ;; If the headline is the first sibling, start a list.
  375. (when (org-export-first-sibling-p headline info)
  376. (format "%s\n" ".RS"))
  377. ;; Itemize headline
  378. ".TP\n.ft I\n" text "\n.ft\n"
  379. contents ".RE")))
  380. ;; If headline is not the last sibling simply return
  381. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  382. ;; blank line.
  383. (if (not (org-export-last-sibling-p headline info)) low-level-body
  384. (replace-regexp-in-string
  385. "[ \t\n]*\\'" ""
  386. low-level-body))))
  387. ;; Case 3. Standard headline. Export it as a section.
  388. (t (format section-fmt text contents )))))
  389. ;;; Horizontal Rule
  390. ;; Not supported
  391. ;;; Inline Babel Call
  392. ;;
  393. ;; Inline Babel Calls are ignored.
  394. ;;; Inline Src Block
  395. (defun org-man-inline-src-block (inline-src-block _contents info)
  396. "Transcode an INLINE-SRC-BLOCK element from Org to Man.
  397. CONTENTS holds the contents of the item. INFO is a plist holding
  398. contextual information."
  399. (let* ((code (org-element-property :value inline-src-block)))
  400. (cond
  401. ((plist-get info :man-source-highlight)
  402. (let* ((tmpdir temporary-file-directory)
  403. (in-file (make-temp-name
  404. (expand-file-name "srchilite" tmpdir)))
  405. (out-file (make-temp-name
  406. (expand-file-name "reshilite" tmpdir)))
  407. (org-lang (org-element-property :language inline-src-block))
  408. (lst-lang
  409. (cadr (assq (intern org-lang)
  410. (plist-get info :man-source-highlight-langs))))
  411. (cmd (concat (expand-file-name "source-highlight")
  412. " -s " lst-lang
  413. " -f groff_man"
  414. " -i " in-file
  415. " -o " out-file )))
  416. (if lst-lang
  417. (let ((code-block "" ))
  418. (with-temp-file in-file (insert code))
  419. (shell-command cmd)
  420. (setq code-block (org-file-contents out-file))
  421. (delete-file in-file)
  422. (delete-file out-file)
  423. code-block)
  424. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE\n"
  425. code))))
  426. ;; Do not use a special package: transcode it verbatim.
  427. (t
  428. (concat ".RS\n.nf\n" "\\fC" "\n" code "\n"
  429. "\\fP\n.fi\n.RE\n")))))
  430. ;;; Inlinetask
  431. ;;; Italic
  432. (defun org-man-italic (_italic contents _info)
  433. "Transcode ITALIC from Org to Man.
  434. CONTENTS is the text with italic markup. INFO is a plist holding
  435. contextual information."
  436. (format "\\fI%s\\fP" contents))
  437. ;;; Item
  438. (defun org-man-item (item contents info)
  439. "Transcode an ITEM element from Org to Man.
  440. CONTENTS holds the contents of the item. INFO is a plist holding
  441. contextual information."
  442. (let* ((bullet (org-element-property :bullet item))
  443. (type (org-element-property :type (org-element-property :parent item)))
  444. (checkbox (pcase (org-element-property :checkbox item)
  445. (`on "\\o'\\(sq\\(mu'")
  446. (`off "\\(sq ")
  447. (`trans "\\o'\\(sq\\(mi'")))
  448. (tag (let ((tag (org-element-property :tag item)))
  449. ;; Check-boxes must belong to the tag.
  450. (and tag (format "\\fB%s\\fP"
  451. (concat checkbox
  452. (org-export-data tag info)))))))
  453. (if (and (null tag) (null checkbox))
  454. (let* ((bullet (org-trim bullet))
  455. (marker (cond ((string= "-" bullet) "\\(em")
  456. ((string= "*" bullet) "\\(bu")
  457. ((eq type 'ordered)
  458. (format "%s " (org-trim bullet)))
  459. (t "\\(dg"))))
  460. (concat ".IP " marker " 4\n"
  461. (org-trim (or contents " " ))))
  462. (concat ".TP\n" (or tag (concat " " checkbox)) "\n"
  463. (org-trim (or contents " " ))))))
  464. ;;; Keyword
  465. (defun org-man-keyword (keyword _contents _info)
  466. "Transcode a KEYWORD element from Org to Man.
  467. CONTENTS is nil. INFO is a plist holding contextual information."
  468. (let ((key (org-element-property :key keyword))
  469. (value (org-element-property :value keyword)))
  470. (cond
  471. ((string= key "MAN") value)
  472. ((string= key "INDEX") nil)
  473. ((string= key "TOC" ) nil))))
  474. ;;; Line Break
  475. (defun org-man-line-break (_line-break _contents _info)
  476. "Transcode a LINE-BREAK object from Org to Man.
  477. CONTENTS is nil. INFO is a plist holding contextual information."
  478. "\n.br\n")
  479. ;;; Link
  480. (defun org-man-link (link desc _info)
  481. "Transcode a LINK object from Org to Man.
  482. DESC is the description part of the link, or the empty string.
  483. INFO is a plist holding contextual information. See
  484. `org-export-data'."
  485. (let* ((type (org-element-property :type link))
  486. (raw-path (org-element-property :path link))
  487. ;; Ensure DESC really exists, or set it to nil.
  488. (desc (and (not (string= desc "")) desc))
  489. (path (cond
  490. ((member type '("http" "https" "ftp" "mailto"))
  491. (concat type ":" raw-path))
  492. ((string= type "file") (org-export-file-uri raw-path))
  493. (t raw-path))))
  494. (cond
  495. ;; Link type is handled by a special function.
  496. ((org-export-custom-protocol-maybe link desc 'man))
  497. ;; External link with a description part.
  498. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  499. ;; External link without a description part.
  500. (path (format "\\fI%s\\fP" path))
  501. ;; No path, only description. Try to do something useful.
  502. (t (format "\\fI%s\\fP" desc)))))
  503. ;;;; Node Property
  504. (defun org-man-node-property (node-property _contents _info)
  505. "Transcode a NODE-PROPERTY element from Org to Man.
  506. CONTENTS is nil. INFO is a plist holding contextual
  507. information."
  508. (format "%s:%s"
  509. (org-element-property :key node-property)
  510. (let ((value (org-element-property :value node-property)))
  511. (if value (concat " " value) ""))))
  512. ;;; Paragraph
  513. (defun org-man-paragraph (paragraph contents _info)
  514. "Transcode a PARAGRAPH element from Org to Man.
  515. CONTENTS is the contents of the paragraph, as a string. INFO is
  516. the plist used as a communication channel."
  517. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  518. (when parent
  519. (let ((parent-type (car parent))
  520. (fixed-paragraph ""))
  521. (cond ((and (eq parent-type 'item)
  522. (plist-get (nth 1 parent) :bullet ))
  523. (setq fixed-paragraph (concat "" contents)))
  524. ((eq parent-type 'section)
  525. (setq fixed-paragraph (concat ".PP\n" contents)))
  526. ((eq parent-type 'footnote-definition)
  527. (setq fixed-paragraph contents))
  528. (t (setq fixed-paragraph (concat "" contents))))
  529. fixed-paragraph ))))
  530. ;;; Plain List
  531. (defun org-man-plain-list (_plain-list contents _info)
  532. "Transcode a PLAIN-LIST element from Org to Man.
  533. CONTENTS is the contents of the list. INFO is a plist holding
  534. contextual information."
  535. contents)
  536. ;;; Plain Text
  537. (defun org-man-plain-text (text info)
  538. "Transcode a TEXT string from Org to Man.
  539. TEXT is the string to transcode. INFO is a plist holding
  540. contextual information."
  541. (let ((output text))
  542. ;; Protect various chars.
  543. (setq output (replace-regexp-in-string
  544. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  545. "$\\" output nil t 1))
  546. ;; Activate smart quotes. Be sure to provide original TEXT string
  547. ;; since OUTPUT may have been modified.
  548. (when (plist-get info :with-smart-quotes)
  549. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  550. ;; Handle break preservation if required.
  551. (when (plist-get info :preserve-breaks)
  552. (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
  553. output)))
  554. ;; Return value.
  555. output))
  556. ;;; Planning
  557. ;;; Property Drawer
  558. (defun org-man-property-drawer (_property-drawer contents _info)
  559. "Transcode a PROPERTY-DRAWER element from Org to Man.
  560. CONTENTS holds the contents of the drawer. INFO is a plist
  561. holding contextual information."
  562. (and (org-string-nw-p contents)
  563. (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
  564. ;;; Quote Block
  565. (defun org-man-quote-block (quote-block contents _info)
  566. "Transcode a QUOTE-BLOCK element from Org to Man.
  567. CONTENTS holds the contents of the block. INFO is a plist
  568. holding contextual information."
  569. (org-man--wrap-label
  570. quote-block
  571. (format ".RS\n%s\n.RE" contents)))
  572. ;;; Radio Target
  573. (defun org-man-radio-target (_radio-target text _info)
  574. "Transcode a RADIO-TARGET object from Org to Man.
  575. TEXT is the text of the target. INFO is a plist holding
  576. contextual information."
  577. text)
  578. ;;; Section
  579. (defun org-man-section (_section contents _info)
  580. "Transcode a SECTION element from Org to Man.
  581. CONTENTS holds the contents of the section. INFO is a plist
  582. holding contextual information."
  583. contents)
  584. ;;; Special Block
  585. (defun org-man-special-block (special-block contents _info)
  586. "Transcode a SPECIAL-BLOCK element from Org to Man.
  587. CONTENTS holds the contents of the block. INFO is a plist
  588. holding contextual information."
  589. (org-man--wrap-label special-block (format "%s\n" contents)))
  590. ;;; Src Block
  591. (defun org-man-src-block (src-block _contents info)
  592. "Transcode a SRC-BLOCK element from Org to Man.
  593. CONTENTS holds the contents of the item. INFO is a plist holding
  594. contextual information."
  595. (if (not (plist-get info :man-source-highlight))
  596. (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
  597. (org-export-format-code-default src-block info))
  598. (let* ((tmpdir temporary-file-directory)
  599. (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
  600. (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
  601. (code (org-element-property :value src-block))
  602. (org-lang (org-element-property :language src-block))
  603. (lst-lang
  604. (cadr (assq (intern org-lang)
  605. (plist-get info :man-source-highlight-langs))))
  606. (cmd (concat "source-highlight"
  607. " -s " lst-lang
  608. " -f groff_man "
  609. " -i " in-file
  610. " -o " out-file)))
  611. (if lst-lang
  612. (let ((code-block ""))
  613. (with-temp-file in-file (insert code))
  614. (shell-command cmd)
  615. (setq code-block (org-file-contents out-file))
  616. (delete-file in-file)
  617. (delete-file out-file)
  618. code-block)
  619. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))
  620. ;;; Statistics Cookie
  621. (defun org-man-statistics-cookie (statistics-cookie _contents _info)
  622. "Transcode a STATISTICS-COOKIE object from Org to Man.
  623. CONTENTS is nil. INFO is a plist holding contextual information."
  624. (org-element-property :value statistics-cookie))
  625. ;;; Strike-Through
  626. (defun org-man-strike-through (_strike-through contents _info)
  627. "Transcode STRIKE-THROUGH from Org to Man.
  628. CONTENTS is the text with strike-through markup. INFO is a plist
  629. holding contextual information."
  630. (format "\\fI%s\\fP" contents))
  631. ;;; Subscript
  632. (defun org-man-subscript (_subscript contents _info)
  633. "Transcode a SUBSCRIPT object from Org to Man.
  634. CONTENTS is the contents of the object. INFO is a plist holding
  635. contextual information."
  636. (format "\\d\\s-2%s\\s+2\\u" contents))
  637. ;;; Superscript "^_%s$
  638. (defun org-man-superscript (_superscript contents _info)
  639. "Transcode a SUPERSCRIPT object from Org to Man.
  640. CONTENTS is the contents of the object. INFO is a plist holding
  641. contextual information."
  642. (format "\\u\\s-2%s\\s+2\\d" contents))
  643. ;;; Table
  644. ;;
  645. ;; `org-man-table' is the entry point for table transcoding. It
  646. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  647. ;; delegates the job to either `org-man-table--table.el-table' or
  648. ;; `org-man-table--org-table' functions, depending of the type of
  649. ;; the table.
  650. ;;
  651. ;; `org-man-table--align-string' is a subroutine used to build
  652. ;; alignment string for Org tables.
  653. (defun org-man-table (table contents info)
  654. "Transcode a TABLE element from Org to Man.
  655. CONTENTS is the contents of the table. INFO is a plist holding
  656. contextual information."
  657. (cond
  658. ;; Case 1: verbatim table.
  659. ((or (plist-get info :man-tables-verbatim)
  660. (let ((attr (read (format "(%s)"
  661. (mapconcat
  662. #'identity
  663. (org-element-property :attr_man table)
  664. " ")))))
  665. (and attr (plist-get attr :verbatim))))
  666. (format ".nf\n\\fC%s\\fP\n.fi"
  667. ;; Re-create table, without affiliated keywords.
  668. (org-trim
  669. (org-element-interpret-data
  670. `(table nil ,@(org-element-contents table))))))
  671. ;; Case 2: Standard table.
  672. (t (org-man-table--org-table table contents info))))
  673. (defun org-man-table--align-string (divider table info)
  674. "Return an appropriate Man alignment string.
  675. TABLE is the considered table. INFO is a plist used as
  676. a communication channel."
  677. (let (alignment)
  678. ;; Extract column groups and alignment from first (non-rule) row.
  679. (org-element-map
  680. (org-element-map table 'table-row
  681. (lambda (row)
  682. (and (eq (org-element-property :type row) 'standard) row))
  683. info 'first-match)
  684. 'table-cell
  685. (lambda (cell)
  686. (let* ((borders (org-export-table-cell-borders cell info))
  687. (raw-width (org-export-table-cell-width cell info))
  688. (width-cm (when raw-width (/ raw-width 5)))
  689. (width (if raw-width (format "w(%dc)"
  690. (if (< width-cm 1) 1 width-cm)) "")))
  691. ;; Check left border for the first cell only.
  692. (when (and (memq 'left borders) (not alignment))
  693. (push "|" alignment))
  694. (push
  695. (concat (pcase (org-export-table-cell-alignment cell info)
  696. (`left "l") (`right "r") (`center "c"))
  697. width
  698. divider)
  699. alignment)
  700. (when (memq 'right borders) (push "|" alignment))))
  701. info)
  702. (apply #'concat (reverse alignment))))
  703. (defun org-man-table--org-table (table contents info)
  704. "Return appropriate Man code for an Org table.
  705. TABLE is the table type element to transcode. CONTENTS is its
  706. contents, as a string. INFO is a plist used as a communication
  707. channel.
  708. This function assumes TABLE has `org' as its `:type' attribute."
  709. (let* ((attr (org-export-read-attribute :attr_man table))
  710. (caption (and (not (plist-get attr :disable-caption))
  711. (org-man--caption/label-string table info)))
  712. (divider (if (plist-get attr :divider) "|" " "))
  713. ;; Determine alignment string.
  714. (alignment (org-man-table--align-string divider table info))
  715. ;; Extract others display options.
  716. (lines (org-split-string contents "\n"))
  717. (attr-list
  718. (delq nil
  719. (list
  720. (and (plist-get attr :expand) "expand")
  721. (let ((placement (plist-get attr :placement)))
  722. (cond ((string= placement 'center) "center")
  723. ((string= placement 'left) nil)
  724. ((plist-get info :man-tables-centered) "center")
  725. (t "")))
  726. (or (plist-get attr :boxtype) "box"))))
  727. (title-line (plist-get attr :title-line))
  728. (long-cells (plist-get attr :long-cells))
  729. (table-format (concat
  730. (format "%s" (or (car attr-list) "" ))
  731. (or
  732. (let ((output-list '()))
  733. (when (cdr attr-list)
  734. (dolist (attr-item (cdr attr-list))
  735. (setq output-list (concat output-list (format ",%s" attr-item)))))
  736. output-list)
  737. "")))
  738. (first-line (when lines (org-split-string (car lines) "\t"))))
  739. ;; Prepare the final format string for the table.
  740. (cond
  741. ;; Others.
  742. (lines (concat ".TS\n " table-format ";\n"
  743. (format "%s.\n"
  744. (let ((final-line ""))
  745. (when title-line
  746. (dotimes (_ (length first-line))
  747. (setq final-line (concat final-line "cb" divider))))
  748. (setq final-line (concat final-line "\n"))
  749. (if alignment
  750. (setq final-line (concat final-line alignment))
  751. (dotimes (_ (length first-line))
  752. (setq final-line (concat final-line "c" divider))))
  753. final-line ))
  754. (format "%s.TE\n"
  755. (let ((final-line "")
  756. (long-line "")
  757. (lines (org-split-string contents "\n")))
  758. (dolist (line-item lines)
  759. (setq long-line "")
  760. (if long-cells
  761. (progn
  762. (if (string= line-item "_")
  763. (setq long-line (format "%s\n" line-item))
  764. ;; else string =
  765. (let ((cell-item-list (org-split-string line-item "\t")))
  766. (dolist (cell-item cell-item-list)
  767. (cond ((eq cell-item (car (last cell-item-list)))
  768. (setq long-line (concat long-line
  769. (format "T{\n%s\nT}\t\n" cell-item ))))
  770. (t
  771. (setq long-line (concat long-line
  772. (format "T{\n%s\nT}\t" cell-item ))))))
  773. long-line))
  774. ;; else long cells
  775. (setq final-line (concat final-line long-line )))
  776. (setq final-line (concat final-line line-item "\n"))))
  777. final-line))
  778. (and caption (format ".TB \"%s\"" caption)))))))
  779. ;;; Table Cell
  780. (defun org-man-table-cell (table-cell contents info)
  781. "Transcode a TABLE-CELL element from Org to Man
  782. CONTENTS is the cell contents. INFO is a plist used as
  783. a communication channel."
  784. (concat
  785. (let ((scientific-format (plist-get info :man-table-scientific-notation)))
  786. (if (and contents
  787. scientific-format
  788. (string-match orgtbl-exp-regexp contents))
  789. ;; Use appropriate format string for scientific notation.
  790. (format scientific-format
  791. (match-string 1 contents)
  792. (match-string 2 contents))
  793. contents))
  794. (when (org-export-get-next-element table-cell info) "\t")))
  795. ;;; Table Row
  796. (defun org-man-table-row (table-row contents info)
  797. "Transcode a TABLE-ROW element from Org to Man.
  798. CONTENTS is the contents of the row. INFO is a plist used as
  799. a communication channel."
  800. ;; Rules are ignored since table separators are deduced from borders
  801. ;; of the current row.
  802. (when (eq (org-element-property :type table-row) 'standard)
  803. (let ((borders
  804. ;; TABLE-ROW's borders are extracted from its first cell.
  805. (org-export-table-cell-borders
  806. (car (org-element-contents table-row)) info)))
  807. (concat
  808. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  809. contents
  810. (cond ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  811. ((memq 'below borders) "\n_"))))))
  812. ;;; Target
  813. (defun org-man-target (target _contents info)
  814. "Transcode a TARGET object from Org to Man.
  815. CONTENTS is nil. INFO is a plist holding contextual
  816. information."
  817. (format "\\fI%s\\fP" (org-export-get-reference target info)))
  818. ;;; Timestamp
  819. (defun org-man-timestamp (_timestamp _contents _info)
  820. "Transcode a TIMESTAMP object from Org to Man.
  821. CONTENTS is nil. INFO is a plist holding contextual information."
  822. "")
  823. ;;; Underline
  824. (defun org-man-underline (_underline contents _info)
  825. "Transcode UNDERLINE from Org to Man.
  826. CONTENTS is the text with underline markup. INFO is a plist
  827. holding contextual information."
  828. (format "\\fI%s\\fP" contents))
  829. ;;; Verbatim
  830. (defun org-man-verbatim (verbatim _contents _info)
  831. "Transcode a VERBATIM object from Org to Man."
  832. (format "\\fI%s\\fP"
  833. (org-man--protect-text (org-element-property :value verbatim))))
  834. ;;; Verse Block
  835. (defun org-man-verse-block (_verse-block contents _info)
  836. "Transcode a VERSE-BLOCK element from Org to Man.
  837. CONTENTS is verse block contents. INFO is a plist holding
  838. contextual information."
  839. (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
  840. ;;; Interactive functions
  841. (defun org-man-export-to-man
  842. (&optional async subtreep visible-only body-only ext-plist)
  843. "Export current buffer to a Man file.
  844. If narrowing is active in the current buffer, only export its
  845. narrowed part.
  846. If a region is active, export that region.
  847. A non-nil optional argument ASYNC means the process should happen
  848. asynchronously. The resulting file should be accessible through
  849. the `org-export-stack' interface.
  850. When optional argument SUBTREEP is non-nil, export the sub-tree
  851. at point, extracting information from the headline properties
  852. first.
  853. When optional argument VISIBLE-ONLY is non-nil, don't export
  854. contents of hidden elements.
  855. When optional argument BODY-ONLY is non-nil, only the body
  856. without any markers.
  857. EXT-PLIST, when provided, is a property list with external
  858. parameters overriding Org default settings, but still inferior to
  859. file-local settings.
  860. Return output file's name."
  861. (interactive)
  862. (let ((outfile (org-export-output-file-name ".man" subtreep)))
  863. (org-export-to-file 'man outfile
  864. async subtreep visible-only body-only ext-plist)))
  865. (defun org-man-export-to-pdf
  866. (&optional async subtreep visible-only body-only ext-plist)
  867. "Export current buffer to Groff then process through to PDF.
  868. If narrowing is active in the current buffer, only export its
  869. narrowed part.
  870. If a region is active, export that region.
  871. A non-nil optional argument ASYNC means the process should happen
  872. asynchronously. The resulting file should be accessible through
  873. the `org-export-stack' interface.
  874. When optional argument SUBTREEP is non-nil, export the sub-tree
  875. at point, extracting information from the headline properties
  876. first.
  877. When optional argument VISIBLE-ONLY is non-nil, don't export
  878. contents of hidden elements.
  879. When optional argument BODY-ONLY is non-nil, only write between
  880. markers.
  881. EXT-PLIST, when provided, is a property list with external
  882. parameters overriding Org default settings, but still inferior to
  883. file-local settings.
  884. Return PDF file's name."
  885. (interactive)
  886. (let ((outfile (org-export-output-file-name ".man" subtreep)))
  887. (org-export-to-file 'man outfile
  888. async subtreep visible-only body-only ext-plist
  889. (lambda (file) (org-latex-compile file)))))
  890. (defun org-man-compile (file)
  891. "Compile a Groff file.
  892. FILE is the name of the file being compiled. Processing is done
  893. through the command specified in `org-man-pdf-process'.
  894. Return PDF file name or an error if it couldn't be produced."
  895. (message "Processing Groff file %s..." file)
  896. (let ((output (org-compile-file file org-man-pdf-process "pdf")))
  897. (when org-man-remove-logfiles
  898. (let ((base (file-name-sans-extension output)))
  899. (dolist (ext org-man-logfiles-extensions)
  900. (let ((file (concat base "." ext)))
  901. (when (file-exists-p file) (delete-file file))))))
  902. (message "Process completed.")
  903. output))
  904. (provide 'ox-man)
  905. ;; Local variables:
  906. ;; generated-autoload-file: "org-loaddefs.el"
  907. ;; End:
  908. ;;; ox-man.el ends here