ox-man.el 41 KB

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