ox-man.el 38 KB

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