ox-groff.el 70 KB

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