ox-groff.el 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. ;;; ox-groff.el --- Groff Back-End for Org Export Engine
  2. ;; Copyright (C) 2011-2014 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. ;; This file is not part of GNU Emacs.
  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 Groff Memorandum Macro back-end for Org
  20. ;; generic exporter.
  21. ;;
  22. ;; To test it, run
  23. ;;
  24. ;; M-: (org-export-to-buffer 'groff "*Test Groff*") RET
  25. ;;
  26. ;; in an org-mode buffer then switch to the buffer to see the Groff
  27. ;; export. See ox.el for more details on how this exporter works.
  28. ;;
  29. ;; It introduces two new buffer keywords: "GROFF_CLASS" and
  30. ;; "GROFF_CLASS_OPTIONS".
  31. ;;; Code:
  32. (eval-when-compile (require 'cl))
  33. (require 'ox)
  34. (defvar orgtbl-exp-regexp)
  35. ;;; Define Back-End
  36. (org-export-define-backend 'groff
  37. '((bold . org-groff-bold)
  38. (center-block . org-groff-center-block)
  39. (clock . org-groff-clock)
  40. (code . org-groff-code)
  41. (comment . (lambda (&rest args) ""))
  42. (comment-block . (lambda (&rest args) ""))
  43. (drawer . org-groff-drawer)
  44. (dynamic-block . org-groff-dynamic-block)
  45. (entity . org-groff-entity)
  46. (example-block . org-groff-example-block)
  47. (export-snippet . org-groff-export-snippet)
  48. (fixed-width . org-groff-fixed-width)
  49. (footnote-definition . org-groff-footnote-definition)
  50. (footnote-reference . org-groff-footnote-reference)
  51. (headline . org-groff-headline)
  52. (horizontal-rule . org-groff-horizontal-rule)
  53. (inline-src-block . org-groff-inline-src-block)
  54. (inlinetask . org-groff-inlinetask)
  55. (italic . org-groff-italic)
  56. (item . org-groff-item)
  57. (keyword . org-groff-keyword)
  58. (line-break . org-groff-line-break)
  59. (link . org-groff-link)
  60. (node-property . org-groff-node-property)
  61. (paragraph . org-groff-paragraph)
  62. (plain-list . org-groff-plain-list)
  63. (plain-text . org-groff-plain-text)
  64. (planning . org-groff-planning)
  65. (property-drawer . org-groff-property-drawer)
  66. (quote-block . org-groff-quote-block)
  67. (radio-target . org-groff-radio-target)
  68. (section . org-groff-section)
  69. (special-block . org-groff-special-block)
  70. (src-block . org-groff-src-block)
  71. (statistics-cookie . org-groff-statistics-cookie)
  72. (strike-through . org-groff-strike-through)
  73. (subscript . org-groff-subscript)
  74. (superscript . org-groff-superscript)
  75. (table . org-groff-table)
  76. (table-cell . org-groff-table-cell)
  77. (table-row . org-groff-table-row)
  78. (target . org-groff-target)
  79. (template . org-groff-template)
  80. (timestamp . org-groff-timestamp)
  81. (underline . org-groff-underline)
  82. (verbatim . org-groff-verbatim)
  83. (verse-block . org-groff-verse-block))
  84. :export-block "GROFF"
  85. :menu-entry
  86. '(?g "Export to GROFF"
  87. ((?g "As GROFF file" org-groff-export-to-groff)
  88. (?p "As PDF file" org-groff-export-to-pdf)
  89. (?o "As PDF file and open"
  90. (lambda (a s v b)
  91. (if a (org-groff-export-to-pdf t s v b)
  92. (org-open-file (org-groff-export-to-pdf nil s v b)))))))
  93. :options-alist
  94. '((:groff-class "GROFF_CLASS" nil org-groff-default-class t)
  95. (:groff-class-options "GROFF_CLASS_OPTIONS" nil nil t)
  96. (:groff-header-extra "GROFF_HEADER" nil nil newline)))
  97. ;;; User Configurable Variables
  98. (defgroup org-export-groff nil
  99. "Options for exporting Org mode files to Groff."
  100. :tag "Org Export Groff"
  101. :group 'org-export)
  102. ;;; Preamble
  103. (defcustom org-groff-default-class "internal"
  104. "The default Groff class."
  105. :group 'org-export-groff
  106. :type '(string :tag "Groff class"))
  107. (defcustom org-groff-classes
  108. '(("file" ".MT 1"
  109. (:heading 'default :type "memo" :last-section "toc"))
  110. ("internal" ".MT 0"
  111. (:heading 'default :type "memo" :last-section "toc"))
  112. ("programmer" ".MT 2"
  113. (:heading 'default :type "memo" :last-section "toc"))
  114. ("engineer" ".MT 3"
  115. (:heading 'default :type "memo" :last-section "toc"))
  116. ("external" ".MT 4"
  117. (:heading 'default :type "memo" :last-section "toc"))
  118. ("letter" ".MT 5"
  119. (:heading 'default :type "memo" :last-section "sign"))
  120. ("custom" ".so file"
  121. (:heading custom-function :type "custom" :last-section "toc"))
  122. ("dummy" ""
  123. (:heading 'default :type "memo"))
  124. ("ms" "ms"
  125. (:heading 'default :type "cover" :last-section "toc"))
  126. ("se_ms" "se_ms"
  127. (:heading 'default :type "cover" :last-section "toc"))
  128. ("block" "BL"
  129. (:heading 'default :type "letter" :last-section "sign"))
  130. ("semiblock" "SB"
  131. (:heading 'default :type "letter" :last-section "sign"))
  132. ("fullblock" "FB"
  133. (:heading 'default :type "letter" :last-section "sign"))
  134. ("simplified" "SP"
  135. (:heading 'default :type "letter" :last-section "sign"))
  136. ("none" "" (:heading 'default :type "custom")))
  137. ;; none means, no Cover or Memorandum Type and no calls to AU, AT, ND and TL
  138. ;; This is to facilitate the creation of custom pages.
  139. ;; dummy means, no Cover or Memorandum Type but calls to AU, AT, ND and TL
  140. ;; are made. This is to facilitate Abstract Insertion.
  141. "This list describes the attributes for the documents being created.
  142. It allows for the creation of new "
  143. :group 'org-export-groff
  144. :type '(repeat
  145. (list (string :tag "Document Type")
  146. (string :tag "Header")
  147. (repeat :tag "Options" :inline t
  148. (choice
  149. (list :tag "Heading")
  150. (function :tag "Hook computing sectioning"))))))
  151. ;;; Headline
  152. (defconst org-groff-special-tags
  153. '("FROM" "TO" "ABSTRACT" "APPENDIX" "BODY" "NS"))
  154. (defcustom org-groff-format-headline-function nil
  155. "Function to format headline text.
  156. This function will be called with 5 arguments:
  157. TODO the todo keyword (string or nil).
  158. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  159. PRIORITY the priority of the headline (integer or nil)
  160. TEXT the main headline text (string).
  161. TAGS the tags as a list of strings (list of strings or nil).
  162. The function result will be used in the section format string.
  163. As an example, one could set the variable to the following, in
  164. order to reproduce the default set-up:
  165. \(defun org-groff-format-headline (todo todo-type priority text tags)
  166. \"Default format function for a headline.\"
  167. \(concat (when todo
  168. \(format \"\\fB%s\\fP \" todo))
  169. \(when priority
  170. \(format \"[\\#%c] \" priority))
  171. text
  172. \(when tags
  173. \(format \" %s \"
  174. \(mapconcat 'identity tags \":\"))))"
  175. :group 'org-export-groff
  176. :type 'function)
  177. ;;; Timestamps
  178. (defcustom org-groff-active-timestamp-format "\\fI%s\\fP"
  179. "A printf format string to be applied to active timestamps."
  180. :group 'org-export-groff
  181. :type 'string)
  182. (defcustom org-groff-inactive-timestamp-format "\\fI%s\\fP"
  183. "A printf format string to be applied to inactive timestamps."
  184. :group 'org-export-groff
  185. :type 'string)
  186. (defcustom org-groff-diary-timestamp-format "\\fI%s\\fP"
  187. "A printf format string to be applied to diary timestamps."
  188. :group 'org-export-groff
  189. :type 'string)
  190. ;;; Links
  191. (defcustom org-groff-inline-image-rules
  192. '(("file" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'")
  193. ("fuzzy" . "\\.\\(jpg\\|png\\|pdf\\|ps\\|eps\\|pic\\)\\'"))
  194. "Rules characterizing image files that can be inlined into Groff.
  195. A rule consists in an association whose key is the type of link
  196. to consider, and value is a regexp that will be matched against
  197. link's path.
  198. Note that, by default, the image extensions actually allowed
  199. depend on the way the Groff file is processed. When used with
  200. pdfgroff, pdf, jpg and png images are OK. When processing
  201. through dvi to Postscript, only ps and eps are allowed. The
  202. default we use here encompasses both."
  203. :group 'org-export-groff
  204. :type '(alist :key-type (string :tag "Type")
  205. :value-type (regexp :tag "Path")))
  206. (defcustom org-groff-link-with-unknown-path-format "\\fI%s\\fP"
  207. "Format string for links with unknown path type."
  208. :group 'org-export-groff
  209. :type 'string)
  210. ;;; Tables
  211. (defcustom org-groff-tables-centered t
  212. "When non-nil, tables are exported in a center environment."
  213. :group 'org-export-groff
  214. :type 'boolean)
  215. (defcustom org-groff-tables-verbatim nil
  216. "When non-nil, tables are exported verbatim."
  217. :group 'org-export-groff
  218. :type 'boolean)
  219. (defcustom org-groff-table-scientific-notation "%sE%s"
  220. "Format string to display numbers in scientific notation.
  221. The format should have \"%s\" twice, for mantissa and exponent
  222. \(i.e. \"%s\\\\times10^{%s}\").
  223. When nil, no transformation is made."
  224. :group 'org-export-groff
  225. :type '(choice
  226. (string :tag "Format string")
  227. (const :tag "No formatting")))
  228. ;;; Text markup
  229. (defcustom org-groff-text-markup-alist
  230. '((bold . "\\fB%s\\fP")
  231. (code . "\\fC%s\\fP")
  232. (italic . "\\fI%s\\fP")
  233. (strike-through . "\\fC%s\\fP") ; Strike through and underline
  234. (underline . "\\fI%s\\fP") ; need to be revised.
  235. (verbatim . "protectedtexttt"))
  236. "Alist of Groff expressions to convert text markup.
  237. The key must be a symbol among `bold', `code', `italic',
  238. `strike-through', `underline' and `verbatim'. The value is
  239. a formatting string to wrap fontified text with it.
  240. If no association can be found for a given markup, text will be
  241. returned as-is."
  242. :group 'org-export-groff
  243. :type 'alist
  244. :options '(bold code italic strike-through underline verbatim))
  245. ;;; Drawers
  246. (defcustom org-groff-format-drawer-function nil
  247. "Function called to format a drawer in Groff code.
  248. The function must accept two parameters:
  249. NAME the drawer name, like \"LOGBOOK\"
  250. CONTENTS the contents of the drawer.
  251. The function should return the string to be exported.
  252. For example, the variable could be set to the following function
  253. in order to mimic default behaviour:
  254. \(defun org-groff-format-drawer-default \(name contents\)
  255. \"Format a drawer element for Groff export.\"
  256. contents\)"
  257. :group 'org-export-groff
  258. :type 'function)
  259. ;;; Inlinetasks
  260. (defcustom org-groff-format-inlinetask-function nil
  261. "Function called to format an inlinetask in Groff code.
  262. The function must accept six parameters:
  263. TODO the todo keyword, as a string
  264. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  265. PRIORITY the inlinetask priority, as a string
  266. NAME the inlinetask name, as a string.
  267. TAGS the inlinetask tags, as a list of strings.
  268. CONTENTS the contents of the inlinetask, as a string.
  269. The function should return the string to be exported.
  270. For example, the variable could be set to the following function
  271. in order to mimic default behaviour:
  272. \(defun org-groff-format-inlinetask \(todo type priority name tags contents\)
  273. \"Format an inline task element for Groff export.\"
  274. \(let ((full-title
  275. \(concat
  276. \(when todo
  277. \(format \"\\fB%s\\fP \" todo))
  278. \(when priority (format \"[\\#%c] \" priority))
  279. title
  280. \(when tags
  281. \(format \":%s:\"
  282. \(mapconcat 'identity tags \":\")))))
  283. \(format (concat \".DS L\\n\"
  284. \"%s\\n\\n\"
  285. \"%s\"
  286. \".DE\")
  287. full-title contents))"
  288. :group 'org-export-groff
  289. :type 'function)
  290. ;; Src blocks
  291. (defcustom org-groff-source-highlight nil
  292. "Use GNU source highlight to embellish source blocks "
  293. :group 'org-export-groff
  294. :type 'boolean)
  295. (defcustom org-groff-source-highlight-langs
  296. '((emacs-lisp "lisp") (lisp "lisp") (clojure "lisp")
  297. (scheme "scheme")
  298. (c "c") (cc "cpp") (csharp "csharp") (d "d")
  299. (fortran "fortran") (cobol "cobol") (pascal "pascal")
  300. (ada "ada") (asm "asm")
  301. (perl "perl") (cperl "perl")
  302. (python "python") (ruby "ruby") (tcl "tcl") (lua "lua")
  303. (java "java") (javascript "javascript")
  304. (tex "latex")
  305. (shell-script "sh") (awk "awk") (diff "diff") (m4 "m4")
  306. (ocaml "caml") (caml "caml")
  307. (sql "sql") (sqlite "sql")
  308. (html "html") (css "css") (xml "xml")
  309. (bat "bat") (bison "bison") (clipper "clipper")
  310. (ldap "ldap") (opa "opa")
  311. (php "php") (postscript "postscript") (prolog "prolog")
  312. (properties "properties") (makefile "makefile")
  313. (tml "tml") (vala "vala") (vbscript "vbscript") (xorg "xorg"))
  314. "Alist mapping languages to their listing language counterpart.
  315. The key is a symbol, the major mode symbol without the \"-mode\".
  316. The value is the string that should be inserted as the language
  317. parameter for the listings package. If the mode name and the
  318. listings name are the same, the language does not need an entry
  319. in this list - but it does not hurt if it is present."
  320. :group 'org-export-groff
  321. :type '(repeat
  322. (list
  323. (symbol :tag "Major mode ")
  324. (string :tag "Listings language"))))
  325. (defcustom org-groff-source-highlight-options nil
  326. "Association list of options for the groff listings package.
  327. These options are supplied as a comma-separated list to the
  328. \\lstset command. Each element of the association list should be
  329. a list containing two strings: the name of the option, and the
  330. value. For example,
  331. (setq org-groff-source-highlight-options
  332. '((\"basicstyle\" \"\\small\")
  333. (\"keywordstyle\" \"\\color{black}\\bfseries\\underbar\")))
  334. will typeset the code in a small size font with underlined, bold
  335. black keywords.
  336. Note that the same options will be applied to blocks of all
  337. languages."
  338. :group 'org-export-groff
  339. :type '(repeat
  340. (list
  341. (string :tag "Listings option name ")
  342. (string :tag "Listings option value"))))
  343. (defvar org-groff-custom-lang-environments nil
  344. "Alist mapping languages to language-specific Groff environments.
  345. It is used during export of src blocks by the listings and
  346. groff packages. For example,
  347. \(setq org-groff-custom-lang-environments
  348. '\(\(python \"pythoncode\"\)\)\)
  349. would have the effect that if org encounters begin_src python
  350. during groff export it will use pythoncode as the source-highlight
  351. language.")
  352. ;;; Plain text
  353. (defcustom org-groff-special-char
  354. '(("(c)" . "\\\\(co")
  355. ("(tm)" . "\\\\(tm")
  356. ("(rg)" . "\\\\(rg"))
  357. "CONS list in which the value of the car
  358. is replace on the value of the CDR. "
  359. :group 'org-export-groff
  360. :type '(list
  361. (cons :tag "Character Subtitute"
  362. (string :tag "Original Character Group")
  363. (string :tag "Replacement Character"))))
  364. ;;; Compilation
  365. (defcustom org-groff-pdf-process
  366. '("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  367. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  368. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf")
  369. "Commands to process a Groff file to a PDF file.
  370. This is a list of strings, each of them will be given to the
  371. shell as a command. %f in the command will be replaced by the
  372. full file name, %b by the file base name \(i.e. without
  373. extension) and %o by the base directory of the file."
  374. :group 'org-export-pdf
  375. :type '(choice
  376. (repeat :tag "Shell command sequence"
  377. (string :tag "Shell command"))
  378. (const :tag "2 runs of pdfgroff"
  379. ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  380. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
  381. (const :tag "3 runs of pdfgroff"
  382. ("pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  383. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"
  384. "pic %f | tbl | eqn | groff -mm | ps2pdf - > %b.pdf"))
  385. (function)))
  386. (defcustom org-groff-logfiles-extensions
  387. '("aux" "idx" "log" "out" "toc" "nav" "snm" "vrb")
  388. "The list of file extensions to consider as Groff logfiles."
  389. :group 'org-export-groff
  390. :type '(repeat (string :tag "Extension")))
  391. (defcustom org-groff-remove-logfiles t
  392. "Non-nil means remove the logfiles produced by PDF production.
  393. These are the .aux, .log, .out, and .toc files."
  394. :group 'org-export-groff
  395. :type 'boolean)
  396. (defcustom org-groff-organization "Org User"
  397. "Name of the organization used to populate the .AF command."
  398. :group 'org-export-groff
  399. :type 'string)
  400. (defcustom org-groff-raster-to-ps nil
  401. "Command used to convert raster to EPS. Nil for no conversion. Make sure that
  402. `org-groff-inline-image-rules' is adjusted accordingly if not conversion is being
  403. done. In this case, remove the entries for jpg and png in the file and fuzzy lists."
  404. :group 'org-export-groff
  405. :type '(choice
  406. (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
  407. (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b} ;grep -v BeginData ${b} > b_${b};mv b_${b} ${b}" )
  408. (const :tag "NetPNM" "a=%s;b=%s;pngtopnm ${a} | pnmtops -noturn > ${b}" )
  409. (const :tag "None" nil)))
  410. (defvar org-groff-registered-references nil)
  411. (defvar org-groff-special-content nil)
  412. ;;; Internal Functions
  413. (defun org-groff--caption/label-string (element info)
  414. "Return caption and label Groff string for ELEMENT.
  415. INFO is a plist holding contextual information. If there's no
  416. caption nor label, return the empty string.
  417. For non-floats, see `org-groff--wrap-label'."
  418. (let ((main (org-export-get-caption element))
  419. (short (org-export-get-caption element t))
  420. (label (org-element-property :name element)))
  421. (cond ((and (not main) (not label)) "")
  422. ((not main) (format "\\fI%s\\fP" label))
  423. ;; Option caption format with short name.
  424. (short (format "%s\n.br\n - %s\n"
  425. (org-export-data short info)
  426. (org-export-data main info)))
  427. ;; Standard caption format.
  428. (t (format "\\fR%s\\fP" (org-export-data main info))))))
  429. (defun org-groff--wrap-label (element output)
  430. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  431. This function shouldn't be used for floats. See
  432. `org-groff--caption/label-string'."
  433. (let ((label (org-element-property :name element)))
  434. (if (or (not output) (not label) (string= output "") (string= label ""))
  435. output
  436. (concat (format "%s\n.br\n" label) output))))
  437. (defun org-groff--text-markup (text markup)
  438. "Format TEXT depending on MARKUP text markup.
  439. See `org-groff-text-markup-alist' for details."
  440. (let ((fmt (cdr (assq markup org-groff-text-markup-alist))))
  441. (cond
  442. ;; No format string: Return raw text.
  443. ((not fmt) text)
  444. ((string= "protectedtexttt" fmt)
  445. (let ((start 0)
  446. (trans '(("\\" . "\\")))
  447. (rtn "")
  448. char)
  449. (while (string-match "[\\{}$%&_#~^]" text)
  450. (setq char (match-string 0 text))
  451. (if (> (match-beginning 0) 0)
  452. (setq rtn (concat rtn (substring text 0 (match-beginning 0)))))
  453. (setq text (substring text (1+ (match-beginning 0))))
  454. (setq char (or (cdr (assoc char trans)) (concat "\\" char))
  455. rtn (concat rtn char)))
  456. (setq text (concat rtn text))
  457. (format "\\fC%s\\fP" text)))
  458. ;; Else use format string.
  459. (t (format fmt text)))))
  460. (defun org-groff--get-tagged-content (tag info)
  461. (cdr (assoc tag org-groff-special-content)))
  462. (defun org-groff--mt-head (title contents attr info)
  463. (concat
  464. ;; 1. Insert Organization
  465. (let ((firm-option (plist-get attr :firm)))
  466. (cond
  467. ((stringp firm-option)
  468. (format ".AF \"%s\" \n" firm-option))
  469. (t (format ".AF \"%s\" \n" (or org-groff-organization "")))))
  470. ;; 2. Title
  471. (let ((subtitle1 (plist-get attr :subtitle1))
  472. (subtitle2 (plist-get attr :subtitle2)))
  473. (cond
  474. ((string= "" title)
  475. (format ".TL \"%s\" \"%s\" \n%s\n"
  476. (or subtitle1 "")
  477. (or subtitle2 "") " "))
  478. ((not (or subtitle1 subtitle2))
  479. (format ".TL\n%s\n"
  480. (or title "")))
  481. (t
  482. (format ".TL \"%s\" \"%s \" \n%s\n"
  483. (or subtitle1 "")
  484. (or subtitle2 "") title))))
  485. ;; 3. Author.
  486. ;; In Groff, .AU *MUST* be placed after .TL
  487. ;; If From, populate with data from From else
  488. ;;
  489. (let ((author (and (plist-get info :with-author)
  490. (let ((auth (plist-get info :author)))
  491. (and auth (org-export-data auth info)))))
  492. (email (and (plist-get info :with-email)
  493. (org-export-data (plist-get info :email) info)))
  494. (from-data (org-groff--get-tagged-content "FROM" info))
  495. (to-data (org-groff--get-tagged-content "TO" info)))
  496. (cond
  497. ((and author from-data)
  498. (let ((au-line
  499. (mapconcat
  500. (lambda (from-line)
  501. (format " \"%s\" " from-line))
  502. (split-string
  503. (setq from-data
  504. (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
  505. (concat
  506. (format ".AU \"%s\" " author) au-line "\n")))
  507. ((and author email (not (string= "" email)))
  508. (format ".AU \"%s\" \"%s\"\n" author email))
  509. (author (format ".AU \"%s\"\n" author))
  510. (t ".AU \"\" \n")))
  511. ;; 4. Author Title, if present
  512. (let ((at-item (plist-get attr :author-title)))
  513. (if (and at-item (stringp at-item))
  514. (format ".AT \"%s\" \n" at-item)
  515. ""))
  516. ;; 5. Date.
  517. (when (plist-get info :with-date)
  518. (let ((date (org-export-data (org-export-get-date info) info)))
  519. (and (org-string-nw-p date) (format ".ND \"%s\"\n" date))))
  520. ;;
  521. ;; If Abstract, then Populate Abstract
  522. ;;
  523. (let ((abstract-data (org-groff--get-tagged-content "ABSTRACT" info))
  524. (to-data (org-groff--get-tagged-content "TO" info)))
  525. (cond
  526. (abstract-data
  527. (format ".AS\n%s\n.AE\n" abstract-data))
  528. (to-data
  529. (format ".AS\n%s\n.AE\n" to-data))))))
  530. (defun org-groff--letter-head (title contents attr info)
  531. (let ((author (and (plist-get info :with-author)
  532. (let ((auth (plist-get info :author)))
  533. (and auth (org-export-data auth info)))))
  534. (email (and (plist-get info :with-email)
  535. (org-export-data (plist-get info :email) info)))
  536. (from-data (org-groff--get-tagged-content "FROM" info))
  537. (at-item (plist-get attr :author-title))
  538. (to-data (org-groff--get-tagged-content "TO" info)))
  539. ;; If FROM then get data from FROM
  540. (if from-data
  541. (setq from-data
  542. (replace-regexp-in-string "\\.P\n" "" from-data))
  543. (setq from-data ""))
  544. (if to-data
  545. (setq to-data
  546. (replace-regexp-in-string "\\.P\n" "" to-data))
  547. (setq from-data ""))
  548. (concat
  549. (cond
  550. (from-data
  551. (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
  552. ((and author email (not (string= "" email)))
  553. (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
  554. (author (format ".WA \"%s\"\n.WE\n" author))
  555. (t ".WA \"\" \n.WE\n"))
  556. ;; If TO then get data from TO
  557. (when to-data
  558. (format ".IA \n%s\n.IE\n" to-data)))))
  559. ;;; Template
  560. (defun org-groff-template (contents info)
  561. "Return complete document string after Groff conversion.
  562. CONTENTS is the transcoded contents string. INFO is a plist
  563. holding export options."
  564. (let* ((title (org-export-data (plist-get info :title) info))
  565. (attr (read
  566. (format "(%s)"
  567. (mapconcat
  568. #'identity
  569. (list (plist-get info :groff-class-options))
  570. " "))))
  571. (class (plist-get info :groff-class))
  572. (class-options (plist-get info :groff-class-options))
  573. (classes (assoc class org-groff-classes))
  574. (classes-options (car (last classes)))
  575. (heading-option (plist-get classes-options :heading))
  576. (type-option (plist-get classes-options :type))
  577. (last-option (plist-get classes-options :last-section))
  578. (hyphenate (plist-get attr :hyphenate))
  579. (justify-right (plist-get attr :justify-right))
  580. (document-class-string
  581. (progn
  582. (org-element-normalize-string
  583. (let* ((header (nth 1 (assoc class org-groff-classes)))
  584. (document-class-item (if (stringp header) header "")))
  585. document-class-item)))))
  586. (concat
  587. (if justify-right
  588. (case justify-right
  589. ('yes ".SA 1 \n")
  590. ('no ".SA 0 \n")
  591. (t ""))
  592. "")
  593. (if hyphenate
  594. (case hyphenate
  595. ('yes ".nr Hy 1 \n")
  596. ('no ".nr Hy 0 \n")
  597. (t ""))
  598. "")
  599. (cond
  600. ((string= type-option "custom") "")
  601. ((and (stringp document-class-string)
  602. (string= type-option "cover"))
  603. (concat
  604. (format ".COVER %s\n" document-class-string)
  605. (org-groff--mt-head title contents attr info)
  606. ".COVEND\n"))
  607. ((string= type-option "memo")
  608. (concat
  609. (org-groff--mt-head title contents attr info)
  610. document-class-string))
  611. ((string= type-option "letter")
  612. (concat
  613. (org-groff--letter-head title contents attr info)
  614. (let ((sa-item (plist-get attr :salutation))
  615. (cn-item (plist-get attr :confidential))
  616. (sj-item (plist-get attr :subject))
  617. (rn-item (plist-get attr :reference))
  618. (at-item (plist-get attr :attention)))
  619. (concat
  620. (if (stringp sa-item)
  621. (format ".LO SA \"%s\" \n" sa-item)
  622. ".LO SA\n")
  623. (when cn-item
  624. (if (stringp cn-item)
  625. (format ".LO CN \"%s\"\n" cn-item)
  626. ".LO CN\n"))
  627. (when (and at-item (stringp at-item))
  628. (format ".LO AT \"%s\" \n" at-item))
  629. (when (and title rn-item)
  630. (format ".LO RN \"%s\"\n" title))
  631. (when (and sj-item (stringp sj-item))
  632. (format ".LO SJ \"%s\" \n" sj-item))
  633. ".LT " document-class-string "\n"))))
  634. (t ""))
  635. contents
  636. (cond
  637. ((string= last-option "toc")
  638. ".TC")
  639. ((string= last-option "sign")
  640. (let ((fc-item (plist-get attr :closing)))
  641. (concat (if (stringp fc-item)
  642. (format ".FC \"%s\" \n" fc-item)
  643. ".FC\n")
  644. ".SG\n")))
  645. (t ""))
  646. (progn
  647. (mapconcat
  648. (lambda (item)
  649. (when (string= (car item) "NS")
  650. (replace-regexp-in-string
  651. "\\.P\n" "" (cdr item))))
  652. (reverse org-groff-special-content) "\n")))))
  653. ;;; Transcode Functions
  654. ;;; Babel Call
  655. ;;
  656. ;; Babel Calls are ignored.
  657. ;;; Bold
  658. (defun org-groff-bold (bold contents info)
  659. "Transcode BOLD from Org to Groff.
  660. CONTENTS is the text with bold markup. INFO is a plist holding
  661. contextual information."
  662. (org-groff--text-markup contents 'bold))
  663. ;;; Center Block
  664. (defun org-groff-center-block (center-block contents info)
  665. "Transcode a CENTER-BLOCK element from Org to Groff.
  666. CONTENTS holds the contents of the center block. INFO is a plist
  667. holding contextual information."
  668. (org-groff--wrap-label
  669. center-block
  670. (format ".DS C \n%s\n.DE" contents)))
  671. ;;; Clock
  672. (defun org-groff-clock (clock contents info)
  673. "Transcode a CLOCK element from Org to Groff.
  674. CONTENTS is nil. INFO is a plist holding contextual
  675. information."
  676. (concat
  677. (format "\\fB%s\\fP " org-clock-string)
  678. (format org-groff-inactive-timestamp-format
  679. (concat (org-translate-time
  680. (org-element-property :raw-value
  681. (org-element-property :value clock)))
  682. (let ((time (org-element-property :duration clock)))
  683. (and time (format " (%s)" time)))))))
  684. ;;; Code
  685. (defun org-groff-code (code contents info)
  686. "Transcode a CODE object from Org to Groff.
  687. CONTENTS is nil. INFO is a plist used as a communication
  688. channel."
  689. (org-groff--text-markup (org-element-property :value code) 'code))
  690. ;;; Comments and Comment Blocks are ignored.
  691. ;;; Drawer
  692. (defun org-groff-drawer (drawer contents info)
  693. "Transcode a DRAWER element from Org to Groff.
  694. CONTENTS holds the contents of the block. INFO is a plist
  695. holding contextual information."
  696. (let* ((name (org-element-property :drawer-name drawer))
  697. (output (if (functionp org-groff-format-drawer-function)
  698. (funcall org-groff-format-drawer-function
  699. name contents)
  700. ;; If there's no user defined function: simply
  701. ;; display contents of the drawer.
  702. contents)))
  703. (org-groff--wrap-label drawer output)))
  704. ;;; Dynamic Block
  705. (defun org-groff-dynamic-block (dynamic-block contents info)
  706. "Transcode a DYNAMIC-BLOCK element from Org to Groff.
  707. CONTENTS holds the contents of the block. INFO is a plist
  708. holding contextual information. See `org-export-data'."
  709. (org-groff--wrap-label dynamic-block contents))
  710. ;;; Entity
  711. (defun org-groff-entity (entity contents info)
  712. "Transcode an ENTITY object from Org to Groff.
  713. CONTENTS are the definition itself. INFO is a plist holding
  714. contextual information."
  715. (org-element-property :utf-8 entity))
  716. ;;; Example Block
  717. (defun org-groff-example-block (example-block contents info)
  718. "Transcode an EXAMPLE-BLOCK element from Org to Groff.
  719. CONTENTS is nil. INFO is a plist holding contextual
  720. information."
  721. (org-groff--wrap-label
  722. example-block
  723. (format ".DS L\n%s\n.DE"
  724. (org-export-format-code-default example-block info))))
  725. ;;; Export Snippet
  726. (defun org-groff-export-snippet (export-snippet contents info)
  727. "Transcode a EXPORT-SNIPPET object from Org to Groff.
  728. CONTENTS is nil. INFO is a plist holding contextual information."
  729. (when (eq (org-export-snippet-backend export-snippet) 'groff)
  730. (org-element-property :value export-snippet)))
  731. ;;; Fixed Width
  732. (defun org-groff-fixed-width (fixed-width contents info)
  733. "Transcode a FIXED-WIDTH element from Org to Groff.
  734. CONTENTS is nil. INFO is a plist holding contextual information."
  735. (org-groff--wrap-label
  736. fixed-width
  737. (format "\\fC\n%s\\fP"
  738. (org-remove-indentation
  739. (org-element-property :value fixed-width)))))
  740. ;;; Footnote Definition
  741. ;;
  742. ;; Footnote Definitions are ignored.
  743. ;;
  744. ;; Footnotes are handled automatically in GROFF. Although manual
  745. ;; references can be added, not really required.
  746. (defun org-groff-footnote-reference (footnote-reference contents info)
  747. ;; Changing from info to footnote-reference
  748. (let* ((raw (org-export-get-footnote-definition footnote-reference info))
  749. (n (org-export-get-footnote-number footnote-reference info))
  750. (data (org-trim (org-export-data raw info)))
  751. (ref-id (plist-get (nth 1 footnote-reference) :label)))
  752. ;; It is a reference
  753. (if (string-match "fn:rl" ref-id)
  754. (if (member ref-id org-groff-registered-references)
  755. (format "\\*[%s]" ref-id)
  756. (progn
  757. (push ref-id org-groff-registered-references)
  758. (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
  759. ;; else it is a footnote
  760. (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
  761. ;;; Headline
  762. (defun org-groff-headline (headline contents info)
  763. "Transcode a HEADLINE element from Org to Groff.
  764. CONTENTS holds the contents of the headline. INFO is a plist
  765. holding contextual information."
  766. (let* ((class (plist-get info :groff-class))
  767. (level (org-export-get-relative-level headline info))
  768. (numberedp (org-export-numbered-headline-p headline info))
  769. ;; Section formatting will set two placeholders: one for the
  770. ;; title and the other for the contents.
  771. (classes (assoc class org-groff-classes))
  772. (classes-options (car (last classes)))
  773. (heading-option (plist-get classes-options :heading))
  774. (section-fmt
  775. (progn
  776. (cond
  777. ((and (symbolp heading-option)
  778. (fboundp heading-option))
  779. (funcall heading-option level numberedp))
  780. ((> level 7) nil)
  781. (t (if numberedp
  782. (concat ".H " (number-to-string level) " \"%s\"\n%s")
  783. ".HU \"%s\"\n%s")))))
  784. ;; End of section-fmt
  785. (text (org-export-data (org-element-property :title headline) info))
  786. (todo
  787. (and (plist-get info :with-todo-keywords)
  788. (let ((todo (org-element-property :todo-keyword headline)))
  789. (and todo (org-export-data todo info)))))
  790. (todo-type (and todo (org-element-property :todo-type headline)))
  791. (tags (and (plist-get info :with-tags)
  792. (org-export-get-tags headline info)))
  793. (priority (and (plist-get info :with-priority)
  794. (org-element-property :priority headline)))
  795. ;; Create the headline text along with a no-tag version. The
  796. ;; latter is required to remove tags from table of contents.
  797. (full-text (if (functionp org-groff-format-headline-function)
  798. ;; User-defined formatting function.
  799. (funcall org-groff-format-headline-function
  800. todo todo-type priority text tags)
  801. ;; Default formatting.
  802. (concat
  803. (when todo
  804. (format "\\fB%s\\fP " todo))
  805. (when priority (format " [\\#%c] " priority))
  806. text
  807. (when tags
  808. (format " \\fC:%s:\\fP "
  809. (mapconcat 'identity tags ":"))))))
  810. (full-text-no-tag
  811. (if (functionp org-groff-format-headline-function)
  812. ;; User-defined formatting function.
  813. (funcall org-groff-format-headline-function
  814. todo todo-type priority text nil)
  815. ;; Default formatting.
  816. (concat
  817. (when todo (format "\\fB%s\\fP " todo))
  818. (when priority (format " [\\#%c] " priority))
  819. text)))
  820. ;; Associate some \label to the headline for internal links.
  821. ;; (headline-label
  822. ;; (format "\\label{sec-%s}\n"
  823. ;; (mapconcat 'number-to-string
  824. ;; (org-export-get-headline-number headline info)
  825. ;; "-")))
  826. (headline-label "")
  827. (pre-blanks
  828. (make-string (org-element-property :pre-blank headline) 10)))
  829. (cond
  830. ;; Case 1: Special Tag
  831. ((member (car tags) org-groff-special-tags)
  832. (cond
  833. ((string= (car tags) "BODY") contents)
  834. ((string= (car tags) "NS")
  835. (progn
  836. (push (cons (car tags)
  837. (format ".NS \"%s\" 1 \n%s"
  838. (car (org-element-property :title headline))
  839. (or contents " ")))
  840. org-groff-special-content) nil))
  841. (t
  842. (progn
  843. (push (cons (car tags) contents) org-groff-special-content)
  844. nil))))
  845. ;; Case 2: This is a footnote section: ignore it.
  846. ((org-element-property :footnote-section-p headline) nil)
  847. ;; Case 3: This is a deep sub-tree: export it as a list item.
  848. ;; Also export as items headlines for which no section
  849. ;; format has been found.
  850. ((or (not section-fmt) (org-export-low-level-p headline info))
  851. ;; Build the real contents of the sub-tree.
  852. (let ((low-level-body
  853. (concat
  854. ;; If the headline is the first sibling, start a list.
  855. (when (org-export-first-sibling-p headline info)
  856. (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
  857. ;; Itemize headline
  858. ".LI\n" full-text "\n" headline-label pre-blanks contents)))
  859. ;; If headline is not the last sibling simply return
  860. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  861. ;; blank line.
  862. (if (not (org-export-last-sibling-p headline info)) low-level-body
  863. (replace-regexp-in-string
  864. "[ \t\n]*\\'"
  865. (concat "\n.LE")
  866. low-level-body))))
  867. ;; Case 4. Standard headline. Export it as a section.
  868. (t
  869. (format section-fmt full-text
  870. (concat headline-label pre-blanks contents))))))
  871. ;;; Horizontal Rule
  872. ;; Not supported
  873. ;;; Inline Babel Call
  874. ;;
  875. ;; Inline Babel Calls are ignored.
  876. ;;; Inline Src Block
  877. (defun org-groff-inline-src-block (inline-src-block contents info)
  878. "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
  879. CONTENTS holds the contents of the item. INFO is a plist holding
  880. contextual information."
  881. (let* ((code (org-element-property :value inline-src-block)))
  882. (cond
  883. (org-groff-source-highlight
  884. (let* ((tmpdir (if (featurep 'xemacs)
  885. temp-directory
  886. temporary-file-directory))
  887. (in-file (make-temp-name
  888. (expand-file-name "srchilite" tmpdir)))
  889. (out-file (make-temp-name
  890. (expand-file-name "reshilite" tmpdir)))
  891. (org-lang (org-element-property :language inline-src-block))
  892. (lst-lang (cadr (assq (intern org-lang)
  893. org-groff-source-highlight-langs)))
  894. (cmd (concat (expand-file-name "source-highlight")
  895. " -s " lst-lang
  896. " -f groff_mm_color "
  897. " -i " in-file
  898. " -o " out-file)))
  899. (if lst-lang
  900. (let ((code-block ""))
  901. (with-temp-file in-file (insert code))
  902. (shell-command cmd)
  903. (setq code-block (org-file-contents out-file))
  904. (delete-file in-file)
  905. (delete-file out-file)
  906. code-block)
  907. (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
  908. code))))
  909. ;; Do not use a special package: transcode it verbatim.
  910. (t
  911. (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
  912. ;;; Inlinetask
  913. (defun org-groff-inlinetask (inlinetask contents info)
  914. "Transcode an INLINETASK element from Org to Groff.
  915. CONTENTS holds the contents of the block. INFO is a plist
  916. holding contextual information."
  917. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  918. (todo (and (plist-get info :with-todo-keywords)
  919. (let ((todo (org-element-property :todo-keyword inlinetask)))
  920. (and todo (org-export-data todo info)))))
  921. (todo-type (org-element-property :todo-type inlinetask))
  922. (tags (and (plist-get info :with-tags)
  923. (org-export-get-tags inlinetask info)))
  924. (priority (and (plist-get info :with-priority)
  925. (org-element-property :priority inlinetask))))
  926. ;; If `org-groff-format-inlinetask-function' is provided, call it
  927. ;; with appropriate arguments.
  928. (if (functionp org-groff-format-inlinetask-function)
  929. (funcall org-groff-format-inlinetask-function
  930. todo todo-type priority title tags contents)
  931. ;; Otherwise, use a default template.
  932. (org-groff--wrap-label
  933. inlinetask
  934. (let ((full-title
  935. (concat
  936. (when todo (format "\\fB%s\\fP " todo))
  937. (when priority (format " [\\#%c] " priority))
  938. title
  939. (when tags (format " \\fC:%s:\\fP "
  940. (mapconcat 'identity tags ":"))))))
  941. (format (concat "\n.DS I\n"
  942. "%s\n"
  943. ".sp"
  944. "%s\n"
  945. ".DE")
  946. full-title contents))))))
  947. ;;; Italic
  948. (defun org-groff-italic (italic contents info)
  949. "Transcode ITALIC from Org to Groff.
  950. CONTENTS is the text with italic markup. INFO is a plist holding
  951. contextual information."
  952. (org-groff--text-markup contents 'italic))
  953. ;;; Item
  954. (defun org-groff-item (item contents info)
  955. "Transcode an ITEM element from Org to Groff.
  956. CONTENTS holds the contents of the item. INFO is a plist holding
  957. contextual information."
  958. (let* ((bullet (org-element-property :bullet item))
  959. (type (org-element-property
  960. :type (org-element-property :parent item)))
  961. (checkbox (case (org-element-property :checkbox item)
  962. (on "\\o'\\(sq\\(mu'")
  963. (off "\\(sq")
  964. (trans "\\o'\\(sq\\(mi'")))
  965. (tag (let ((tag (org-element-property :tag item)))
  966. ;; Check-boxes must belong to the tag.
  967. (and tag (format "%s"
  968. (concat checkbox
  969. (org-export-data tag info)))))))
  970. (cond
  971. ((or checkbox tag)
  972. (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
  973. "\n"
  974. (org-trim (or contents " "))))
  975. ((eq type 'ordered)
  976. (concat ".LI"
  977. "\n"
  978. (org-trim (or contents " "))))
  979. (t
  980. (let* ((bullet (org-trim bullet))
  981. (marker (cond ((string= "-" bullet) "\\(em")
  982. ((string= "*" bullet) "\\(bu")
  983. (t "\\(dg"))))
  984. (concat ".LI " marker "\n"
  985. (org-trim (or contents " "))))))))
  986. ;;; Keyword
  987. (defun org-groff-keyword (keyword contents info)
  988. "Transcode a KEYWORD element from Org to Groff.
  989. CONTENTS is nil. INFO is a plist holding contextual information."
  990. (let ((key (org-element-property :key keyword))
  991. (value (org-element-property :value keyword)))
  992. (cond
  993. ((string= key "GROFF") value)
  994. (t nil))))
  995. ;;; Line Break
  996. (defun org-groff-line-break (line-break contents info)
  997. "Transcode a LINE-BREAK object from Org to Groff.
  998. CONTENTS is nil. INFO is a plist holding contextual information."
  999. ".br\n")
  1000. ;;; Link
  1001. ;; Inline images just place a call to .PSPIC or .PS/.PE
  1002. ;; and load the graph.
  1003. (defun org-groff-link--inline-image (link info)
  1004. "Return Groff code for an inline image.
  1005. LINK is the link pointing to the inline image. INFO is a plist
  1006. used as a communication channel."
  1007. (let* ((parent (org-export-get-parent-element link))
  1008. (path (let ((raw-path (org-element-property :path link)))
  1009. (if (not (file-name-absolute-p raw-path)) raw-path
  1010. (expand-file-name raw-path))))
  1011. (attr (org-export-read-attribute :attr_groff link))
  1012. (placement
  1013. (let ((pos (plist-get attr :position)))
  1014. (cond ((string= pos 'center) "")
  1015. ((string= pos 'left) "-L")
  1016. ((string= pos 'right) "-R")
  1017. (t ""))))
  1018. (width (or (plist-get attr :width) ""))
  1019. (height (or (plist-get attr :height) ""))
  1020. (caption (and (not (plist-get attr :disable-caption))
  1021. (org-groff--caption/label-string parent info))))
  1022. ;; Now clear ATTR from any special keyword and set a default value
  1023. ;; if nothing is left. Return proper string.
  1024. (concat
  1025. (cond
  1026. ((and org-groff-raster-to-ps
  1027. (or (string-match ".\.png$" path)
  1028. (string-match ".\.jpg$" path)))
  1029. (let ((eps-path (concat path ".eps")))
  1030. (shell-command (format org-groff-raster-to-ps path eps-path))
  1031. (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
  1032. placement eps-path width height)))
  1033. ((string-match ".\.pic$" path)
  1034. (format "\n.PS\ncopy \"%s\"\n.PE" path))
  1035. (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
  1036. placement path width height)))
  1037. (and caption (format "\n.FG \"%s\"" caption)))))
  1038. (defun org-groff-link (link desc info)
  1039. "Transcode a LINK object from Org to Groff.
  1040. DESC is the description part of the link, or the empty string.
  1041. INFO is a plist holding contextual information. See
  1042. `org-export-data'."
  1043. (let* ((type (org-element-property :type link))
  1044. (raw-path (org-element-property :path link))
  1045. ;; Ensure DESC really exists, or set it to nil.
  1046. (desc (and (not (string= desc "")) desc))
  1047. (imagep (org-export-inline-image-p
  1048. link org-groff-inline-image-rules))
  1049. (path (cond
  1050. ((member type '("http" "https" "ftp" "mailto"))
  1051. (concat type ":" raw-path))
  1052. ((and (string= type "file") (file-name-absolute-p raw-path))
  1053. (concat "file://" raw-path))
  1054. (t raw-path)))
  1055. protocol)
  1056. (cond
  1057. ;; Image file.
  1058. (imagep (org-groff-link--inline-image link info))
  1059. ;; import groff files
  1060. ((and (string= type "file")
  1061. (string-match ".\.groff$" raw-path))
  1062. (concat ".so " raw-path "\n"))
  1063. ;; Radio link: transcode target's contents and use them as link's
  1064. ;; description.
  1065. ((string= type "radio")
  1066. (let ((destination (org-export-resolve-radio-link link info)))
  1067. (if (not destination) desc
  1068. (format "\\fI [%s] \\fP"
  1069. (org-export-solidify-link-text
  1070. (org-element-property :value destination))))))
  1071. ;; Links pointing to a headline: find destination and build
  1072. ;; appropriate referencing command.
  1073. ((member type '("custom-id" "fuzzy" "id"))
  1074. (let ((destination (if (string= type "fuzzy")
  1075. (org-export-resolve-fuzzy-link link info)
  1076. (org-export-resolve-id-link link info))))
  1077. (case (org-element-type destination)
  1078. ;; Id link points to an external file.
  1079. (plain-text
  1080. (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
  1081. (format "\\fI file://%s \\fP" destination)))
  1082. ;; Fuzzy link points nowhere.
  1083. ('nil
  1084. (format org-groff-link-with-unknown-path-format
  1085. (or desc
  1086. (org-export-data
  1087. (org-element-property :raw-link link) info))))
  1088. ;; LINK points to a headline. If headlines are numbered and
  1089. ;; the link has no description, display headline's number.
  1090. ;; Otherwise, display description or headline's title.
  1091. (headline
  1092. (let ((label ""))
  1093. (if (and (plist-get info :section-numbers) (not desc))
  1094. (format "\\fI%s\\fP" label)
  1095. (format "\\fI%s\\fP"
  1096. (or desc
  1097. (org-export-data
  1098. (org-element-property :title destination) info))))))
  1099. ;; Fuzzy link points to a target. Do as above.
  1100. (otherwise
  1101. (let ((path (org-export-solidify-link-text path)))
  1102. (if (not desc) (format "\\fI%s\\fP" path)
  1103. (format "%s \\fBat\\fP \\fI%s\\fP" desc path)))))))
  1104. ;; External link with a description part.
  1105. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  1106. ;; External link without a description part.
  1107. (path (format "\\fI%s\\fP" path))
  1108. ;; No path, only description. Try to do something useful.
  1109. (t (format org-groff-link-with-unknown-path-format desc)))))
  1110. ;;; Node Property
  1111. (defun org-groff-node-property (node-property contents info)
  1112. "Transcode a NODE-PROPERTY element from Org to Groff.
  1113. CONTENTS is nil. INFO is a plist holding contextual
  1114. information."
  1115. (format "%s:%s"
  1116. (org-element-property :key node-property)
  1117. (let ((value (org-element-property :value node-property)))
  1118. (if value (concat " " value) ""))))
  1119. ;;; Paragraph
  1120. (defun org-groff-paragraph (paragraph contents info)
  1121. "Transcode a PARAGRAPH element from Org to Groff.
  1122. CONTENTS is the contents of the paragraph, as a string. INFO is
  1123. the plist used as a communication channel."
  1124. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  1125. (when parent
  1126. (let* ((parent-type (car parent))
  1127. (fixed-paragraph "")
  1128. (class (plist-get info :groff-class))
  1129. (class-options (plist-get info :groff-class-options))
  1130. (classes (assoc class org-groff-classes))
  1131. (classes-options (car (last classes)))
  1132. (paragraph-option (plist-get classes-options :paragraph)))
  1133. (cond
  1134. ((and (symbolp paragraph-option)
  1135. (fboundp paragraph-option))
  1136. (funcall paragraph-option parent-type parent contents))
  1137. ((and (eq parent-type 'item)
  1138. (plist-get (nth 1 parent) :bullet))
  1139. (setq fixed-paragraph (concat "" contents)))
  1140. ((eq parent-type 'section)
  1141. (setq fixed-paragraph (concat ".P\n" contents)))
  1142. ((eq parent-type 'footnote-definition)
  1143. (setq fixed-paragraph (concat "" contents)))
  1144. (t (setq fixed-paragraph (concat "" contents))))
  1145. fixed-paragraph))))
  1146. ;;; Plain List
  1147. (defun org-groff-plain-list (plain-list contents info)
  1148. "Transcode a PLAIN-LIST element from Org to Groff.
  1149. CONTENTS is the contents of the list. INFO is a plist holding
  1150. contextual information."
  1151. (let* ((type (org-element-property :type plain-list))
  1152. (attr (mapconcat #'identity
  1153. (org-element-property :attr_groff plain-list)
  1154. " "))
  1155. (groff-type (cond
  1156. ((eq type 'ordered) ".AL")
  1157. ((eq type 'unordered) ".BL")
  1158. ((eq type 'descriptive) ".VL 2.0i"))))
  1159. (org-groff--wrap-label
  1160. plain-list
  1161. (format "%s\n%s\n.LE" groff-type contents))))
  1162. ;;; Plain Text
  1163. (defun org-groff-plain-text (text info)
  1164. "Transcode a TEXT string from Org to Groff.
  1165. TEXT is the string to transcode. INFO is a plist holding
  1166. contextual information."
  1167. (let ((output text))
  1168. ;; Protect various characters.
  1169. (setq output (replace-regexp-in-string
  1170. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1171. "$\\" output nil t 1))
  1172. ;; Activate smart quotes. Be sure to provide original TEXT string
  1173. ;; since OUTPUT may have been modified.
  1174. (when (plist-get info :with-smart-quotes)
  1175. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  1176. ;; Handle Special Characters
  1177. (if org-groff-special-char
  1178. (dolist (special-char-list org-groff-special-char)
  1179. (setq output
  1180. (replace-regexp-in-string (car special-char-list)
  1181. (cdr special-char-list) output))))
  1182. ;; Handle break preservation if required.
  1183. (when (plist-get info :preserve-breaks)
  1184. (setq output (replace-regexp-in-string
  1185. "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output)))
  1186. ;; Return value.
  1187. output))
  1188. ;;; Planning
  1189. (defun org-groff-planning (planning contents info)
  1190. "Transcode a PLANNING element from Org to Groff.
  1191. CONTENTS is nil. INFO is a plist holding contextual
  1192. information."
  1193. (concat
  1194. (mapconcat
  1195. 'identity
  1196. (delq nil
  1197. (list
  1198. (let ((closed (org-element-property :closed planning)))
  1199. (when closed
  1200. (concat
  1201. (format "\\fR %s \\fP" org-closed-string)
  1202. (format org-groff-inactive-timestamp-format
  1203. (org-translate-time
  1204. (org-element-property :raw-value closed))))))
  1205. (let ((deadline (org-element-property :deadline planning)))
  1206. (when deadline
  1207. (concat
  1208. (format "\\fB %s \\fP" org-deadline-string)
  1209. (format org-groff-active-timestamp-format
  1210. (org-translate-time
  1211. (org-element-property :raw-value deadline))))))
  1212. (let ((scheduled (org-element-property :scheduled planning)))
  1213. (when scheduled
  1214. (concat
  1215. (format "\\fR %s \\fP" org-scheduled-string)
  1216. (format org-groff-active-timestamp-format
  1217. (org-translate-time
  1218. (org-element-property :raw-value scheduled))))))))
  1219. "")
  1220. ""))
  1221. ;;;; Property Drawer
  1222. (defun org-groff-property-drawer (property-drawer contents info)
  1223. "Transcode a PROPERTY-DRAWER element from Org to Groff.
  1224. CONTENTS holds the contents of the drawer. INFO is a plist
  1225. holding contextual information."
  1226. (and (org-string-nw-p contents)
  1227. (format "\\fC\n%s\\fP" contents)))
  1228. ;;; Quote Block
  1229. (defun org-groff-quote-block (quote-block contents info)
  1230. "Transcode a QUOTE-BLOCK element from Org to Groff.
  1231. CONTENTS holds the contents of the block. INFO is a plist
  1232. holding contextual information."
  1233. (org-groff--wrap-label
  1234. quote-block
  1235. (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
  1236. ;;; Radio Target
  1237. (defun org-groff-radio-target (radio-target text info)
  1238. "Transcode a RADIO-TARGET object from Org to Groff.
  1239. TEXT is the text of the target. INFO is a plist holding
  1240. contextual information."
  1241. (format "%s - %s"
  1242. (org-export-solidify-link-text
  1243. (org-element-property :value radio-target))
  1244. text))
  1245. ;;; Section
  1246. (defun org-groff-section (section contents info)
  1247. "Transcode a SECTION element from Org to Groff.
  1248. CONTENTS holds the contents of the section. INFO is a plist
  1249. holding contextual information."
  1250. contents)
  1251. ;;; Special Block
  1252. (defun org-groff-special-block (special-block contents info)
  1253. "Transcode a SPECIAL-BLOCK element from Org to Groff.
  1254. CONTENTS holds the contents of the block. INFO is a plist
  1255. holding contextual information."
  1256. (if (org-export-raw-special-block-p special-block info)
  1257. (org-remove-indentation (org-element-property :raw-value special-block))
  1258. (let ((type (downcase (org-element-property :type special-block))))
  1259. (org-groff--wrap-label special-block (format "%s\n" contents)))))
  1260. ;;; Src Block
  1261. (defun org-groff-src-block (src-block contents info)
  1262. "Transcode a SRC-BLOCK element from Org to Groff.
  1263. CONTENTS holds the contents of the item. INFO is a plist holding
  1264. contextual information."
  1265. (let* ((lang (org-element-property :language src-block))
  1266. (label (org-element-property :name src-block))
  1267. (code (org-element-property :value src-block))
  1268. (custom-env (and lang
  1269. (cadr (assq (intern lang)
  1270. org-groff-custom-lang-environments))))
  1271. (num-start (case (org-element-property :number-lines src-block)
  1272. (continued (org-export-get-loc src-block info))
  1273. (new 0)))
  1274. (retain-labels (org-element-property :retain-labels src-block))
  1275. (caption (and (not (org-export-read-attribute
  1276. :attr_groff src-block :disable-caption))
  1277. (org-groff--caption/label-string src-block info))))
  1278. (cond
  1279. ;; Case 1. No source fontification.
  1280. ((not org-groff-source-highlight)
  1281. (concat
  1282. (format ".DS I\n\\fC%s\\fP\n.DE\n"
  1283. (org-export-format-code-default src-block info))
  1284. (and caption (format ".EX \"%s\" " caption))))
  1285. ;; Case 2. Source fontification.
  1286. (org-groff-source-highlight
  1287. (let* ((tmpdir (if (featurep 'xemacs)
  1288. temp-directory
  1289. temporary-file-directory))
  1290. (in-file (make-temp-name
  1291. (expand-file-name "srchilite" tmpdir)))
  1292. (out-file (make-temp-name
  1293. (expand-file-name "reshilite" tmpdir)))
  1294. (org-lang (org-element-property :language src-block))
  1295. (lst-lang (cadr (assq (intern org-lang)
  1296. org-groff-source-highlight-langs)))
  1297. (cmd (concat "source-highlight"
  1298. " -s " lst-lang
  1299. " -f groff_mm_color "
  1300. " -i " in-file
  1301. " -o " out-file)))
  1302. (concat
  1303. (if lst-lang
  1304. (let ((code-block ""))
  1305. (with-temp-file in-file (insert code))
  1306. (shell-command cmd)
  1307. (setq code-block (org-file-contents out-file))
  1308. (delete-file in-file)
  1309. (delete-file out-file)
  1310. (format "%s\n" code-block))
  1311. (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
  1312. code))
  1313. (and caption (format ".EX \"%s\" " caption))))))))
  1314. ;;; Statistics Cookie
  1315. (defun org-groff-statistics-cookie (statistics-cookie contents info)
  1316. "Transcode a STATISTICS-COOKIE object from Org to Groff.
  1317. CONTENTS is nil. INFO is a plist holding contextual information."
  1318. (org-element-property :value statistics-cookie))
  1319. ;;; Strike-Through
  1320. (defun org-groff-strike-through (strike-through contents info)
  1321. "Transcode STRIKE-THROUGH from Org to Groff.
  1322. CONTENTS is the text with strike-through markup. INFO is a plist
  1323. holding contextual information."
  1324. (org-groff--text-markup contents 'strike-through))
  1325. ;;; Subscript
  1326. (defun org-groff-subscript (subscript contents info)
  1327. "Transcode a SUBSCRIPT object from Org to Groff.
  1328. CONTENTS is the contents of the object. INFO is a plist holding
  1329. contextual information."
  1330. (format "\\d\\s-2%s\\s+2\\u" contents))
  1331. ;;; Superscript "^_%s$
  1332. (defun org-groff-superscript (superscript contents info)
  1333. "Transcode a SUPERSCRIPT object from Org to Groff.
  1334. CONTENTS is the contents of the object. INFO is a plist holding
  1335. contextual information."
  1336. (format "\\u\\s-2%s\\s+2\\d" contents))
  1337. ;;; Table
  1338. ;;
  1339. ;; `org-groff-table' is the entry point for table transcoding. It
  1340. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1341. ;; delegates the job to `org-groff-table--org-table' function,
  1342. ;; depending of the type of the table.
  1343. ;;
  1344. ;; `org-groff-table--align-string' is a subroutine used to build
  1345. ;; alignment string for Org tables.
  1346. (defun org-groff-table (table contents info)
  1347. "Transcode a TABLE element from Org to Groff.
  1348. CONTENTS is the contents of the table. INFO is a plist holding
  1349. contextual information."
  1350. (cond
  1351. ;; Case 1: verbatim table.
  1352. ((or org-groff-tables-verbatim
  1353. (let ((attr (read (format "(%s)"
  1354. (mapconcat
  1355. #'identity
  1356. (org-element-property :attr_groff table) " ")))))
  1357. (and attr (plist-get attr :verbatim))))
  1358. (format ".DS L\n\\fC%s\\fP\n.DE"
  1359. ;; Re-create table, without affiliated keywords.
  1360. (org-trim
  1361. (org-element-interpret-data
  1362. `(table nil ,@(org-element-contents table))))))
  1363. ;; Case 2: Standard table.
  1364. (t (org-groff-table--org-table table contents info))))
  1365. (defun org-groff-table--align-string (divider table info)
  1366. "Return an appropriate Groff alignment string.
  1367. TABLE is the considered table. INFO is a plist used as
  1368. a communication channel."
  1369. (let (alignment)
  1370. ;; Extract column groups and alignment from first (non-rule) row.
  1371. (org-element-map
  1372. (org-element-map table 'table-row
  1373. (lambda (row)
  1374. (and (eq (org-element-property :type row) 'standard) row))
  1375. info 'first-match)
  1376. 'table-cell
  1377. (lambda (cell)
  1378. (let* ((borders (org-export-table-cell-borders cell info))
  1379. (raw-width (org-export-table-cell-width cell info))
  1380. (width-cm (when raw-width (/ raw-width 5)))
  1381. (width (if raw-width (format "w(%dc)"
  1382. (if (< width-cm 1) 1 width-cm)) "")))
  1383. ;; Check left border for the first cell only.
  1384. ;; Alignment is nil on assignment
  1385. (when (and (memq 'left borders) (not alignment))
  1386. (push "|" alignment))
  1387. (push
  1388. (case (org-export-table-cell-alignment cell info)
  1389. (left (concat "l" width divider))
  1390. (right (concat "r" width divider))
  1391. (center (concat "c" width divider)))
  1392. alignment)
  1393. (when (memq 'right borders) (push "|" alignment))))
  1394. info)
  1395. (apply 'concat (reverse alignment))))
  1396. (defun org-groff-table--org-table (table contents info)
  1397. "Return appropriate Groff code for an Org table.
  1398. TABLE is the table type element to transcode. CONTENTS is its
  1399. contents, as a string. INFO is a plist used as a communication
  1400. channel.
  1401. This function assumes TABLE has `org' as its `:type' attribute."
  1402. (let* ((attr (org-export-read-attribute :attr_groff table))
  1403. (label (org-element-property :name table))
  1404. (caption (and (not (plist-get attr :disable-caption))
  1405. (org-groff--caption/label-string table info)))
  1406. (divider (if (plist-get attr :divider) "|" " "))
  1407. ;; Determine alignment string.
  1408. (alignment (org-groff-table--align-string divider table info))
  1409. ;; Extract others display options.
  1410. (lines (org-split-string contents "\n"))
  1411. (attr-list
  1412. (delq nil
  1413. (list (and (plist-get attr :expand) "expand")
  1414. (let ((placement (plist-get attr :placement)))
  1415. (cond ((string= placement 'center) "center")
  1416. ((string= placement 'left) nil)
  1417. (t (if org-groff-tables-centered "center" ""))))
  1418. (or (plist-get attr :boxtype) "box"))))
  1419. (title-line (plist-get attr :title-line))
  1420. (long-cells (plist-get attr :long-cells))
  1421. (table-format
  1422. (concat
  1423. (or (car attr-list) "")
  1424. (or
  1425. (let (output-list)
  1426. (when (cdr attr-list)
  1427. (dolist (attr-item (cdr attr-list))
  1428. (setq output-list (concat output-list
  1429. (format ",%s" attr-item)))))
  1430. output-list) "")))
  1431. (first-line
  1432. (when lines (org-split-string (car lines) "\t"))))
  1433. ;; Prepare the final format string for the table.
  1434. (cond
  1435. ;; Others.
  1436. (lines
  1437. (concat ".TS\n " table-format ";\n"
  1438. (format "%s.\n"
  1439. (let ((final-line ""))
  1440. (when title-line
  1441. (dotimes (i (length first-line))
  1442. (setq final-line (concat final-line "cb" divider))))
  1443. (setq final-line (concat final-line "\n"))
  1444. (if alignment
  1445. (setq final-line (concat final-line alignment))
  1446. (dotimes (i (length first-line))
  1447. (setq final-line (concat final-line "c" divider))))
  1448. final-line))
  1449. (format "%s.TE\n"
  1450. (let ((final-line "")
  1451. (long-line "")
  1452. (lines (org-split-string contents "\n")))
  1453. (dolist (line-item lines)
  1454. (setq long-line "")
  1455. (if long-cells
  1456. (progn
  1457. (if (string= line-item "_")
  1458. (setq long-line (format "%s\n" line-item))
  1459. ;; else string =
  1460. (let ((cell-item-list (org-split-string line-item "\t")))
  1461. (dolist (cell-item cell-item-list)
  1462. (cond ((eq cell-item (car (last cell-item-list)))
  1463. (setq long-line (concat long-line
  1464. (format "T{\n%s\nT}\t\n" cell-item))))
  1465. (t
  1466. (setq long-line (concat long-line
  1467. (format "T{\n%s\nT}\t" cell-item))))))
  1468. long-line))
  1469. ;; else long cells
  1470. (setq final-line (concat final-line long-line)))
  1471. (setq final-line (concat final-line line-item "\n"))))
  1472. final-line))
  1473. (if caption (format ".TB \"%s\"" caption) ""))))))
  1474. ;;; Table Cell
  1475. (defun org-groff-table-cell (table-cell contents info)
  1476. "Transcode a TABLE-CELL element from Org to Groff
  1477. CONTENTS is the cell contents. INFO is a plist used as
  1478. a communication channel."
  1479. (progn
  1480. (concat (if (and contents
  1481. org-groff-table-scientific-notation
  1482. (string-match orgtbl-exp-regexp contents))
  1483. ;; Use appropriate format string for scientific
  1484. ;; notation.
  1485. (format org-groff-table-scientific-notation
  1486. (match-string 1 contents)
  1487. (match-string 2 contents))
  1488. contents)
  1489. (when (org-export-get-next-element table-cell info) "\t"))))
  1490. ;;; Table Row
  1491. (defun org-groff-table-row (table-row contents info)
  1492. "Transcode a TABLE-ROW element from Org to Groff
  1493. CONTENTS is the contents of the row. INFO is a plist used as
  1494. a communication channel."
  1495. ;; Rules are ignored since table separators are deduced from
  1496. ;; borders of the current row.
  1497. (when (eq (org-element-property :type table-row) 'standard)
  1498. (let* ((attr (mapconcat 'identity
  1499. (org-element-property
  1500. :attr_groff (org-export-get-parent table-row))
  1501. " "))
  1502. ;; TABLE-ROW's borders are extracted from its first cell.
  1503. (borders
  1504. (org-export-table-cell-borders
  1505. (car (org-element-contents table-row)) info)))
  1506. (concat
  1507. ;; Mark horizontal lines
  1508. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  1509. contents
  1510. (cond
  1511. ;; When BOOKTABS are activated enforce bottom rule even when
  1512. ;; no hline was specifically marked.
  1513. ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  1514. ((memq 'below borders) "\n_"))))))
  1515. ;;; Target
  1516. (defun org-groff-target (target contents info)
  1517. "Transcode a TARGET object from Org to Groff.
  1518. CONTENTS is nil. INFO is a plist holding contextual
  1519. information."
  1520. (format "\\fI%s\\fP"
  1521. (org-export-solidify-link-text (org-element-property :value target))))
  1522. ;;; Timestamp
  1523. (defun org-groff-timestamp (timestamp contents info)
  1524. "Transcode a TIMESTAMP object from Org to Groff.
  1525. CONTENTS is nil. INFO is a plist holding contextual
  1526. information."
  1527. (let ((value (org-groff-plain-text
  1528. (org-timestamp-translate timestamp) info)))
  1529. (case (org-element-property :type timestamp)
  1530. ((active active-range)
  1531. (format org-groff-active-timestamp-format value))
  1532. ((inactive inactive-range)
  1533. (format org-groff-inactive-timestamp-format value))
  1534. (t (format org-groff-diary-timestamp-format value)))))
  1535. ;;; Underline
  1536. (defun org-groff-underline (underline contents info)
  1537. "Transcode UNDERLINE from Org to Groff.
  1538. CONTENTS is the text with underline markup. INFO is a plist
  1539. holding contextual information."
  1540. (org-groff--text-markup contents 'underline))
  1541. ;;; Verbatim
  1542. (defun org-groff-verbatim (verbatim contents info)
  1543. "Transcode a VERBATIM object from Org to Groff.
  1544. CONTENTS is nil. INFO is a plist used as a communication
  1545. channel."
  1546. (org-groff--text-markup (org-element-property :value verbatim) 'verbatim))
  1547. ;;; Verse Block
  1548. (defun org-groff-verse-block (verse-block contents info)
  1549. "Transcode a VERSE-BLOCK element from Org to Groff.
  1550. CONTENTS is verse block contents. INFO is a plist holding
  1551. contextual information."
  1552. (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
  1553. ;;; Interactive functions
  1554. (defun org-groff-export-to-groff
  1555. (&optional async subtreep visible-only body-only ext-plist)
  1556. "Export current buffer to a Groff file.
  1557. If narrowing is active in the current buffer, only export its
  1558. narrowed part.
  1559. If a region is active, export that region.
  1560. A non-nil optional argument ASYNC means the process should happen
  1561. asynchronously. The resulting file should be accessible through
  1562. the `org-export-stack' interface.
  1563. When optional argument SUBTREEP is non-nil, export the sub-tree
  1564. at point, extracting information from the headline properties
  1565. first.
  1566. When optional argument VISIBLE-ONLY is non-nil, don't export
  1567. contents of hidden elements.
  1568. EXT-PLIST, when provided, is a property list with external
  1569. parameters overriding Org default settings, but still inferior to
  1570. file-local settings.
  1571. Return output file's name."
  1572. (interactive)
  1573. (let ((outfile (org-export-output-file-name ".groff" subtreep))
  1574. (org-groff-registered-references nil)
  1575. (org-groff-special-content nil))
  1576. (org-export-to-file 'groff outfile
  1577. async subtreep visible-only body-only ext-plist)))
  1578. (defun org-groff-export-to-pdf
  1579. (&optional async subtreep visible-only body-only ext-plist)
  1580. "Export current buffer to Groff then process through to PDF.
  1581. If narrowing is active in the current buffer, only export its
  1582. narrowed part.
  1583. If a region is active, export that region.
  1584. A non-nil optional argument ASYNC means the process should happen
  1585. asynchronously. The resulting file should be accessible through
  1586. the `org-export-stack' interface.
  1587. When optional argument SUBTREEP is non-nil, export the sub-tree
  1588. at point, extracting information from the headline properties
  1589. first.
  1590. When optional argument VISIBLE-ONLY is non-nil, don't export
  1591. contents of hidden elements.
  1592. EXT-PLIST, when provided, is a property list with external
  1593. parameters overriding Org default settings, but still inferior to
  1594. file-local settings.
  1595. Return PDF file's name."
  1596. (interactive)
  1597. (let ((outfile (org-export-output-file-name ".groff" subtreep)))
  1598. (org-export-to-file 'groff outfile
  1599. async subtreep visible-only body-only ext-plist
  1600. (lambda (file) (org-groff-compile file)))))
  1601. (defun org-groff-compile (file)
  1602. "Compile a Groff file.
  1603. FILE is the name of the file being compiled. Processing is done
  1604. through the command specified in `org-groff-pdf-process'.
  1605. Return PDF file name or an error if it couldn't be produced."
  1606. (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
  1607. (full-name (file-truename file))
  1608. (out-dir (file-name-directory file))
  1609. ;; Properly set working directory for compilation.
  1610. (default-directory (if (file-name-absolute-p file)
  1611. (file-name-directory full-name)
  1612. default-directory))
  1613. errors)
  1614. (message (format "Processing Groff file %s ..." file))
  1615. (save-window-excursion
  1616. (cond
  1617. ;; A function is provided: Apply it.
  1618. ((functionp org-groff-pdf-process)
  1619. (funcall org-groff-pdf-process (shell-quote-argument file)))
  1620. ;; A list is provided: Replace %b, %f and %o with appropriate
  1621. ;; values in each command before applying it. Output is
  1622. ;; redirected to "*Org PDF Groff Output*" buffer.
  1623. ((consp org-groff-pdf-process)
  1624. (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
  1625. (mapc
  1626. (lambda (command)
  1627. (shell-command
  1628. (replace-regexp-in-string
  1629. "%b" (shell-quote-argument base-name)
  1630. (replace-regexp-in-string
  1631. "%f" (shell-quote-argument full-name)
  1632. (replace-regexp-in-string
  1633. "%o" (shell-quote-argument out-dir) command t t)
  1634. t t) t t)
  1635. outbuf))
  1636. org-groff-pdf-process)
  1637. ;; Collect standard errors from output buffer.
  1638. (setq errors (org-groff-collect-errors outbuf))))
  1639. (t (error "No valid command to process to PDF")))
  1640. (let ((pdffile (concat out-dir base-name ".pdf")))
  1641. ;; Check for process failure. Provide collected errors if
  1642. ;; possible.
  1643. (if (not (file-exists-p pdffile))
  1644. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1645. (when errors (concat ": " errors))))
  1646. ;; Else remove log files, when specified, and signal end of
  1647. ;; process to user, along with any error encountered.
  1648. (when org-groff-remove-logfiles
  1649. (dolist (ext org-groff-logfiles-extensions)
  1650. (let ((file (concat out-dir base-name "." ext)))
  1651. (when (file-exists-p file) (delete-file file)))))
  1652. (message (concat "Process completed"
  1653. (if (not errors) "."
  1654. (concat " with errors: " errors)))))
  1655. ;; Return output file name.
  1656. pdffile))))
  1657. (defun org-groff-collect-errors (buffer)
  1658. "Collect some kind of errors from \"groff\" output
  1659. BUFFER is the buffer containing output.
  1660. Return collected error types as a string, or nil if there was
  1661. none."
  1662. (with-current-buffer buffer
  1663. (save-excursion
  1664. (goto-char (point-max))
  1665. ;; Find final run
  1666. nil)))
  1667. (provide 'ox-groff)
  1668. ;;; ox-groff.el ends here