ox-man.el 38 KB

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