org-e-man.el 46 KB

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