org-e-groff.el 72 KB

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