ox-man.el 41 KB

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