ox-groff.el 69 KB

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