ox-man.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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-link-expand "org-attach" (link &optional buffer-or-name))
  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* ((type (org-element-property :type link))
  488. (raw-path (org-element-property :path link))
  489. ;; Ensure DESC really exists, or set it to nil.
  490. (desc (and (not (string= desc "")) desc))
  491. (path (cond
  492. ((member type '("http" "https" "ftp" "mailto"))
  493. (concat type ":" raw-path))
  494. ((member type '("file" "attachment"))
  495. (when (string= type "attachment")
  496. (setq raw-path (org-attach-link-expand link)))
  497. (org-export-file-uri raw-path))
  498. (t raw-path))))
  499. (cond
  500. ;; Link type is handled by a special function.
  501. ((org-export-custom-protocol-maybe link desc 'man info))
  502. ;; External link with a description part.
  503. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  504. ;; External link without a description part.
  505. (path (format "\\fI%s\\fP" path))
  506. ;; No path, only description. Try to do something useful.
  507. (t (format "\\fI%s\\fP" desc)))))
  508. ;;;; Node Property
  509. (defun org-man-node-property (node-property _contents _info)
  510. "Transcode a NODE-PROPERTY element from Org to Man.
  511. CONTENTS is nil. INFO is a plist holding contextual
  512. information."
  513. (format "%s:%s"
  514. (org-element-property :key node-property)
  515. (let ((value (org-element-property :value node-property)))
  516. (if value (concat " " value) ""))))
  517. ;;; Paragraph
  518. (defun org-man-paragraph (paragraph contents _info)
  519. "Transcode a PARAGRAPH element from Org to Man.
  520. CONTENTS is the contents of the paragraph, as a string. INFO is
  521. the plist used as a communication channel."
  522. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  523. (when parent
  524. (let ((parent-type (car parent))
  525. (fixed-paragraph ""))
  526. (cond ((and (eq parent-type 'item)
  527. (plist-get (nth 1 parent) :bullet ))
  528. (setq fixed-paragraph (concat "" contents)))
  529. ((eq parent-type 'section)
  530. (setq fixed-paragraph (concat ".PP\n" contents)))
  531. ((eq parent-type 'footnote-definition)
  532. (setq fixed-paragraph contents))
  533. (t (setq fixed-paragraph (concat "" contents))))
  534. fixed-paragraph ))))
  535. ;;; Plain List
  536. (defun org-man-plain-list (_plain-list contents _info)
  537. "Transcode a PLAIN-LIST element from Org to Man.
  538. CONTENTS is the contents of the list. INFO is a plist holding
  539. contextual information."
  540. contents)
  541. ;;; Plain Text
  542. (defun org-man-plain-text (text info)
  543. "Transcode a TEXT string from Org to Man.
  544. TEXT is the string to transcode. INFO is a plist holding
  545. contextual information."
  546. (let ((output text))
  547. ;; Protect various chars.
  548. (setq output (replace-regexp-in-string
  549. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  550. "$\\" output nil t 1))
  551. ;; Activate smart quotes. Be sure to provide original TEXT string
  552. ;; since OUTPUT may have been modified.
  553. (when (plist-get info :with-smart-quotes)
  554. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  555. ;; Handle break preservation if required.
  556. (when (plist-get info :preserve-breaks)
  557. (setq output (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n"
  558. output)))
  559. ;; Return value.
  560. output))
  561. ;;; Planning
  562. ;;; Property Drawer
  563. (defun org-man-property-drawer (_property-drawer contents _info)
  564. "Transcode a PROPERTY-DRAWER element from Org to Man.
  565. CONTENTS holds the contents of the drawer. INFO is a plist
  566. holding contextual information."
  567. (and (org-string-nw-p contents)
  568. (format ".RS\n.nf\n%s\n.fi\n.RE" contents)))
  569. ;;; Quote Block
  570. (defun org-man-quote-block (quote-block contents _info)
  571. "Transcode a QUOTE-BLOCK element from Org to Man.
  572. CONTENTS holds the contents of the block. INFO is a plist
  573. holding contextual information."
  574. (org-man--wrap-label
  575. quote-block
  576. (format ".RS\n%s\n.RE" contents)))
  577. ;;; Radio Target
  578. (defun org-man-radio-target (_radio-target text _info)
  579. "Transcode a RADIO-TARGET object from Org to Man.
  580. TEXT is the text of the target. INFO is a plist holding
  581. contextual information."
  582. text)
  583. ;;; Section
  584. (defun org-man-section (_section contents _info)
  585. "Transcode a SECTION element from Org to Man.
  586. CONTENTS holds the contents of the section. INFO is a plist
  587. holding contextual information."
  588. contents)
  589. ;;; Special Block
  590. (defun org-man-special-block (special-block contents _info)
  591. "Transcode a SPECIAL-BLOCK element from Org to Man.
  592. CONTENTS holds the contents of the block. INFO is a plist
  593. holding contextual information."
  594. (org-man--wrap-label special-block (format "%s\n" contents)))
  595. ;;; Src Block
  596. (defun org-man-src-block (src-block _contents info)
  597. "Transcode a SRC-BLOCK element from Org to Man.
  598. CONTENTS holds the contents of the item. INFO is a plist holding
  599. contextual information."
  600. (if (not (plist-get info :man-source-highlight))
  601. (format ".RS\n.nf\n\\fC%s\\fP\n.fi\n.RE\n\n"
  602. (org-export-format-code-default src-block info))
  603. (let* ((tmpdir temporary-file-directory)
  604. (in-file (make-temp-name (expand-file-name "srchilite" tmpdir)))
  605. (out-file (make-temp-name (expand-file-name "reshilite" tmpdir)))
  606. (code (org-element-property :value src-block))
  607. (org-lang (org-element-property :language src-block))
  608. (lst-lang
  609. (cadr (assq (intern org-lang)
  610. (plist-get info :man-source-highlight-langs))))
  611. (cmd (concat "source-highlight"
  612. " -s " lst-lang
  613. " -f groff_man "
  614. " -i " in-file
  615. " -o " out-file)))
  616. (if lst-lang
  617. (let ((code-block ""))
  618. (with-temp-file in-file (insert code))
  619. (shell-command cmd)
  620. (setq code-block (org-file-contents out-file))
  621. (delete-file in-file)
  622. (delete-file out-file)
  623. code-block)
  624. (format ".RS\n.nf\n\\fC\\m[black]%s\\m[]\\fP\n.fi\n.RE" code)))))
  625. ;;; Statistics Cookie
  626. (defun org-man-statistics-cookie (statistics-cookie _contents _info)
  627. "Transcode a STATISTICS-COOKIE object from Org to Man.
  628. CONTENTS is nil. INFO is a plist holding contextual information."
  629. (org-element-property :value statistics-cookie))
  630. ;;; Strike-Through
  631. (defun org-man-strike-through (_strike-through contents _info)
  632. "Transcode STRIKE-THROUGH from Org to Man.
  633. CONTENTS is the text with strike-through markup. INFO is a plist
  634. holding contextual information."
  635. (format "\\fI%s\\fP" contents))
  636. ;;; Subscript
  637. (defun org-man-subscript (_subscript contents _info)
  638. "Transcode a SUBSCRIPT object from Org to Man.
  639. CONTENTS is the contents of the object. INFO is a plist holding
  640. contextual information."
  641. (format "\\d\\s-2%s\\s+2\\u" contents))
  642. ;;; Superscript "^_%s$
  643. (defun org-man-superscript (_superscript contents _info)
  644. "Transcode a SUPERSCRIPT object from Org to Man.
  645. CONTENTS is the contents of the object. INFO is a plist holding
  646. contextual information."
  647. (format "\\u\\s-2%s\\s+2\\d" contents))
  648. ;;; Table
  649. ;;
  650. ;; `org-man-table' is the entry point for table transcoding. It
  651. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  652. ;; delegates the job to either `org-man-table--table.el-table' or
  653. ;; `org-man-table--org-table' functions, depending of the type of
  654. ;; the table.
  655. ;;
  656. ;; `org-man-table--align-string' is a subroutine used to build
  657. ;; alignment string for Org tables.
  658. (defun org-man-table (table contents info)
  659. "Transcode a TABLE element from Org to Man.
  660. CONTENTS is the contents of the table. INFO is a plist holding
  661. contextual information."
  662. (cond
  663. ;; Case 1: verbatim table.
  664. ((or (plist-get info :man-tables-verbatim)
  665. (let ((attr (read (format "(%s)"
  666. (mapconcat
  667. #'identity
  668. (org-element-property :attr_man table)
  669. " ")))))
  670. (and attr (plist-get attr :verbatim))))
  671. (format ".nf\n\\fC%s\\fP\n.fi"
  672. ;; Re-create table, without affiliated keywords.
  673. (org-trim
  674. (org-element-interpret-data
  675. `(table nil ,@(org-element-contents table))))))
  676. ;; Case 2: Standard table.
  677. (t (org-man-table--org-table table contents info))))
  678. (defun org-man-table--align-string (divider table info)
  679. "Return an appropriate Man alignment string.
  680. TABLE is the considered table. INFO is a plist used as
  681. a communication channel."
  682. (let (alignment)
  683. ;; Extract column groups and alignment from first (non-rule) row.
  684. (org-element-map
  685. (org-element-map table 'table-row
  686. (lambda (row)
  687. (and (eq (org-element-property :type row) 'standard) row))
  688. info 'first-match)
  689. 'table-cell
  690. (lambda (cell)
  691. (let* ((borders (org-export-table-cell-borders cell info))
  692. (raw-width (org-export-table-cell-width cell info))
  693. (width-cm (when raw-width (/ raw-width 5)))
  694. (width (if raw-width (format "w(%dc)"
  695. (if (< width-cm 1) 1 width-cm)) "")))
  696. ;; Check left border for the first cell only.
  697. (when (and (memq 'left borders) (not alignment))
  698. (push "|" alignment))
  699. (push
  700. (concat (pcase (org-export-table-cell-alignment cell info)
  701. (`left "l") (`right "r") (`center "c"))
  702. width
  703. divider)
  704. alignment)
  705. (when (memq 'right borders) (push "|" alignment))))
  706. info)
  707. (apply #'concat (reverse alignment))))
  708. (defun org-man-table--org-table (table contents info)
  709. "Return appropriate Man code for an Org table.
  710. TABLE is the table type element to transcode. CONTENTS is its
  711. contents, as a string. INFO is a plist used as a communication
  712. channel.
  713. This function assumes TABLE has `org' as its `:type' attribute."
  714. (let* ((attr (org-export-read-attribute :attr_man table))
  715. (caption (and (not (plist-get attr :disable-caption))
  716. (org-man--caption/label-string table info)))
  717. (divider (if (plist-get attr :divider) "|" " "))
  718. ;; Determine alignment string.
  719. (alignment (org-man-table--align-string divider table info))
  720. ;; Extract others display options.
  721. (lines (org-split-string contents "\n"))
  722. (attr-list
  723. (delq nil
  724. (list
  725. (and (plist-get attr :expand) "expand")
  726. (let ((placement (plist-get attr :placement)))
  727. (cond ((string= placement 'center) "center")
  728. ((string= placement 'left) nil)
  729. ((plist-get info :man-tables-centered) "center")
  730. (t "")))
  731. (or (plist-get attr :boxtype) "box"))))
  732. (title-line (plist-get attr :title-line))
  733. (long-cells (plist-get attr :long-cells))
  734. (table-format (concat
  735. (format "%s" (or (car attr-list) "" ))
  736. (or
  737. (let ((output-list '()))
  738. (when (cdr attr-list)
  739. (dolist (attr-item (cdr attr-list))
  740. (setq output-list (concat output-list (format ",%s" attr-item)))))
  741. output-list)
  742. "")))
  743. (first-line (when lines (org-split-string (car lines) "\t"))))
  744. ;; Prepare the final format string for the table.
  745. (cond
  746. ;; Others.
  747. (lines (concat ".TS\n " table-format ";\n"
  748. (format "%s.\n"
  749. (let ((final-line ""))
  750. (when title-line
  751. (dotimes (_ (length first-line))
  752. (setq final-line (concat final-line "cb" divider))))
  753. (setq final-line (concat final-line "\n"))
  754. (if alignment
  755. (setq final-line (concat final-line alignment))
  756. (dotimes (_ (length first-line))
  757. (setq final-line (concat final-line "c" divider))))
  758. final-line ))
  759. (format "%s.TE\n"
  760. (let ((final-line "")
  761. (long-line "")
  762. (lines (org-split-string contents "\n")))
  763. (dolist (line-item lines)
  764. (setq long-line "")
  765. (if long-cells
  766. (progn
  767. (if (string= line-item "_")
  768. (setq long-line (format "%s\n" line-item))
  769. ;; else string =
  770. (let ((cell-item-list (org-split-string line-item "\t")))
  771. (dolist (cell-item cell-item-list)
  772. (cond ((eq cell-item (car (last cell-item-list)))
  773. (setq long-line (concat long-line
  774. (format "T{\n%s\nT}\t\n" cell-item ))))
  775. (t
  776. (setq long-line (concat long-line
  777. (format "T{\n%s\nT}\t" cell-item ))))))
  778. long-line))
  779. ;; else long cells
  780. (setq final-line (concat final-line long-line )))
  781. (setq final-line (concat final-line line-item "\n"))))
  782. final-line))
  783. (and caption (format ".TB \"%s\"" caption)))))))
  784. ;;; Table Cell
  785. (defun org-man-table-cell (table-cell contents info)
  786. "Transcode a TABLE-CELL element from Org to Man
  787. CONTENTS is the cell contents. INFO is a plist used as
  788. a communication channel."
  789. (concat
  790. (let ((scientific-format (plist-get info :man-table-scientific-notation)))
  791. (if (and contents
  792. scientific-format
  793. (string-match orgtbl-exp-regexp contents))
  794. ;; Use appropriate format string for scientific notation.
  795. (format scientific-format
  796. (match-string 1 contents)
  797. (match-string 2 contents))
  798. contents))
  799. (when (org-export-get-next-element table-cell info) "\t")))
  800. ;;; Table Row
  801. (defun org-man-table-row (table-row contents info)
  802. "Transcode a TABLE-ROW element from Org to Man.
  803. CONTENTS is the contents of the row. INFO is a plist used as
  804. a communication channel."
  805. ;; Rules are ignored since table separators are deduced from borders
  806. ;; of the current row.
  807. (when (eq (org-element-property :type table-row) 'standard)
  808. (let ((borders
  809. ;; TABLE-ROW's borders are extracted from its first cell.
  810. (org-export-table-cell-borders
  811. (car (org-element-contents table-row)) info)))
  812. (concat
  813. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  814. contents
  815. (cond ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  816. ((memq 'below borders) "\n_"))))))
  817. ;;; Target
  818. (defun org-man-target (target _contents info)
  819. "Transcode a TARGET object from Org to Man.
  820. CONTENTS is nil. INFO is a plist holding contextual
  821. information."
  822. (format "\\fI%s\\fP" (org-export-get-reference target info)))
  823. ;;; Timestamp
  824. (defun org-man-timestamp (_timestamp _contents _info)
  825. "Transcode a TIMESTAMP object from Org to Man.
  826. CONTENTS is nil. INFO is a plist holding contextual information."
  827. "")
  828. ;;; Underline
  829. (defun org-man-underline (_underline contents _info)
  830. "Transcode UNDERLINE from Org to Man.
  831. CONTENTS is the text with underline markup. INFO is a plist
  832. holding contextual information."
  833. (format "\\fI%s\\fP" contents))
  834. ;;; Verbatim
  835. (defun org-man-verbatim (verbatim _contents _info)
  836. "Transcode a VERBATIM object from Org to Man."
  837. (format "\\fI%s\\fP"
  838. (org-man--protect-text (org-element-property :value verbatim))))
  839. ;;; Verse Block
  840. (defun org-man-verse-block (_verse-block contents _info)
  841. "Transcode a VERSE-BLOCK element from Org to Man.
  842. CONTENTS is verse block contents. INFO is a plist holding
  843. contextual information."
  844. (format ".RS\n.ft I\n%s\n.ft\n.RE" contents))
  845. ;;; Interactive functions
  846. (defun org-man-export-to-man
  847. (&optional async subtreep visible-only body-only ext-plist)
  848. "Export current buffer to a Man file.
  849. If narrowing is active in the current buffer, only export its
  850. narrowed part.
  851. If a region is active, export that region.
  852. A non-nil optional argument ASYNC means the process should happen
  853. asynchronously. The resulting file should be accessible through
  854. the `org-export-stack' interface.
  855. When optional argument SUBTREEP is non-nil, export the sub-tree
  856. at point, extracting information from the headline properties
  857. first.
  858. When optional argument VISIBLE-ONLY is non-nil, don't export
  859. contents of hidden elements.
  860. When optional argument BODY-ONLY is non-nil, only the body
  861. without any markers.
  862. EXT-PLIST, when provided, is a property list with external
  863. parameters overriding Org default settings, but still inferior to
  864. file-local settings.
  865. Return output file's name."
  866. (interactive)
  867. (let ((outfile (org-export-output-file-name ".man" subtreep)))
  868. (org-export-to-file 'man outfile
  869. async subtreep visible-only body-only ext-plist)))
  870. (defun org-man-export-to-pdf
  871. (&optional async subtreep visible-only body-only ext-plist)
  872. "Export current buffer to Groff then process through to PDF.
  873. If narrowing is active in the current buffer, only export its
  874. narrowed part.
  875. If a region is active, export that region.
  876. A non-nil optional argument ASYNC means the process should happen
  877. asynchronously. The resulting file should be accessible through
  878. the `org-export-stack' interface.
  879. When optional argument SUBTREEP is non-nil, export the sub-tree
  880. at point, extracting information from the headline properties
  881. first.
  882. When optional argument VISIBLE-ONLY is non-nil, don't export
  883. contents of hidden elements.
  884. When optional argument BODY-ONLY is non-nil, only write between
  885. markers.
  886. EXT-PLIST, when provided, is a property list with external
  887. parameters overriding Org default settings, but still inferior to
  888. file-local settings.
  889. Return PDF file's name."
  890. (interactive)
  891. (let ((outfile (org-export-output-file-name ".man" subtreep)))
  892. (org-export-to-file 'man outfile
  893. async subtreep visible-only body-only ext-plist
  894. (lambda (file) (org-latex-compile file)))))
  895. (defun org-man-compile (file)
  896. "Compile a Groff file.
  897. FILE is the name of the file being compiled. Processing is done
  898. through the command specified in `org-man-pdf-process'.
  899. Return PDF file name or an error if it couldn't be produced."
  900. (message "Processing Groff file %s..." file)
  901. (let ((output (org-compile-file file org-man-pdf-process "pdf")))
  902. (when org-man-remove-logfiles
  903. (let ((base (file-name-sans-extension output)))
  904. (dolist (ext org-man-logfiles-extensions)
  905. (let ((file (concat base "." ext)))
  906. (when (file-exists-p file) (delete-file file))))))
  907. (message "Process completed.")
  908. output))
  909. (provide 'ox-man)
  910. ;;; ox-man.el ends here