org-e-groff.el 74 KB

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