ox-groff.el 70 KB

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