ox-man.el 38 KB

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