ox-man.el 41 KB

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