ox-man.el 42 KB

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