org-e-groff.el 74 KB

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