org-e-groff.el 74 KB

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