ox-man.el 41 KB

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