ox-man.el 41 KB

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