ox-groff.el 69 KB

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