ox-man.el 41 KB

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