ox-man.el 41 KB

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