org-e-groff.el 74 KB

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