ox-groff.el 71 KB

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