ox-man.el 37 KB

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