ox-man.el 42 KB

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