ox-man.el 38 KB

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