ox-man.el 41 KB

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