ox-groff.el 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  1. ;;; ox-groff.el --- Groff Back-End for Org Export Engine
  2. ;; Copyright (C) 2011-2015 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. ;; This file is not part of GNU Emacs.
  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. (node-property . org-groff-node-property)
  60. (paragraph . org-groff-paragraph)
  61. (plain-list . org-groff-plain-list)
  62. (plain-text . org-groff-plain-text)
  63. (planning . org-groff-planning)
  64. (property-drawer . org-groff-property-drawer)
  65. (quote-block . org-groff-quote-block)
  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 a 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 ((title (if (plist-get info :with-title) title ""))
  471. (subtitle1 (plist-get attr :subtitle1))
  472. (subtitle2 (plist-get attr :subtitle2)))
  473. (cond
  474. ((string= "" title)
  475. (format ".TL \"%s\" \"%s\" \n%s\n"
  476. (or subtitle1 "")
  477. (or subtitle2 "") " "))
  478. ((not (or subtitle1 subtitle2))
  479. (format ".TL\n%s\n"
  480. (or title "")))
  481. (t
  482. (format ".TL \"%s\" \"%s \" \n%s\n"
  483. (or subtitle1 "")
  484. (or subtitle2 "") title))))
  485. ;; 3. Author.
  486. ;; In Groff, .AU *MUST* be placed after .TL
  487. ;; If From, populate with data from From else
  488. ;;
  489. (let ((author (and (plist-get info :with-author)
  490. (let ((auth (plist-get info :author)))
  491. (and auth (org-export-data auth info)))))
  492. (email (and (plist-get info :with-email)
  493. (org-export-data (plist-get info :email) info)))
  494. (from-data (org-groff--get-tagged-content "FROM" info))
  495. (to-data (org-groff--get-tagged-content "TO" info)))
  496. (cond
  497. ((and author from-data)
  498. (let ((au-line
  499. (mapconcat
  500. (lambda (from-line)
  501. (format " \"%s\" " from-line))
  502. (split-string
  503. (setq from-data
  504. (replace-regexp-in-string "\\.P\n" "" from-data)) "\n") "")))
  505. (concat
  506. (format ".AU \"%s\" " author) au-line "\n")))
  507. ((and author email (not (string= "" email)))
  508. (format ".AU \"%s\" \"%s\"\n" author email))
  509. (author (format ".AU \"%s\"\n" author))
  510. (t ".AU \"\" \n")))
  511. ;; 4. Author Title, if present
  512. (let ((at-item (plist-get attr :author-title)))
  513. (if (and at-item (stringp at-item))
  514. (format ".AT \"%s\" \n" at-item)
  515. ""))
  516. ;; 5. Date.
  517. (when (plist-get info :with-date)
  518. (let ((date (org-export-data (org-export-get-date info) info)))
  519. (and (org-string-nw-p date) (format ".ND \"%s\"\n" date))))
  520. ;;
  521. ;; If Abstract, then Populate Abstract
  522. ;;
  523. (let ((abstract-data (org-groff--get-tagged-content "ABSTRACT" info))
  524. (to-data (org-groff--get-tagged-content "TO" info)))
  525. (cond
  526. (abstract-data
  527. (format ".AS\n%s\n.AE\n" abstract-data))
  528. (to-data
  529. (format ".AS\n%s\n.AE\n" to-data))))))
  530. (defun org-groff--letter-head (title contents attr info)
  531. (let ((author (and (plist-get info :with-author)
  532. (let ((auth (plist-get info :author)))
  533. (and auth (org-export-data auth info)))))
  534. (email (and (plist-get info :with-email)
  535. (org-export-data (plist-get info :email) info)))
  536. (from-data (org-groff--get-tagged-content "FROM" info))
  537. (at-item (plist-get attr :author-title))
  538. (to-data (org-groff--get-tagged-content "TO" info)))
  539. ;; If FROM then get data from FROM
  540. (if from-data
  541. (setq from-data
  542. (replace-regexp-in-string "\\.P\n" "" from-data))
  543. (setq from-data ""))
  544. (if to-data
  545. (setq to-data
  546. (replace-regexp-in-string "\\.P\n" "" to-data))
  547. (setq from-data ""))
  548. (concat
  549. (cond
  550. (from-data
  551. (format ".WA \"%s\" \"%s\" \n%s\n.WE\n" author (or at-item "") from-data))
  552. ((and author email (not (string= "" email)))
  553. (format ".WA \"%s\"\n \"%s\"\n.WE\n" author email))
  554. (author (format ".WA \"%s\"\n.WE\n" author))
  555. (t ".WA \"\" \n.WE\n"))
  556. ;; If TO then get data from TO
  557. (when to-data
  558. (format ".IA \n%s\n.IE\n" to-data)))))
  559. ;;; Template
  560. (defun org-groff-template (contents info)
  561. "Return complete document string after Groff conversion.
  562. CONTENTS is the transcoded contents string. INFO is a plist
  563. holding export options."
  564. (let* ((title (org-export-data (plist-get info :title) info))
  565. (attr (read
  566. (format "(%s)"
  567. (mapconcat
  568. #'identity
  569. (list (plist-get info :groff-class-options))
  570. " "))))
  571. (class (plist-get info :groff-class))
  572. (class-options (plist-get info :groff-class-options))
  573. (classes (assoc class org-groff-classes))
  574. (classes-options (car (last classes)))
  575. (heading-option (plist-get classes-options :heading))
  576. (type-option (plist-get classes-options :type))
  577. (last-option (plist-get classes-options :last-section))
  578. (hyphenate (plist-get attr :hyphenate))
  579. (justify-right (plist-get attr :justify-right))
  580. (document-class-string
  581. (progn
  582. (org-element-normalize-string
  583. (let* ((header (nth 1 (assoc class org-groff-classes)))
  584. (document-class-item (if (stringp header) header "")))
  585. document-class-item)))))
  586. (concat
  587. (if justify-right
  588. (case justify-right
  589. ('yes ".SA 1 \n")
  590. ('no ".SA 0 \n")
  591. (t ""))
  592. "")
  593. (if hyphenate
  594. (case hyphenate
  595. ('yes ".nr Hy 1 \n")
  596. ('no ".nr Hy 0 \n")
  597. (t ""))
  598. "")
  599. (cond
  600. ((string= type-option "custom") "")
  601. ((and (stringp document-class-string)
  602. (string= type-option "cover"))
  603. (concat
  604. (format ".COVER %s\n" document-class-string)
  605. (org-groff--mt-head title contents attr info)
  606. ".COVEND\n"))
  607. ((string= type-option "memo")
  608. (concat
  609. (org-groff--mt-head title contents attr info)
  610. document-class-string))
  611. ((string= type-option "letter")
  612. (concat
  613. (org-groff--letter-head title contents attr info)
  614. (let ((sa-item (plist-get attr :salutation))
  615. (cn-item (plist-get attr :confidential))
  616. (sj-item (plist-get attr :subject))
  617. (rn-item (plist-get attr :reference))
  618. (at-item (plist-get attr :attention)))
  619. (concat
  620. (if (stringp sa-item)
  621. (format ".LO SA \"%s\" \n" sa-item)
  622. ".LO SA\n")
  623. (when cn-item
  624. (if (stringp cn-item)
  625. (format ".LO CN \"%s\"\n" cn-item)
  626. ".LO CN\n"))
  627. (when (and at-item (stringp at-item))
  628. (format ".LO AT \"%s\" \n" at-item))
  629. (when (and title rn-item)
  630. (format ".LO RN \"%s\"\n" title))
  631. (when (and sj-item (stringp sj-item))
  632. (format ".LO SJ \"%s\" \n" sj-item))
  633. ".LT " document-class-string "\n"))))
  634. (t ""))
  635. contents
  636. (cond
  637. ((string= last-option "toc")
  638. ".TC")
  639. ((string= last-option "sign")
  640. (let ((fc-item (plist-get attr :closing)))
  641. (concat (if (stringp fc-item)
  642. (format ".FC \"%s\" \n" fc-item)
  643. ".FC\n")
  644. ".SG\n")))
  645. (t ""))
  646. (progn
  647. (mapconcat
  648. (lambda (item)
  649. (when (string= (car item) "NS")
  650. (replace-regexp-in-string
  651. "\\.P\n" "" (cdr item))))
  652. (reverse org-groff-special-content) "\n")))))
  653. ;;; Transcode Functions
  654. ;;; Babel Call
  655. ;;
  656. ;; Babel Calls are ignored.
  657. ;;; Bold
  658. (defun org-groff-bold (bold contents info)
  659. "Transcode BOLD from Org to Groff.
  660. CONTENTS is the text with bold markup. INFO is a plist holding
  661. contextual information."
  662. (org-groff--text-markup contents 'bold))
  663. ;;; Center Block
  664. (defun org-groff-center-block (center-block contents info)
  665. "Transcode a CENTER-BLOCK element from Org to Groff.
  666. CONTENTS holds the contents of the center block. INFO is a plist
  667. holding contextual information."
  668. (org-groff--wrap-label
  669. center-block
  670. (format ".DS C \n%s\n.DE" contents)))
  671. ;;; Clock
  672. (defun org-groff-clock (clock contents info)
  673. "Transcode a CLOCK element from Org to Groff.
  674. CONTENTS is nil. INFO is a plist holding contextual
  675. information."
  676. (concat
  677. (format "\\fB%s\\fP " org-clock-string)
  678. (format org-groff-inactive-timestamp-format
  679. (concat (org-translate-time
  680. (org-element-property :raw-value
  681. (org-element-property :value clock)))
  682. (let ((time (org-element-property :duration clock)))
  683. (and time (format " (%s)" time)))))))
  684. ;;; Code
  685. (defun org-groff-code (code contents info)
  686. "Transcode a CODE object from Org to Groff.
  687. CONTENTS is nil. INFO is a plist used as a communication
  688. channel."
  689. (org-groff--text-markup (org-element-property :value code) 'code))
  690. ;;; Comments and Comment Blocks are ignored.
  691. ;;; Drawer
  692. (defun org-groff-drawer (drawer contents info)
  693. "Transcode a DRAWER element from Org to Groff.
  694. CONTENTS holds the contents of the block. INFO is a plist
  695. holding contextual information."
  696. (let* ((name (org-element-property :drawer-name drawer))
  697. (output (if (functionp org-groff-format-drawer-function)
  698. (funcall org-groff-format-drawer-function
  699. name contents)
  700. ;; If there's no user defined function: simply
  701. ;; display contents of the drawer.
  702. contents)))
  703. (org-groff--wrap-label drawer output)))
  704. ;;; Dynamic Block
  705. (defun org-groff-dynamic-block (dynamic-block contents info)
  706. "Transcode a DYNAMIC-BLOCK element from Org to Groff.
  707. CONTENTS holds the contents of the block. INFO is a plist
  708. holding contextual information. See `org-export-data'."
  709. (org-groff--wrap-label dynamic-block contents))
  710. ;;; Entity
  711. (defun org-groff-entity (entity contents info)
  712. "Transcode an ENTITY object from Org to Groff.
  713. CONTENTS are the definition itself. INFO is a plist holding
  714. contextual information."
  715. (org-element-property :utf-8 entity))
  716. ;;; Example Block
  717. (defun org-groff-example-block (example-block contents info)
  718. "Transcode an EXAMPLE-BLOCK element from Org to Groff.
  719. CONTENTS is nil. INFO is a plist holding contextual
  720. information."
  721. (org-groff--wrap-label
  722. example-block
  723. (format ".DS L\n%s\n.DE"
  724. (org-export-format-code-default example-block info))))
  725. ;;; Export Block
  726. (defun org-groff-export-block (export-block contents info)
  727. "Transcode a EXPORT-BLOCK element from Org to Groff.
  728. CONTENTS is nil. INFO is a plist holding contextual information."
  729. (when (string= (org-element-property :type export-block) "GROFF")
  730. (org-remove-indentation (org-element-property :value export-block))))
  731. ;;; Export Snippet
  732. (defun org-groff-export-snippet (export-snippet contents info)
  733. "Transcode a EXPORT-SNIPPET object from Org to Groff.
  734. CONTENTS is nil. INFO is a plist holding contextual information."
  735. (when (eq (org-export-snippet-backend export-snippet) 'groff)
  736. (org-element-property :value export-snippet)))
  737. ;;; Fixed Width
  738. (defun org-groff-fixed-width (fixed-width contents info)
  739. "Transcode a FIXED-WIDTH element from Org to Groff.
  740. CONTENTS is nil. INFO is a plist holding contextual information."
  741. (org-groff--wrap-label
  742. fixed-width
  743. (format "\\fC\n%s\\fP"
  744. (org-remove-indentation
  745. (org-element-property :value fixed-width)))))
  746. ;;; Footnote Definition
  747. ;;
  748. ;; Footnote Definitions are ignored.
  749. ;;
  750. ;; Footnotes are handled automatically in GROFF. Although manual
  751. ;; references can be added, not really required.
  752. (defun org-groff-footnote-reference (footnote-reference contents info)
  753. ;; Changing from info to footnote-reference
  754. (let* ((raw (org-export-get-footnote-definition footnote-reference info))
  755. (n (org-export-get-footnote-number footnote-reference info))
  756. (data (org-trim (org-export-data raw info)))
  757. (ref-id (plist-get (nth 1 footnote-reference) :label)))
  758. ;; It is a reference
  759. (if (string-match "fn:rl" ref-id)
  760. (if (member ref-id org-groff-registered-references)
  761. (format "\\*[%s]" ref-id)
  762. (progn
  763. (push ref-id org-groff-registered-references)
  764. (format "\\*(Rf\n.RS \"%s\" \n%s\n.RF\n" ref-id data)))
  765. ;; else it is a footnote
  766. (format "\\u\\s-2%s\\d\\s+2\n.FS %s\n%s\n.FE\n" n n data))))
  767. ;;; Headline
  768. (defun org-groff-headline (headline contents info)
  769. "Transcode a HEADLINE element from Org to Groff.
  770. CONTENTS holds the contents of the headline. INFO is a plist
  771. holding contextual information."
  772. (let* ((class (plist-get info :groff-class))
  773. (level (org-export-get-relative-level headline info))
  774. (numberedp (org-export-numbered-headline-p headline info))
  775. ;; Section formatting will set two placeholders: one for the
  776. ;; title and the other for the contents.
  777. (classes (assoc class org-groff-classes))
  778. (classes-options (car (last classes)))
  779. (heading-option (plist-get classes-options :heading))
  780. (section-fmt
  781. (progn
  782. (cond
  783. ((and (symbolp heading-option)
  784. (fboundp heading-option))
  785. (funcall heading-option level numberedp))
  786. ((> level 7) nil)
  787. (t (if numberedp
  788. (concat ".H " (number-to-string level) " \"%s\"\n%s")
  789. ".HU \"%s\"\n%s")))))
  790. ;; End of section-fmt
  791. (text (org-export-data (org-element-property :title headline) info))
  792. (todo
  793. (and (plist-get info :with-todo-keywords)
  794. (let ((todo (org-element-property :todo-keyword headline)))
  795. (and todo (org-export-data todo info)))))
  796. (todo-type (and todo (org-element-property :todo-type headline)))
  797. (tags (and (plist-get info :with-tags)
  798. (org-export-get-tags headline info)))
  799. (priority (and (plist-get info :with-priority)
  800. (org-element-property :priority headline)))
  801. ;; Create the headline text along with a no-tag version. The
  802. ;; latter is required to remove tags from table of contents.
  803. (full-text (if (functionp org-groff-format-headline-function)
  804. ;; User-defined formatting function.
  805. (funcall org-groff-format-headline-function
  806. todo todo-type priority text tags)
  807. ;; Default formatting.
  808. (concat
  809. (when todo
  810. (format "\\fB%s\\fP " todo))
  811. (when priority (format " [\\#%c] " priority))
  812. text
  813. (when tags
  814. (format " \\fC:%s:\\fP "
  815. (mapconcat 'identity tags ":"))))))
  816. (full-text-no-tag
  817. (if (functionp org-groff-format-headline-function)
  818. ;; User-defined formatting function.
  819. (funcall org-groff-format-headline-function
  820. todo todo-type priority text nil)
  821. ;; Default formatting.
  822. (concat
  823. (when todo (format "\\fB%s\\fP " todo))
  824. (when priority (format " [\\#%c] " priority))
  825. text)))
  826. ;; Associate some \label to the headline for internal links.
  827. ;; (headline-label
  828. ;; (format "\\label{sec-%s}\n"
  829. ;; (mapconcat 'number-to-string
  830. ;; (org-export-get-headline-number headline info)
  831. ;; "-")))
  832. (headline-label "")
  833. (pre-blanks
  834. (make-string (org-element-property :pre-blank headline) 10)))
  835. (cond
  836. ;; Case 1: Special Tag
  837. ((member (car tags) org-groff-special-tags)
  838. (cond
  839. ((string= (car tags) "BODY") contents)
  840. ((string= (car tags) "NS")
  841. (progn
  842. (push (cons (car tags)
  843. (format ".NS \"%s\" 1 \n%s"
  844. (car (org-element-property :title headline))
  845. (or contents " ")))
  846. org-groff-special-content) nil))
  847. (t
  848. (progn
  849. (push (cons (car tags) contents) org-groff-special-content)
  850. nil))))
  851. ;; Case 2: This is a footnote section: ignore it.
  852. ((org-element-property :footnote-section-p headline) nil)
  853. ;; Case 3: This is a deep sub-tree: export it as a list item.
  854. ;; Also export as items headlines for which no section
  855. ;; format has been found.
  856. ((or (not section-fmt) (org-export-low-level-p headline info))
  857. ;; Build the real contents of the sub-tree.
  858. (let ((low-level-body
  859. (concat
  860. ;; If the headline is the first sibling, start a list.
  861. (when (org-export-first-sibling-p headline info)
  862. (format "%s\n" (if numberedp ".AL 1\n" ".DL \n")))
  863. ;; Itemize headline
  864. ".LI\n" full-text "\n" headline-label pre-blanks contents)))
  865. ;; If headline is not the last sibling simply return
  866. ;; LOW-LEVEL-BODY. Otherwise, also close the list, before any
  867. ;; blank line.
  868. (if (not (org-export-last-sibling-p headline info)) low-level-body
  869. (replace-regexp-in-string
  870. "[ \t\n]*\\'"
  871. (concat "\n.LE")
  872. low-level-body))))
  873. ;; Case 4. Standard headline. Export it as a section.
  874. (t
  875. (format section-fmt full-text
  876. (concat headline-label pre-blanks contents))))))
  877. ;;; Horizontal Rule
  878. ;; Not supported
  879. ;;; Inline Babel Call
  880. ;;
  881. ;; Inline Babel Calls are ignored.
  882. ;;; Inline Src Block
  883. (defun org-groff-inline-src-block (inline-src-block contents info)
  884. "Transcode an INLINE-SRC-BLOCK element from Org to Groff.
  885. CONTENTS holds the contents of the item. INFO is a plist holding
  886. contextual information."
  887. (let* ((code (org-element-property :value inline-src-block)))
  888. (cond
  889. (org-groff-source-highlight
  890. (let* ((tmpdir (if (featurep 'xemacs)
  891. temp-directory
  892. temporary-file-directory))
  893. (in-file (make-temp-name
  894. (expand-file-name "srchilite" tmpdir)))
  895. (out-file (make-temp-name
  896. (expand-file-name "reshilite" tmpdir)))
  897. (org-lang (org-element-property :language inline-src-block))
  898. (lst-lang (cadr (assq (intern org-lang)
  899. org-groff-source-highlight-langs)))
  900. (cmd (concat (expand-file-name "source-highlight")
  901. " -s " lst-lang
  902. " -f groff_mm_color "
  903. " -i " in-file
  904. " -o " out-file)))
  905. (if lst-lang
  906. (let ((code-block ""))
  907. (with-temp-file in-file (insert code))
  908. (shell-command cmd)
  909. (setq code-block (org-file-contents out-file))
  910. (delete-file in-file)
  911. (delete-file out-file)
  912. code-block)
  913. (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
  914. code))))
  915. ;; Do not use a special package: transcode it verbatim.
  916. (t
  917. (concat ".DS I\n" "\\fC" code "\\fP\n.DE\n")))))
  918. ;;; Inlinetask
  919. (defun org-groff-inlinetask (inlinetask contents info)
  920. "Transcode an INLINETASK element from Org to Groff.
  921. CONTENTS holds the contents of the block. INFO is a plist
  922. holding contextual information."
  923. (let ((title (org-export-data (org-element-property :title inlinetask) info))
  924. (todo (and (plist-get info :with-todo-keywords)
  925. (let ((todo (org-element-property :todo-keyword inlinetask)))
  926. (and todo (org-export-data todo info)))))
  927. (todo-type (org-element-property :todo-type inlinetask))
  928. (tags (and (plist-get info :with-tags)
  929. (org-export-get-tags inlinetask info)))
  930. (priority (and (plist-get info :with-priority)
  931. (org-element-property :priority inlinetask))))
  932. ;; If `org-groff-format-inlinetask-function' is provided, call it
  933. ;; with appropriate arguments.
  934. (if (functionp org-groff-format-inlinetask-function)
  935. (funcall org-groff-format-inlinetask-function
  936. todo todo-type priority title tags contents)
  937. ;; Otherwise, use a default template.
  938. (org-groff--wrap-label
  939. inlinetask
  940. (let ((full-title
  941. (concat
  942. (when todo (format "\\fB%s\\fP " todo))
  943. (when priority (format " [\\#%c] " priority))
  944. title
  945. (when tags (format " \\fC:%s:\\fP "
  946. (mapconcat 'identity tags ":"))))))
  947. (format (concat "\n.DS I\n"
  948. "%s\n"
  949. ".sp"
  950. "%s\n"
  951. ".DE")
  952. full-title contents))))))
  953. ;;; Italic
  954. (defun org-groff-italic (italic contents info)
  955. "Transcode ITALIC from Org to Groff.
  956. CONTENTS is the text with italic markup. INFO is a plist holding
  957. contextual information."
  958. (org-groff--text-markup contents 'italic))
  959. ;;; Item
  960. (defun org-groff-item (item contents info)
  961. "Transcode an ITEM element from Org to Groff.
  962. CONTENTS holds the contents of the item. INFO is a plist holding
  963. contextual information."
  964. (let* ((bullet (org-element-property :bullet item))
  965. (type (org-element-property
  966. :type (org-element-property :parent item)))
  967. (checkbox (case (org-element-property :checkbox item)
  968. (on "\\o'\\(sq\\(mu'")
  969. (off "\\(sq")
  970. (trans "\\o'\\(sq\\(mi'")))
  971. (tag (let ((tag (org-element-property :tag item)))
  972. ;; Check-boxes must belong to the tag.
  973. (and tag (format "%s"
  974. (concat checkbox
  975. (org-export-data tag info)))))))
  976. (cond
  977. ((or checkbox tag)
  978. (concat ".LI ""\"" (or tag (concat "\\ " checkbox)) "\""
  979. "\n"
  980. (org-trim (or contents " "))))
  981. ((eq type 'ordered)
  982. (concat ".LI"
  983. "\n"
  984. (org-trim (or contents " "))))
  985. (t
  986. (let* ((bullet (org-trim bullet))
  987. (marker (cond ((string= "-" bullet) "\\(em")
  988. ((string= "*" bullet) "\\(bu")
  989. (t "\\(dg"))))
  990. (concat ".LI " marker "\n"
  991. (org-trim (or contents " "))))))))
  992. ;;; Keyword
  993. (defun org-groff-keyword (keyword contents info)
  994. "Transcode a KEYWORD element from Org to Groff.
  995. CONTENTS is nil. INFO is a plist holding contextual information."
  996. (let ((key (org-element-property :key keyword))
  997. (value (org-element-property :value keyword)))
  998. (cond
  999. ((string= key "GROFF") value)
  1000. (t nil))))
  1001. ;;; Line Break
  1002. (defun org-groff-line-break (line-break contents info)
  1003. "Transcode a LINE-BREAK object from Org to Groff.
  1004. CONTENTS is nil. INFO is a plist holding contextual information."
  1005. ".br\n")
  1006. ;;; Link
  1007. ;; Inline images just place a call to .PSPIC or .PS/.PE
  1008. ;; and load the graph.
  1009. (defun org-groff-link--inline-image (link info)
  1010. "Return Groff code for an inline image.
  1011. LINK is the link pointing to the inline image. INFO is a plist
  1012. used as a communication channel."
  1013. (let* ((parent (org-export-get-parent-element link))
  1014. (path (let ((raw-path (org-element-property :path link)))
  1015. (if (not (file-name-absolute-p raw-path)) raw-path
  1016. (expand-file-name raw-path))))
  1017. (attr (org-export-read-attribute :attr_groff link))
  1018. (placement
  1019. (let ((pos (plist-get attr :position)))
  1020. (cond ((string= pos 'center) "")
  1021. ((string= pos 'left) "-L")
  1022. ((string= pos 'right) "-R")
  1023. (t ""))))
  1024. (width (or (plist-get attr :width) ""))
  1025. (height (or (plist-get attr :height) ""))
  1026. (caption (and (not (plist-get attr :disable-caption))
  1027. (org-groff--caption/label-string parent info))))
  1028. ;; Now clear ATTR from any special keyword and set a default value
  1029. ;; if nothing is left. Return proper string.
  1030. (concat
  1031. (cond
  1032. ((and org-groff-raster-to-ps
  1033. (or (string-match ".\.png$" path)
  1034. (string-match ".\.jpg$" path)))
  1035. (let ((eps-path (concat path ".eps")))
  1036. (shell-command (format org-groff-raster-to-ps path eps-path))
  1037. (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
  1038. placement eps-path width height)))
  1039. ((string-match ".\.pic$" path)
  1040. (format "\n.PS\ncopy \"%s\"\n.PE" path))
  1041. (t (format "\n.DS L F\n.PSPIC %s \"%s\" %s %s\n.DE "
  1042. placement path width height)))
  1043. (and caption (format "\n.FG \"%s\"" caption)))))
  1044. (defun org-groff-link (link desc info)
  1045. "Transcode a LINK object from Org to Groff.
  1046. DESC is the description part of the link, or the empty string.
  1047. INFO is a plist holding contextual information. See
  1048. `org-export-data'."
  1049. (let* ((type (org-element-property :type link))
  1050. (raw-path (org-element-property :path link))
  1051. ;; Ensure DESC really exists, or set it to nil.
  1052. (desc (and (not (string= desc "")) desc))
  1053. (imagep (org-export-inline-image-p
  1054. link org-groff-inline-image-rules))
  1055. (path (cond
  1056. ((member type '("http" "https" "ftp" "mailto"))
  1057. (concat type ":" raw-path))
  1058. ((string= type "file") (org-export-file-uri raw-path))
  1059. (t raw-path))))
  1060. (cond
  1061. ((org-export-custom-protocol-maybe link desc 'groff))
  1062. ;; Image file.
  1063. (imagep (org-groff-link--inline-image link info))
  1064. ;; import groff files
  1065. ((and (string= type "file")
  1066. (string-match ".\.groff$" raw-path))
  1067. (concat ".so " raw-path "\n"))
  1068. ;; Radio link: transcode target's contents and use them as link's
  1069. ;; description.
  1070. ((string= type "radio")
  1071. (let ((destination (org-export-resolve-radio-link link info)))
  1072. (if (not destination) desc
  1073. (format "\\fI [%s] \\fP"
  1074. (org-export-get-reference destination info)))))
  1075. ;; Links pointing to a headline: find destination and build
  1076. ;; appropriate referencing command.
  1077. ((member type '("custom-id" "fuzzy" "id"))
  1078. (let ((destination (if (string= type "fuzzy")
  1079. (org-export-resolve-fuzzy-link link info)
  1080. (org-export-resolve-id-link link info))))
  1081. (case (org-element-type destination)
  1082. ;; Id link points to an external file.
  1083. (plain-text
  1084. (if desc (format "%s \\fBat\\fP \\fIfile://%s\\fP" desc destination)
  1085. (format "\\fI file://%s \\fP" destination)))
  1086. ;; Fuzzy link points nowhere.
  1087. ('nil
  1088. (format org-groff-link-with-unknown-path-format
  1089. (or desc
  1090. (org-export-data
  1091. (org-element-property :raw-link link) info))))
  1092. ;; LINK points to a headline. If headlines are numbered and
  1093. ;; the link has no description, display headline's number.
  1094. ;; Otherwise, display description or headline's title.
  1095. (headline
  1096. (let ((label ""))
  1097. (if (and (plist-get info :section-numbers) (not desc))
  1098. (format "\\fI%s\\fP" label)
  1099. (format "\\fI%s\\fP"
  1100. (or desc
  1101. (org-export-data
  1102. (org-element-property :title destination) info))))))
  1103. ;; Fuzzy link points to a target. Do as above.
  1104. (otherwise
  1105. (let ((ref (org-export-get-reference destination info)))
  1106. (if (not desc) (format "\\fI%s\\fP" ref)
  1107. (format "%s \\fBat\\fP \\fI%s\\fP" desc ref)))))))
  1108. ;; External link with a description part.
  1109. ((and path desc) (format "%s \\fBat\\fP \\fI%s\\fP" path desc))
  1110. ;; External link without a description part.
  1111. (path (format "\\fI%s\\fP" path))
  1112. ;; No path, only description. Try to do something useful.
  1113. (t (format org-groff-link-with-unknown-path-format desc)))))
  1114. ;;; Node Property
  1115. (defun org-groff-node-property (node-property contents info)
  1116. "Transcode a NODE-PROPERTY element from Org to Groff.
  1117. CONTENTS is nil. INFO is a plist holding contextual
  1118. information."
  1119. (format "%s:%s"
  1120. (org-element-property :key node-property)
  1121. (let ((value (org-element-property :value node-property)))
  1122. (if value (concat " " value) ""))))
  1123. ;;; Paragraph
  1124. (defun org-groff-paragraph (paragraph contents info)
  1125. "Transcode a PARAGRAPH element from Org to Groff.
  1126. CONTENTS is the contents of the paragraph, as a string. INFO is
  1127. the plist used as a communication channel."
  1128. (let ((parent (plist-get (nth 1 paragraph) :parent)))
  1129. (when parent
  1130. (let* ((parent-type (car parent))
  1131. (fixed-paragraph "")
  1132. (class (plist-get info :groff-class))
  1133. (class-options (plist-get info :groff-class-options))
  1134. (classes (assoc class org-groff-classes))
  1135. (classes-options (car (last classes)))
  1136. (paragraph-option (plist-get classes-options :paragraph)))
  1137. (cond
  1138. ((and (symbolp paragraph-option)
  1139. (fboundp paragraph-option))
  1140. (funcall paragraph-option parent-type parent contents))
  1141. ((and (eq parent-type 'item)
  1142. (plist-get (nth 1 parent) :bullet))
  1143. (setq fixed-paragraph (concat "" contents)))
  1144. ((eq parent-type 'section)
  1145. (setq fixed-paragraph (concat ".P\n" contents)))
  1146. ((eq parent-type 'footnote-definition)
  1147. (setq fixed-paragraph (concat "" contents)))
  1148. (t (setq fixed-paragraph (concat "" contents))))
  1149. fixed-paragraph))))
  1150. ;;; Plain List
  1151. (defun org-groff-plain-list (plain-list contents info)
  1152. "Transcode a PLAIN-LIST element from Org to Groff.
  1153. CONTENTS is the contents of the list. INFO is a plist holding
  1154. contextual information."
  1155. (let* ((type (org-element-property :type plain-list))
  1156. (attr (mapconcat #'identity
  1157. (org-element-property :attr_groff plain-list)
  1158. " "))
  1159. (groff-type (cond
  1160. ((eq type 'ordered) ".AL")
  1161. ((eq type 'unordered) ".BL")
  1162. ((eq type 'descriptive) ".VL 2.0i"))))
  1163. (org-groff--wrap-label
  1164. plain-list
  1165. (format "%s\n%s\n.LE" groff-type contents))))
  1166. ;;; Plain Text
  1167. (defun org-groff-plain-text (text info)
  1168. "Transcode a TEXT string from Org to Groff.
  1169. TEXT is the string to transcode. INFO is a plist holding
  1170. contextual information."
  1171. (let ((output text))
  1172. ;; Protect various characters.
  1173. (setq output (replace-regexp-in-string
  1174. "\\(?:[^\\]\\|^\\)\\(\\\\\\)\\(?:[^%$#&{}~^_\\]\\|$\\)"
  1175. "$\\" output nil t 1))
  1176. ;; Activate smart quotes. Be sure to provide original TEXT string
  1177. ;; since OUTPUT may have been modified.
  1178. (when (plist-get info :with-smart-quotes)
  1179. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  1180. ;; Handle Special Characters
  1181. (if org-groff-special-char
  1182. (dolist (special-char-list org-groff-special-char)
  1183. (setq output
  1184. (replace-regexp-in-string (car special-char-list)
  1185. (cdr special-char-list) output))))
  1186. ;; Handle break preservation if required.
  1187. (when (plist-get info :preserve-breaks)
  1188. (setq output (replace-regexp-in-string
  1189. "\\(\\\\\\\\\\)?[ \t]*\n" ".br\n" output)))
  1190. ;; Return value.
  1191. output))
  1192. ;;; Planning
  1193. (defun org-groff-planning (planning contents info)
  1194. "Transcode a PLANNING element from Org to Groff.
  1195. CONTENTS is nil. INFO is a plist holding contextual
  1196. information."
  1197. (concat
  1198. (mapconcat
  1199. 'identity
  1200. (delq nil
  1201. (list
  1202. (let ((closed (org-element-property :closed planning)))
  1203. (when closed
  1204. (concat
  1205. (format "\\fR %s \\fP" org-closed-string)
  1206. (format org-groff-inactive-timestamp-format
  1207. (org-translate-time
  1208. (org-element-property :raw-value closed))))))
  1209. (let ((deadline (org-element-property :deadline planning)))
  1210. (when deadline
  1211. (concat
  1212. (format "\\fB %s \\fP" org-deadline-string)
  1213. (format org-groff-active-timestamp-format
  1214. (org-translate-time
  1215. (org-element-property :raw-value deadline))))))
  1216. (let ((scheduled (org-element-property :scheduled planning)))
  1217. (when scheduled
  1218. (concat
  1219. (format "\\fR %s \\fP" org-scheduled-string)
  1220. (format org-groff-active-timestamp-format
  1221. (org-translate-time
  1222. (org-element-property :raw-value scheduled))))))))
  1223. "")
  1224. ""))
  1225. ;;;; Property Drawer
  1226. (defun org-groff-property-drawer (property-drawer contents info)
  1227. "Transcode a PROPERTY-DRAWER element from Org to Groff.
  1228. CONTENTS holds the contents of the drawer. INFO is a plist
  1229. holding contextual information."
  1230. (and (org-string-nw-p contents)
  1231. (format "\\fC\n%s\\fP" contents)))
  1232. ;;; Quote Block
  1233. (defun org-groff-quote-block (quote-block contents info)
  1234. "Transcode a QUOTE-BLOCK element from Org to Groff.
  1235. CONTENTS holds the contents of the block. INFO is a plist
  1236. holding contextual information."
  1237. (org-groff--wrap-label
  1238. quote-block
  1239. (format ".DS I\n.I\n%s\n.R\n.DE" contents)))
  1240. ;;; Radio Target
  1241. (defun org-groff-radio-target (radio-target text info)
  1242. "Transcode a RADIO-TARGET object from Org to Groff.
  1243. TEXT is the text of the target. INFO is a plist holding
  1244. contextual information."
  1245. (format "%s - %s" (org-export-get-reference radio-target info) text))
  1246. ;;; Section
  1247. (defun org-groff-section (section contents info)
  1248. "Transcode a SECTION element from Org to Groff.
  1249. CONTENTS holds the contents of the section. INFO is a plist
  1250. holding contextual information."
  1251. contents)
  1252. ;;; Special Block
  1253. (defun org-groff-special-block (special-block contents info)
  1254. "Transcode a SPECIAL-BLOCK element from Org to Groff.
  1255. CONTENTS holds the contents of the block. INFO is a plist
  1256. holding contextual information."
  1257. (let ((type (org-element-property :type special-block)))
  1258. (org-groff--wrap-label
  1259. special-block
  1260. (format "%s\n" contents))))
  1261. ;;; Src Block
  1262. (defun org-groff-src-block (src-block contents info)
  1263. "Transcode a SRC-BLOCK element from Org to Groff.
  1264. CONTENTS holds the contents of the item. INFO is a plist holding
  1265. contextual information."
  1266. (let* ((lang (org-element-property :language src-block))
  1267. (label (org-element-property :name src-block))
  1268. (code (org-element-property :value src-block))
  1269. (custom-env (and lang
  1270. (cadr (assq (intern lang)
  1271. org-groff-custom-lang-environments))))
  1272. (num-start (case (org-element-property :number-lines src-block)
  1273. (continued (org-export-get-loc src-block info))
  1274. (new 0)))
  1275. (retain-labels (org-element-property :retain-labels src-block))
  1276. (caption (and (not (org-export-read-attribute
  1277. :attr_groff src-block :disable-caption))
  1278. (org-groff--caption/label-string src-block info))))
  1279. (cond
  1280. ;; Case 1. No source fontification.
  1281. ((not org-groff-source-highlight)
  1282. (concat
  1283. (format ".DS I\n\\fC%s\\fP\n.DE\n"
  1284. (org-export-format-code-default src-block info))
  1285. (and caption (format ".EX \"%s\" " caption))))
  1286. ;; Case 2. Source fontification.
  1287. (org-groff-source-highlight
  1288. (let* ((tmpdir (if (featurep 'xemacs)
  1289. temp-directory
  1290. temporary-file-directory))
  1291. (in-file (make-temp-name
  1292. (expand-file-name "srchilite" tmpdir)))
  1293. (out-file (make-temp-name
  1294. (expand-file-name "reshilite" tmpdir)))
  1295. (org-lang (org-element-property :language src-block))
  1296. (lst-lang (cadr (assq (intern org-lang)
  1297. org-groff-source-highlight-langs)))
  1298. (cmd (concat "source-highlight"
  1299. " -s " lst-lang
  1300. " -f groff_mm_color "
  1301. " -i " in-file
  1302. " -o " out-file)))
  1303. (concat
  1304. (if lst-lang
  1305. (let ((code-block ""))
  1306. (with-temp-file in-file (insert code))
  1307. (shell-command cmd)
  1308. (setq code-block (org-file-contents out-file))
  1309. (delete-file in-file)
  1310. (delete-file out-file)
  1311. (format "%s\n" code-block))
  1312. (format ".DS I\n\\fC\\m[black]%s\\m[]\\fP\n.DE\n"
  1313. code))
  1314. (and caption (format ".EX \"%s\" " caption))))))))
  1315. ;;; Statistics Cookie
  1316. (defun org-groff-statistics-cookie (statistics-cookie contents info)
  1317. "Transcode a STATISTICS-COOKIE object from Org to Groff.
  1318. CONTENTS is nil. INFO is a plist holding contextual information."
  1319. (org-element-property :value statistics-cookie))
  1320. ;;; Strike-Through
  1321. (defun org-groff-strike-through (strike-through contents info)
  1322. "Transcode STRIKE-THROUGH from Org to Groff.
  1323. CONTENTS is the text with strike-through markup. INFO is a plist
  1324. holding contextual information."
  1325. (org-groff--text-markup contents 'strike-through))
  1326. ;;; Subscript
  1327. (defun org-groff-subscript (subscript contents info)
  1328. "Transcode a SUBSCRIPT object from Org to Groff.
  1329. CONTENTS is the contents of the object. INFO is a plist holding
  1330. contextual information."
  1331. (format "\\d\\s-2%s\\s+2\\u" contents))
  1332. ;;; Superscript "^_%s$
  1333. (defun org-groff-superscript (superscript contents info)
  1334. "Transcode a SUPERSCRIPT object from Org to Groff.
  1335. CONTENTS is the contents of the object. INFO is a plist holding
  1336. contextual information."
  1337. (format "\\u\\s-2%s\\s+2\\d" contents))
  1338. ;;; Table
  1339. ;;
  1340. ;; `org-groff-table' is the entry point for table transcoding. It
  1341. ;; takes care of tables with a "verbatim" attribute. Otherwise, it
  1342. ;; delegates the job to `org-groff-table--org-table' function,
  1343. ;; depending of the type of the table.
  1344. ;;
  1345. ;; `org-groff-table--align-string' is a subroutine used to build
  1346. ;; alignment string for Org tables.
  1347. (defun org-groff-table (table contents info)
  1348. "Transcode a TABLE element from Org to Groff.
  1349. CONTENTS is the contents of the table. INFO is a plist holding
  1350. contextual information."
  1351. (cond
  1352. ;; Case 1: verbatim table.
  1353. ((or org-groff-tables-verbatim
  1354. (let ((attr (read (format "(%s)"
  1355. (mapconcat
  1356. #'identity
  1357. (org-element-property :attr_groff table) " ")))))
  1358. (and attr (plist-get attr :verbatim))))
  1359. (format ".DS L\n\\fC%s\\fP\n.DE"
  1360. ;; Re-create table, without affiliated keywords.
  1361. (org-trim
  1362. (org-element-interpret-data
  1363. `(table nil ,@(org-element-contents table))))))
  1364. ;; Case 2: Standard table.
  1365. (t (org-groff-table--org-table table contents info))))
  1366. (defun org-groff-table--align-string (divider table info)
  1367. "Return an appropriate Groff alignment string.
  1368. TABLE is the considered table. INFO is a plist used as
  1369. a communication channel."
  1370. (let (alignment)
  1371. ;; Extract column groups and alignment from first (non-rule) row.
  1372. (org-element-map
  1373. (org-element-map table 'table-row
  1374. (lambda (row)
  1375. (and (eq (org-element-property :type row) 'standard) row))
  1376. info 'first-match)
  1377. 'table-cell
  1378. (lambda (cell)
  1379. (let* ((borders (org-export-table-cell-borders cell info))
  1380. (raw-width (org-export-table-cell-width cell info))
  1381. (width-cm (when raw-width (/ raw-width 5)))
  1382. (width (if raw-width (format "w(%dc)"
  1383. (if (< width-cm 1) 1 width-cm)) "")))
  1384. ;; Check left border for the first cell only.
  1385. ;; Alignment is nil on assignment
  1386. (when (and (memq 'left borders) (not alignment))
  1387. (push "|" alignment))
  1388. (push
  1389. (case (org-export-table-cell-alignment cell info)
  1390. (left (concat "l" width divider))
  1391. (right (concat "r" width divider))
  1392. (center (concat "c" width divider)))
  1393. alignment)
  1394. (when (memq 'right borders) (push "|" alignment))))
  1395. info)
  1396. (apply 'concat (reverse alignment))))
  1397. (defun org-groff-table--org-table (table contents info)
  1398. "Return appropriate Groff code for an Org table.
  1399. TABLE is the table type element to transcode. CONTENTS is its
  1400. contents, as a string. INFO is a plist used as a communication
  1401. channel.
  1402. This function assumes TABLE has `org' as its `:type' attribute."
  1403. (let* ((attr (org-export-read-attribute :attr_groff table))
  1404. (label (org-element-property :name table))
  1405. (caption (and (not (plist-get attr :disable-caption))
  1406. (org-groff--caption/label-string table info)))
  1407. (divider (if (plist-get attr :divider) "|" " "))
  1408. ;; Determine alignment string.
  1409. (alignment (org-groff-table--align-string divider table info))
  1410. ;; Extract others display options.
  1411. (lines (org-split-string contents "\n"))
  1412. (attr-list
  1413. (delq nil
  1414. (list (and (plist-get attr :expand) "expand")
  1415. (let ((placement (plist-get attr :placement)))
  1416. (cond ((string= placement 'center) "center")
  1417. ((string= placement 'left) nil)
  1418. (t (if org-groff-tables-centered "center" ""))))
  1419. (or (plist-get attr :boxtype) "box"))))
  1420. (title-line (plist-get attr :title-line))
  1421. (long-cells (plist-get attr :long-cells))
  1422. (table-format
  1423. (concat
  1424. (or (car attr-list) "")
  1425. (or
  1426. (let (output-list)
  1427. (when (cdr attr-list)
  1428. (dolist (attr-item (cdr attr-list))
  1429. (setq output-list (concat output-list
  1430. (format ",%s" attr-item)))))
  1431. output-list) "")))
  1432. (first-line
  1433. (when lines (org-split-string (car lines) "\t"))))
  1434. ;; Prepare the final format string for the table.
  1435. (cond
  1436. ;; Others.
  1437. (lines
  1438. (concat ".TS\n " table-format ";\n"
  1439. (format "%s.\n"
  1440. (let ((final-line ""))
  1441. (when title-line
  1442. (dotimes (i (length first-line))
  1443. (setq final-line (concat final-line "cb" divider))))
  1444. (setq final-line (concat final-line "\n"))
  1445. (if alignment
  1446. (setq final-line (concat final-line alignment))
  1447. (dotimes (i (length first-line))
  1448. (setq final-line (concat final-line "c" divider))))
  1449. final-line))
  1450. (format "%s.TE\n"
  1451. (let ((final-line "")
  1452. (long-line "")
  1453. (lines (org-split-string contents "\n")))
  1454. (dolist (line-item lines)
  1455. (setq long-line "")
  1456. (if long-cells
  1457. (progn
  1458. (if (string= line-item "_")
  1459. (setq long-line (format "%s\n" line-item))
  1460. ;; else string =
  1461. (let ((cell-item-list (org-split-string line-item "\t")))
  1462. (dolist (cell-item cell-item-list)
  1463. (cond ((eq cell-item (car (last cell-item-list)))
  1464. (setq long-line (concat long-line
  1465. (format "T{\n%s\nT}\t\n" cell-item))))
  1466. (t
  1467. (setq long-line (concat long-line
  1468. (format "T{\n%s\nT}\t" cell-item))))))
  1469. long-line))
  1470. ;; else long cells
  1471. (setq final-line (concat final-line long-line)))
  1472. (setq final-line (concat final-line line-item "\n"))))
  1473. final-line))
  1474. (if caption (format ".TB \"%s\"" caption) ""))))))
  1475. ;;; Table Cell
  1476. (defun org-groff-table-cell (table-cell contents info)
  1477. "Transcode a TABLE-CELL element from Org to Groff
  1478. CONTENTS is the cell contents. INFO is a plist used as
  1479. a communication channel."
  1480. (progn
  1481. (concat (if (and contents
  1482. org-groff-table-scientific-notation
  1483. (string-match orgtbl-exp-regexp contents))
  1484. ;; Use appropriate format string for scientific
  1485. ;; notation.
  1486. (format org-groff-table-scientific-notation
  1487. (match-string 1 contents)
  1488. (match-string 2 contents))
  1489. contents)
  1490. (when (org-export-get-next-element table-cell info) "\t"))))
  1491. ;;; Table Row
  1492. (defun org-groff-table-row (table-row contents info)
  1493. "Transcode a TABLE-ROW element from Org to Groff
  1494. CONTENTS is the contents of the row. INFO is a plist used as
  1495. a communication channel."
  1496. ;; Rules are ignored since table separators are deduced from
  1497. ;; borders of the current row.
  1498. (when (eq (org-element-property :type table-row) 'standard)
  1499. (let* ((attr (mapconcat 'identity
  1500. (org-element-property
  1501. :attr_groff (org-export-get-parent table-row))
  1502. " "))
  1503. ;; TABLE-ROW's borders are extracted from its first cell.
  1504. (borders
  1505. (org-export-table-cell-borders
  1506. (car (org-element-contents table-row)) info)))
  1507. (concat
  1508. ;; Mark horizontal lines
  1509. (cond ((and (memq 'top borders) (memq 'above borders)) "_\n"))
  1510. contents
  1511. (cond
  1512. ;; When BOOKTABS are activated enforce bottom rule even when
  1513. ;; no hline was specifically marked.
  1514. ((and (memq 'bottom borders) (memq 'below borders)) "\n_")
  1515. ((memq 'below borders) "\n_"))))))
  1516. ;;; Target
  1517. (defun org-groff-target (target contents info)
  1518. "Transcode a TARGET object from Org to Groff.
  1519. CONTENTS is nil. INFO is a plist holding contextual
  1520. information."
  1521. (format "\\fI%s\\fP" (org-export-get-reference target info)))
  1522. ;;; Timestamp
  1523. (defun org-groff-timestamp (timestamp contents info)
  1524. "Transcode a TIMESTAMP object from Org to Groff.
  1525. CONTENTS is nil. INFO is a plist holding contextual
  1526. information."
  1527. (let ((value (org-groff-plain-text
  1528. (org-timestamp-translate timestamp) info)))
  1529. (case (org-element-property :type timestamp)
  1530. ((active active-range)
  1531. (format org-groff-active-timestamp-format value))
  1532. ((inactive inactive-range)
  1533. (format org-groff-inactive-timestamp-format value))
  1534. (t (format org-groff-diary-timestamp-format value)))))
  1535. ;;; Underline
  1536. (defun org-groff-underline (underline contents info)
  1537. "Transcode UNDERLINE from Org to Groff.
  1538. CONTENTS is the text with underline markup. INFO is a plist
  1539. holding contextual information."
  1540. (org-groff--text-markup contents 'underline))
  1541. ;;; Verbatim
  1542. (defun org-groff-verbatim (verbatim contents info)
  1543. "Transcode a VERBATIM object from Org to Groff.
  1544. CONTENTS is nil. INFO is a plist used as a communication
  1545. channel."
  1546. (org-groff--text-markup (org-element-property :value verbatim) 'verbatim))
  1547. ;;; Verse Block
  1548. (defun org-groff-verse-block (verse-block contents info)
  1549. "Transcode a VERSE-BLOCK element from Org to Groff.
  1550. CONTENTS is verse block contents. INFO is a plist holding
  1551. contextual information."
  1552. (format ".DS C\n.ft HI\n%s\n.ft\n.DE" contents))
  1553. ;;; Interactive functions
  1554. (defun org-groff-export-to-groff
  1555. (&optional async subtreep visible-only body-only ext-plist)
  1556. "Export current buffer to a Groff file.
  1557. If narrowing is active in the current buffer, only export its
  1558. narrowed part.
  1559. If a region is active, export that region.
  1560. A non-nil optional argument ASYNC means the process should happen
  1561. asynchronously. The resulting file should be accessible through
  1562. the `org-export-stack' interface.
  1563. When optional argument SUBTREEP is non-nil, export the sub-tree
  1564. at point, extracting information from the headline properties
  1565. first.
  1566. When optional argument VISIBLE-ONLY is non-nil, don't export
  1567. contents of hidden elements.
  1568. EXT-PLIST, when provided, is a property list with external
  1569. parameters overriding Org default settings, but still inferior to
  1570. file-local settings.
  1571. Return output file's name."
  1572. (interactive)
  1573. (let ((outfile (org-export-output-file-name ".groff" subtreep))
  1574. (org-groff-registered-references nil)
  1575. (org-groff-special-content nil))
  1576. (org-export-to-file 'groff outfile
  1577. async subtreep visible-only body-only ext-plist)))
  1578. (defun org-groff-export-to-pdf
  1579. (&optional async subtreep visible-only body-only ext-plist)
  1580. "Export current buffer to Groff then process through to PDF.
  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 PDF file's name."
  1596. (interactive)
  1597. (let ((outfile (org-export-output-file-name ".groff" subtreep)))
  1598. (org-export-to-file 'groff outfile
  1599. async subtreep visible-only body-only ext-plist
  1600. (lambda (file) (org-groff-compile file)))))
  1601. (defun org-groff-compile (file)
  1602. "Compile a Groff file.
  1603. FILE is the name of the file being compiled. Processing is done
  1604. through the command specified in `org-groff-pdf-process'.
  1605. Return PDF file name or an error if it couldn't be produced."
  1606. (let* ((base-name (file-name-sans-extension (file-name-nondirectory file)))
  1607. (full-name (file-truename file))
  1608. (out-dir (file-name-directory file))
  1609. ;; Properly set working directory for compilation.
  1610. (default-directory (if (file-name-absolute-p file)
  1611. (file-name-directory full-name)
  1612. default-directory))
  1613. errors)
  1614. (message (format "Processing Groff file %s ..." file))
  1615. (save-window-excursion
  1616. (cond
  1617. ;; A function is provided: Apply it.
  1618. ((functionp org-groff-pdf-process)
  1619. (funcall org-groff-pdf-process (shell-quote-argument file)))
  1620. ;; A list is provided: Replace %b, %f and %o with appropriate
  1621. ;; values in each command before applying it. Output is
  1622. ;; redirected to "*Org PDF Groff Output*" buffer.
  1623. ((consp org-groff-pdf-process)
  1624. (let ((outbuf (get-buffer-create "*Org PDF Groff Output*")))
  1625. (mapc
  1626. (lambda (command)
  1627. (shell-command
  1628. (replace-regexp-in-string
  1629. "%b" (shell-quote-argument base-name)
  1630. (replace-regexp-in-string
  1631. "%f" (shell-quote-argument full-name)
  1632. (replace-regexp-in-string
  1633. "%o" (shell-quote-argument out-dir) command t t)
  1634. t t) t t)
  1635. outbuf))
  1636. org-groff-pdf-process)
  1637. ;; Collect standard errors from output buffer.
  1638. (setq errors (org-groff-collect-errors outbuf))))
  1639. (t (error "No valid command to process to PDF")))
  1640. (let ((pdffile (concat out-dir base-name ".pdf")))
  1641. ;; Check for process failure. Provide collected errors if
  1642. ;; possible.
  1643. (if (not (file-exists-p pdffile))
  1644. (error (concat (format "PDF file %s wasn't produced" pdffile)
  1645. (when errors (concat ": " errors))))
  1646. ;; Else remove log files, when specified, and signal end of
  1647. ;; process to user, along with any error encountered.
  1648. (when org-groff-remove-logfiles
  1649. (dolist (ext org-groff-logfiles-extensions)
  1650. (let ((file (concat out-dir base-name "." ext)))
  1651. (when (file-exists-p file) (delete-file file)))))
  1652. (message (concat "Process completed"
  1653. (if (not errors) "."
  1654. (concat " with errors: " errors)))))
  1655. ;; Return output file name.
  1656. pdffile))))
  1657. (defun org-groff-collect-errors (buffer)
  1658. "Collect some kind of errors from \"groff\" output
  1659. BUFFER is the buffer containing output.
  1660. Return collected error types as a string, or nil if there was
  1661. none."
  1662. (with-current-buffer buffer
  1663. (save-excursion
  1664. (goto-char (point-max))
  1665. ;; Find final run
  1666. nil)))
  1667. (provide 'ox-groff)
  1668. ;;; ox-groff.el ends here