ox-groff.el 69 KB

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