ox-man.el 41 KB

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