org-e-groff.el 77 KB

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