ox-man.el 38 KB

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