org-e-odt.el 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  1. ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is not part of GNU Emacs.
  7. ;; GNU Emacs 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. ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl)
  21. (require 'table))
  22. (require 'format-spec)
  23. (require 'org-export)
  24. ;;; Define Back-End
  25. (org-export-define-backend e-odt
  26. ((bold . org-e-odt-bold)
  27. (center-block . org-e-odt-center-block)
  28. (clock . org-e-odt-clock)
  29. (code . org-e-odt-code)
  30. (drawer . org-e-odt-drawer)
  31. (dynamic-block . org-e-odt-dynamic-block)
  32. (entity . org-e-odt-entity)
  33. (example-block . org-e-odt-example-block)
  34. (export-block . org-e-odt-export-block)
  35. (export-snippet . org-e-odt-export-snippet)
  36. (fixed-width . org-e-odt-fixed-width)
  37. (footnote-definition . org-e-odt-footnote-definition)
  38. (footnote-reference . org-e-odt-footnote-reference)
  39. (headline . org-e-odt-headline)
  40. (horizontal-rule . org-e-odt-horizontal-rule)
  41. (inline-src-block . org-e-odt-inline-src-block)
  42. (inlinetask . org-e-odt-inlinetask)
  43. (italic . org-e-odt-italic)
  44. (item . org-e-odt-item)
  45. (keyword . org-e-odt-keyword)
  46. (latex-environment . org-e-odt-latex-environment)
  47. (latex-fragment . org-e-odt-latex-fragment)
  48. (line-break . org-e-odt-line-break)
  49. (link . org-e-odt-link)
  50. (macro . org-e-odt-macro)
  51. (paragraph . org-e-odt-paragraph)
  52. (plain-list . org-e-odt-plain-list)
  53. (plain-text . org-e-odt-plain-text)
  54. (planning . org-e-odt-planning)
  55. (property-drawer . org-e-odt-property-drawer)
  56. (quote-block . org-e-odt-quote-block)
  57. (quote-section . org-e-odt-quote-section)
  58. (radio-target . org-e-odt-radio-target)
  59. (section . org-e-odt-section)
  60. (special-block . org-e-odt-special-block)
  61. (src-block . org-e-odt-src-block)
  62. (statistics-cookie . org-e-odt-statistics-cookie)
  63. (strike-through . org-e-odt-strike-through)
  64. (subscript . org-e-odt-subscript)
  65. (superscript . org-e-odt-superscript)
  66. (table . org-e-odt-table)
  67. (table-cell . org-e-odt-table-cell)
  68. (table-row . org-e-odt-table-row)
  69. (target . org-e-odt-target)
  70. (template . org-e-odt-template)
  71. (timestamp . org-e-odt-timestamp)
  72. (underline . org-e-odt-underline)
  73. (verbatim . org-e-odt-verbatim)
  74. (verse-block . org-e-odt-verse-block))
  75. :export-block "ODT"
  76. :options-alist
  77. ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
  78. (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
  79. ;;; Dependencies
  80. ;;; Hooks
  81. ;;; Function Declarations
  82. (declare-function org-id-find-id-file "org-id" (id))
  83. (declare-function hfy-face-to-style "htmlfontify" (fn))
  84. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  85. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  86. (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
  87. (declare-function browse-url-file-url "browse-url" (file))
  88. ;;; Internal Variables
  89. (defconst org-e-odt-special-string-regexps
  90. '(("\\\\-" . "&#x00ad;\\1") ; shy
  91. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  92. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  93. ("\\.\\.\\." . "&#x2026;")) ; hellip
  94. "Regular expressions for special string conversion.")
  95. (defconst org-e-odt-styles-dir
  96. (let* ((d (expand-file-name (org-find-library-dir "org-e-odt.el")))
  97. (styles-dir-list (list
  98. (concat d "../../etc/styles/") ;; Git
  99. (concat d "../../etc/org/") ;; system
  100. (concat d "../etc/org/"))) ;; ELPA org-plus
  101. styles-dir)
  102. (setq styles-dir
  103. (car
  104. (delq nil
  105. (mapcar (lambda (sd)
  106. (when (and (file-readable-p
  107. (expand-file-name
  108. "OrgOdtContentTemplate.xml" sd))
  109. (file-readable-p
  110. (expand-file-name
  111. "OrgOdtStyles.xml" sd)))
  112. sd))
  113. styles-dir-list))))
  114. (or styles-dir
  115. (error "Cannot find factory styles files for Org ODT, aborting")))
  116. "Directory that holds auxiliary XML files used by the ODT exporter.
  117. This directory contains the following XML files -
  118. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\".
  119. These XML files are used as the default values of
  120. `org-export-odt-styles-file' and
  121. `org-export-odt-content-template-file'.")
  122. (defconst org-e-odt-bookmark-prefix "OrgXref.")
  123. (defconst org-e-odt-manifest-file-entry-tag
  124. "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  125. (defconst org-e-odt-file-extensions
  126. '(("odt" . "OpenDocument Text")
  127. ("ott" . "OpenDocument Text Template")
  128. ("odm" . "OpenDocument Master Document")
  129. ("ods" . "OpenDocument Spreadsheet")
  130. ("ots" . "OpenDocument Spreadsheet Template")
  131. ("odg" . "OpenDocument Drawing (Graphics)")
  132. ("otg" . "OpenDocument Drawing Template")
  133. ("odp" . "OpenDocument Presentation")
  134. ("otp" . "OpenDocument Presentation Template")
  135. ("odi" . "OpenDocument Image")
  136. ("odf" . "OpenDocument Formula")
  137. ("odc" . "OpenDocument Chart")))
  138. (defvar org-e-odt-table-style-format
  139. "
  140. <style:style style:name=\"%s\" style:family=\"table\">
  141. <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
  142. </style:style>
  143. "
  144. "Template for auto-generated Table styles.")
  145. (defvar org-e-odt-automatic-styles '()
  146. "Registry of automatic styles for various OBJECT-TYPEs.
  147. The variable has the following form:
  148. \(\(OBJECT-TYPE-A
  149. \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
  150. \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
  151. \(OBJECT-TYPE-B
  152. \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
  153. \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
  154. ...\).
  155. OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
  156. OBJECT-PROPS is (typically) a plist created by passing
  157. \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
  158. Use `org-e-odt-add-automatic-style' to add update this variable.'")
  159. (defvar org-e-odt-object-counters nil
  160. "Running counters for various OBJECT-TYPEs.
  161. Use this to generate automatic names and style-names. See
  162. `org-e-odt-add-automatic-style'.")
  163. (defvar org-e-odt-src-block-paragraph-format
  164. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  165. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  166. <style:background-image/>
  167. </style:paragraph-properties>
  168. <style:text-properties fo:color=\"%s\"/>
  169. </style:style>"
  170. "Custom paragraph style for colorized source and example blocks.
  171. This style is much the same as that of \"OrgFixedWidthBlock\"
  172. except that the foreground and background colors are set
  173. according to the default face identified by the `htmlfontify'.")
  174. (defvar hfy-optimisations)
  175. (defvar org-e-odt-embedded-formulas-count 0)
  176. (defvar org-e-odt-entity-frame-styles
  177. '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
  178. ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
  179. ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
  180. ("CaptionedAs-CharImage" "__Figure__"
  181. ("OrgCaptionedImage"
  182. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  183. ("OrgInlineImage" nil "as-char"))
  184. ("CaptionedParagraphImage" "__Figure__"
  185. ("OrgCaptionedImage"
  186. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  187. ("OrgImageCaptionFrame" nil "paragraph"))
  188. ("CaptionedPageImage" "__Figure__"
  189. ("OrgCaptionedImage"
  190. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  191. ("OrgPageImageCaptionFrame" nil "page"))
  192. ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
  193. ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
  194. ("CaptionedDisplayFormula" "__MathFormula__"
  195. ("OrgCaptionedFormula" nil "paragraph")
  196. ("OrgFormulaCaptionFrame" nil "as-char"))))
  197. (defvar org-e-odt-embedded-images-count 0)
  198. (defvar org-e-odt-image-size-probe-method
  199. (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
  200. '(emacs fixed))
  201. "Ordered list of methods for determining image sizes.")
  202. (defvar org-e-odt-default-image-sizes-alist
  203. '(("as-char" . (5 . 0.4))
  204. ("paragraph" . (5 . 5)))
  205. "Hardcoded image dimensions one for each of the anchor
  206. methods.")
  207. ;; A4 page size is 21.0 by 29.7 cms
  208. ;; The default page settings has 2cm margin on each of the sides. So
  209. ;; the effective text area is 17.0 by 25.7 cm
  210. (defvar org-e-odt-max-image-size '(17.0 . 20.0)
  211. "Limiting dimensions for an embedded image.")
  212. (defvar org-e-odt-label-styles
  213. '(("math-formula" "%c" "text" "(%n)")
  214. ("math-label" "(%n)" "text" "(%n)")
  215. ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
  216. ("value" "%e %n: %c" "value" "%n"))
  217. "Specify how labels are applied and referenced.
  218. This is an alist where each element is of the
  219. form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
  220. LABEL-REF-FMT).
  221. LABEL-ATTACH-FMT controls how labels and captions are attached to
  222. an entity. It may contain following specifiers - %e, %n and %c.
  223. %e is replaced with the CATEGORY-NAME. %n is replaced with
  224. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  225. with CAPTION. See `org-e-odt-format-label-definition'.
  226. LABEL-REF-MODE and LABEL-REF-FMT controls how label references
  227. are generated. The following XML is generated for a label
  228. reference - \"<text:sequence-ref
  229. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  230. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  231. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  232. %n is replaced with SEQNO. See
  233. `org-e-odt-format-label-reference'.")
  234. (defvar org-e-odt-category-map-alist
  235. '(("__Table__" "Table" "value" "Table")
  236. ("__Figure__" "Illustration" "value" "Figure")
  237. ("__MathFormula__" "Text" "math-formula" "Equation")
  238. ("__DvipngImage__" "Equation" "value" "Equation")
  239. ("__Listing__" "Listing" "value" "Listing")
  240. ;; ("__Table__" "Table" "category-and-value")
  241. ;; ("__Figure__" "Figure" "category-and-value")
  242. ;; ("__DvipngImage__" "Equation" "category-and-value")
  243. )
  244. "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
  245. This is a list where each entry is of the form \\(CATEGORY-HANDLE
  246. OD-VARIABLE LABEL-STYLE CATEGORY-NAME\\). CATEGORY_HANDLE
  247. identifies the captionable entity in question. OD-VARIABLE is
  248. the OpenDocument sequence counter associated with the entity.
  249. These counters are declared within
  250. \"<text:sequence-decls>...</text:sequence-decls>\" block of
  251. `org-e-odt-content-template-file'. LABEL-STYLE is a key into
  252. `org-e-odt-label-styles' and specifies how a given entity should
  253. be captioned and referenced. CATEGORY-NAME is used for
  254. qualifying captions on export. You can modify the CATEGORY-NAME
  255. used in the exported document by modifying
  256. `org-export-dictionary'. For example, an embedded image in an
  257. English document is captioned as \"Figure 1: Orgmode Logo\", by
  258. default. If you want the image to be captioned as \"Illustration
  259. 1: Orgmode Logo\" instead, install an entry in
  260. `org-export-dictionary' which translates \"Figure\" to
  261. \"Illustration\" when the language is \"en\" and encoding is
  262. `:utf-8'.")
  263. (defvar org-e-odt-manifest-file-entries nil)
  264. (defvar hfy-user-sheet-assoc)
  265. (defvar org-e-odt-zip-dir nil
  266. "Temporary work directory for OpenDocument exporter.")
  267. ;;; User Configuration Variables
  268. (defgroup org-export-e-odt nil
  269. "Options for exporting Org mode files to ODT."
  270. :tag "Org Export ODT"
  271. :group 'org-export)
  272. ;;;; Debugging
  273. (defcustom org-e-odt-prettify-xml nil
  274. "Specify whether or not the xml output should be prettified.
  275. When this option is turned on, `indent-region' is run on all
  276. component xml buffers before they are saved. Turn this off for
  277. regular use. Turn this on if you need to examine the xml
  278. visually."
  279. :group 'org-export-e-odt
  280. :version "24.1"
  281. :type 'boolean)
  282. ;;;; Document schema
  283. (defcustom org-e-odt-schema-dir
  284. (let* ((d (expand-file-name (org-find-library-dir "org-e-odt.el")))
  285. (schema-dir-list (list (concat d "../etc/schema/"))) ;; ELPA and Git
  286. schema-dir)
  287. (setq schema-dir
  288. (car
  289. (delq nil
  290. (mapcar (lambda (sd)
  291. (when (and (file-readable-p
  292. (expand-file-name
  293. "od-manifest-schema-v1.2-cs01.rnc" sd))
  294. (file-readable-p
  295. (expand-file-name
  296. "od-schema-v1.2-cs01.rnc" sd))
  297. (file-readable-p
  298. (expand-file-name
  299. "schemas.xml" sd)))
  300. sd))
  301. schema-dir-list))))
  302. (or schema-dir
  303. (message "No OpenDocument schema files installed")))
  304. "Directory that contains OpenDocument schema files.
  305. This directory contains:
  306. 1. rnc files for OpenDocument schema
  307. 2. a \"schemas.xml\" file that specifies locating rules needed
  308. for auto validation of OpenDocument XML files.
  309. Use the customize interface to set this variable. This ensures
  310. that `rng-schema-locating-files' is updated and auto-validation
  311. of OpenDocument XML takes place based on the value
  312. `rng-nxml-auto-validate-flag'.
  313. The OASIS schema files are available only in the Org's private
  314. git repository. It is *not* bundled with GNU ELPA tar or
  315. standard Emacs distribution."
  316. :type '(choice
  317. (const :tag "Not set" nil)
  318. (directory :tag "Schema directory"))
  319. :group 'org-e-odt
  320. :version "24.1"
  321. :set
  322. (lambda (var value)
  323. "Set `org-e-odt-schema-dir'.
  324. Also add it to `rng-schema-locating-files'."
  325. (let ((schema-dir value))
  326. (set var
  327. (if (and
  328. (file-readable-p
  329. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
  330. (file-readable-p
  331. (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
  332. (file-readable-p
  333. (expand-file-name "schemas.xml" schema-dir)))
  334. schema-dir
  335. (when value
  336. (message "No OpenDocument schema files" value))
  337. nil)))
  338. (when org-e-odt-schema-dir
  339. (eval-after-load 'rng-loc
  340. '(add-to-list 'rng-schema-locating-files
  341. (expand-file-name "schemas.xml"
  342. org-e-odt-schema-dir))))))
  343. ;;;; Document styles
  344. (defcustom org-e-odt-content-template-file nil
  345. "Template file for \"content.xml\".
  346. The exporter embeds the exported content just before
  347. \"</office:text>\" element.
  348. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  349. under `org-e-odt-styles-dir' is used."
  350. :type 'file
  351. :group 'org-export-e-odt
  352. :version "24.1")
  353. (defcustom org-e-odt-styles-file nil
  354. "Default styles file for use with ODT export.
  355. Valid values are one of:
  356. 1. nil
  357. 2. path to a styles.xml file
  358. 3. path to a *.odt or a *.ott file
  359. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  360. ...))
  361. In case of option 1, an in-built styles.xml is used. See
  362. `org-e-odt-styles-dir' for more information.
  363. In case of option 3, the specified file is unzipped and the
  364. styles.xml embedded therein is used.
  365. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  366. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  367. generated odt file. Use relative path for specifying the
  368. FILE-MEMBERS. styles.xml must be specified as one of the
  369. FILE-MEMBERS.
  370. Use options 1, 2 or 3 only if styles.xml alone suffices for
  371. achieving the desired formatting. Use option 4, if the styles.xml
  372. references additional files like header and footer images for
  373. achieving the desired formatting.
  374. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  375. a per-file basis. For example,
  376. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  377. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  378. :group 'org-export-e-odt
  379. :version "24.1"
  380. :type
  381. '(choice
  382. (const :tag "Factory settings" nil)
  383. (file :must-match t :tag "styles.xml")
  384. (file :must-match t :tag "ODT or OTT file")
  385. (list :tag "ODT or OTT file + Members"
  386. (file :must-match t :tag "ODF Text or Text Template file")
  387. (cons :tag "Members"
  388. (file :tag " Member" "styles.xml")
  389. (repeat (file :tag "Member"))))))
  390. (defcustom org-e-odt-display-outline-level 2
  391. "Outline levels considered for enumerating captioned entities."
  392. :group 'org-export-e-odt
  393. :version "24.2"
  394. :type 'integer)
  395. ;;;; Document conversion
  396. (defcustom org-e-odt-convert-processes
  397. '(("LibreOffice"
  398. "soffice --headless --convert-to %f%x --outdir %d %i")
  399. ("unoconv"
  400. "unoconv -f %f -o %d %i"))
  401. "Specify a list of document converters and their usage.
  402. The converters in this list are offered as choices while
  403. customizing `org-e-odt-convert-process'.
  404. This variable is a list where each element is of the
  405. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  406. of the converter. CONVERTER-CMD is the shell command for the
  407. converter and can contain format specifiers. These format
  408. specifiers are interpreted as below:
  409. %i input file name in full
  410. %I input file name as a URL
  411. %f format of the output file
  412. %o output file name in full
  413. %O output file name as a URL
  414. %d output dir in full
  415. %D output dir as a URL.
  416. %x extra options as set in `org-e-odt-convert-capabilities'."
  417. :group 'org-export-e-odt
  418. :version "24.1"
  419. :type
  420. '(choice
  421. (const :tag "None" nil)
  422. (alist :tag "Converters"
  423. :key-type (string :tag "Converter Name")
  424. :value-type (group (string :tag "Command line")))))
  425. (defcustom org-e-odt-convert-process "LibreOffice"
  426. "Use this converter to convert from \"odt\" format to other formats.
  427. During customization, the list of converter names are populated
  428. from `org-e-odt-convert-processes'."
  429. :group 'org-export-e-odt
  430. :version "24.1"
  431. :type '(choice :convert-widget
  432. (lambda (w)
  433. (apply 'widget-convert (widget-type w)
  434. (eval (car (widget-get w :args)))))
  435. `((const :tag "None" nil)
  436. ,@(mapcar (lambda (c)
  437. `(const :tag ,(car c) ,(car c)))
  438. org-e-odt-convert-processes))))
  439. (defcustom org-e-odt-convert-capabilities
  440. '(("Text"
  441. ("odt" "ott" "doc" "rtf" "docx")
  442. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  443. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  444. ("Web"
  445. ("html")
  446. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  447. ("Spreadsheet"
  448. ("ods" "ots" "xls" "csv" "xlsx")
  449. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  450. ("xls" "xls") ("xlsx" "xlsx")))
  451. ("Presentation"
  452. ("odp" "otp" "ppt" "pptx")
  453. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  454. ("pptx" "pptx") ("odg" "odg"))))
  455. "Specify input and output formats of `org-e-odt-convert-process'.
  456. More correctly, specify the set of input and output formats that
  457. the user is actually interested in.
  458. This variable is an alist where each element is of the
  459. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  460. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  461. alist where each element is of the form (OUTPUT-FMT
  462. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  463. The variable is interpreted as follows:
  464. `org-e-odt-convert-process' can take any document that is in
  465. INPUT-FMT-LIST and produce any document that is in the
  466. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  467. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  468. serves dual purposes:
  469. - It is used for populating completion candidates during
  470. `org-e-odt-convert' commands.
  471. - It is used as the value of \"%f\" specifier in
  472. `org-e-odt-convert-process'.
  473. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  474. `org-e-odt-convert-process'.
  475. DOCUMENT-CLASS is used to group a set of file formats in
  476. INPUT-FMT-LIST in to a single class.
  477. Note that this variable inherently captures how LibreOffice based
  478. converters work. LibreOffice maps documents of various formats
  479. to classes like Text, Web, Spreadsheet, Presentation etc and
  480. allow document of a given class (irrespective of it's source
  481. format) to be converted to any of the export formats associated
  482. with that class.
  483. See default setting of this variable for an typical
  484. configuration."
  485. :group 'org-export-e-odt
  486. :version "24.1"
  487. :type
  488. '(choice
  489. (const :tag "None" nil)
  490. (alist :tag "Capabilities"
  491. :key-type (string :tag "Document Class")
  492. :value-type
  493. (group (repeat :tag "Input formats" (string :tag "Input format"))
  494. (alist :tag "Output formats"
  495. :key-type (string :tag "Output format")
  496. :value-type
  497. (group (string :tag "Output file extension")
  498. (choice
  499. (const :tag "None" nil)
  500. (string :tag "Extra options"))))))))
  501. (defcustom org-e-odt-preferred-output-format nil
  502. "Automatically post-process to this format after exporting to \"odt\".
  503. Interactive commands `org-export-as-e-odt' and
  504. `org-export-as-e-odt-and-open' export first to \"odt\" format and
  505. then use `org-e-odt-convert-process' to convert the
  506. resulting document to this format. During customization of this
  507. variable, the list of valid values are populated based on
  508. `org-e-odt-convert-capabilities'."
  509. :group 'org-export-e-odt
  510. :version "24.1"
  511. :type '(choice :convert-widget
  512. (lambda (w)
  513. (apply 'widget-convert (widget-type w)
  514. (eval (car (widget-get w :args)))))
  515. `((const :tag "None" nil)
  516. ,@(mapcar (lambda (c)
  517. `(const :tag ,c ,c))
  518. (org-e-odt-reachable-formats "odt")))))
  519. ;;;; Drawers
  520. (defcustom org-e-odt-format-drawer-function nil
  521. "Function called to format a drawer in HTML code.
  522. The function must accept two parameters:
  523. NAME the drawer name, like \"LOGBOOK\"
  524. CONTENTS the contents of the drawer.
  525. The function should return the string to be exported.
  526. For example, the variable could be set to the following function
  527. in order to mimic default behaviour:
  528. \(defun org-e-odt-format-drawer-default \(name contents\)
  529. \"Format a drawer element for HTML export.\"
  530. contents\)"
  531. :group 'org-export-e-odt
  532. :type 'function)
  533. ;;;; Headline
  534. (defcustom org-e-odt-format-headline-function nil
  535. "Function to format headline text.
  536. This function will be called with 5 arguments:
  537. TODO the todo keyword \(string or nil\).
  538. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  539. PRIORITY the priority of the headline \(integer or nil\)
  540. TEXT the main headline text \(string\).
  541. TAGS the tags string, separated with colons \(string or nil\).
  542. The function result will be used in the section format string.
  543. As an example, one could set the variable to the following, in
  544. order to reproduce the default set-up:
  545. \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
  546. \"Default format function for an headline.\"
  547. \(concat \(when todo
  548. \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  549. \(when priority
  550. \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  551. text
  552. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  553. :group 'org-export-e-odt
  554. :type 'function)
  555. ;;;; Inlinetasks
  556. (defcustom org-e-odt-format-inlinetask-function nil
  557. "Function called to format an inlinetask in HTML code.
  558. The function must accept six parameters:
  559. TODO the todo keyword, as a string
  560. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  561. PRIORITY the inlinetask priority, as a string
  562. NAME the inlinetask name, as a string.
  563. TAGS the inlinetask tags, as a string.
  564. CONTENTS the contents of the inlinetask, as a string.
  565. The function should return the string to be exported.
  566. For example, the variable could be set to the following function
  567. in order to mimic default behaviour:
  568. \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
  569. \"Format an inline task element for HTML export.\"
  570. \(let \(\(full-title
  571. \(concat
  572. \(when todo
  573. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  574. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  575. title
  576. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  577. \(format \(concat \"\\\\begin{center}\\n\"
  578. \"\\\\fbox{\\n\"
  579. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  580. \"%s\\n\\n\"
  581. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  582. \"%s\"
  583. \"\\\\end{minipage}}\"
  584. \"\\\\end{center}\"\)
  585. full-title contents\)\)"
  586. :group 'org-export-e-odt
  587. :type 'function)
  588. ;;;; Links
  589. (defcustom org-e-odt-inline-image-rules
  590. '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
  591. "Rules characterizing image files that can be inlined into HTML.
  592. A rule consists in an association whose key is the type of link
  593. to consider, and value is a regexp that will be matched against
  594. link's path.
  595. Note that, by default, the image extension *actually* allowed
  596. depend on the way the HTML file is processed. When used with
  597. pdflatex, pdf, jpg and png images are OK. When processing
  598. through dvi to Postscript, only ps and eps are allowed. The
  599. default we use here encompasses both."
  600. :group 'org-export-e-odt
  601. :type '(alist :key-type (string :tag "Type")
  602. :value-type (regexp :tag "Path")))
  603. (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
  604. "Scaling factor for converting images pixels to inches.
  605. Use this for sizing of embedded images. See Info node `(org)
  606. Images in ODT export' for more information."
  607. :type 'float
  608. :group 'org-export-e-odt
  609. :version "24.1")
  610. ;;;; Plain text
  611. (defcustom org-e-odt-quotes
  612. '(("fr"
  613. ("\\(\\s-\\|[[(]\\|^\\)\"" . "« ")
  614. ("\\(\\S-\\)\"" . "» ")
  615. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  616. ("en"
  617. ("\\(\\s-\\|[[(]\\|^\\)\"" . "“")
  618. ("\\(\\S-\\)\"" . "”")
  619. ("\\(\\s-\\|(\\|^\\)'" . "‘")
  620. ("\\(\\S-\\)'" . "’")))
  621. "Alist for quotes to use when converting english double-quotes.
  622. The CAR of each item in this alist is the language code.
  623. The CDR of each item in this alist is a list of three CONS:
  624. - the first CONS defines the opening quote;
  625. - the second CONS defines the closing quote;
  626. - the last CONS defines single quotes.
  627. For each item in a CONS, the first string is a regexp
  628. for allowed characters before/after the quote, the second
  629. string defines the replacement string for this quote."
  630. :group 'org-export-e-odt
  631. :type '(list
  632. (cons :tag "Opening quote"
  633. (string :tag "Regexp for char before")
  634. (string :tag "Replacement quote "))
  635. (cons :tag "Closing quote"
  636. (string :tag "Regexp for char after ")
  637. (string :tag "Replacement quote "))
  638. (cons :tag "Single quote"
  639. (string :tag "Regexp for char before")
  640. (string :tag "Replacement quote "))))
  641. ;;;; Src Block
  642. (defcustom org-e-odt-create-custom-styles-for-srcblocks t
  643. "Whether custom styles for colorized source blocks be automatically created.
  644. When this option is turned on, the exporter creates custom styles
  645. for source blocks based on the advice of `htmlfontify'. Creation
  646. of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
  647. When this option is turned off exporter does not create such
  648. styles.
  649. Use the latter option if you do not want the custom styles to be
  650. based on your current display settings. It is necessary that the
  651. styles.xml already contains needed styles for colorizing to work.
  652. This variable is effective only if
  653. `org-e-odt-fontify-srcblocks' is turned on."
  654. :group 'org-export-e-odt
  655. :version "24.1"
  656. :type 'boolean)
  657. (defcustom org-e-odt-fontify-srcblocks t
  658. "Specify whether or not source blocks need to be fontified.
  659. Turn this option on if you want to colorize the source code
  660. blocks in the exported file. For colorization to work, you need
  661. to make available an enhanced version of `htmlfontify' library."
  662. :type 'boolean
  663. :group 'org-export-e-odt
  664. :version "24.1")
  665. ;;;; Table
  666. (defcustom org-e-odt-table-caption-above t
  667. "When non-nil, place caption string at the beginning of the table.
  668. Otherwise, place it near the end."
  669. :group 'org-export-e-odt
  670. :type 'boolean)
  671. (defcustom org-e-odt-table-styles
  672. '(("OrgEquation" "OrgEquation"
  673. ((use-first-column-styles . t)
  674. (use-last-column-styles . t))))
  675. "Specify how Table Styles should be derived from a Table Template.
  676. This is a list where each element is of the
  677. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  678. TABLE-STYLE-NAME is the style associated with the table through
  679. \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
  680. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  681. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  682. below) that is included in
  683. `org-e-odt-content-template-file'.
  684. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  685. \"TableCell\"
  686. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  687. \"TableParagraph\"
  688. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  689. \"FirstRow\" | \"LastRow\" |
  690. \"EvenRow\" | \"OddRow\" |
  691. \"EvenColumn\" | \"OddColumn\" | \"\"
  692. where \"+\" above denotes string concatenation.
  693. TABLE-CELL-OPTIONS is an alist where each element is of the
  694. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  695. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  696. `use-last-row-styles' |
  697. `use-first-column-styles' |
  698. `use-last-column-styles' |
  699. `use-banding-rows-styles' |
  700. `use-banding-columns-styles' |
  701. `use-first-row-styles'
  702. ON-OR-OFF := `t' | `nil'
  703. For example, with the following configuration
  704. \(setq org-e-odt-table-styles
  705. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  706. \(\(use-first-row-styles . t\)
  707. \(use-first-column-styles . t\)\)\)
  708. \(\"TableWithHeaderColumns\" \"Custom\"
  709. \(\(use-first-column-styles . t\)\)\)\)\)
  710. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  711. style will use the following table-cell styles -
  712. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  713. \"CustomTableCell\" and the following paragraph styles
  714. \"CustomFirstRowTableParagraph\",
  715. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  716. as appropriate.
  717. 2. A table associated with \"TableWithHeaderColumns\" style will
  718. use the following table-cell styles -
  719. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  720. following paragraph styles
  721. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  722. as appropriate..
  723. Note that TABLE-TEMPLATE-NAME corresponds to the
  724. \"<table:table-template>\" elements contained within
  725. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  726. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  727. \"table:template-name\" and \"table:use-first-row-styles\" etc
  728. attributes of \"<table:table>\" element. Refer ODF-1.2
  729. specification for more information. Also consult the
  730. implementation filed under `org-e-odt-get-table-cell-styles'.
  731. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  732. formatting of numbered display equations. Do not delete this
  733. style from the list."
  734. :group 'org-export-e-odt
  735. :version "24.1"
  736. :type '(choice
  737. (const :tag "None" nil)
  738. (repeat :tag "Table Styles"
  739. (list :tag "Table Style Specification"
  740. (string :tag "Table Style Name")
  741. (string :tag "Table Template Name")
  742. (alist :options (use-first-row-styles
  743. use-last-row-styles
  744. use-first-column-styles
  745. use-last-column-styles
  746. use-banding-rows-styles
  747. use-banding-columns-styles)
  748. :key-type symbol
  749. :value-type (const :tag "True" t))))))
  750. ;;; Internal functions
  751. ;;;; Date
  752. (defun org-e-odt--date (&optional org-ts fmt)
  753. (save-match-data
  754. (let* ((time
  755. (and (stringp org-ts)
  756. (string-match org-ts-regexp0 org-ts)
  757. (apply 'encode-time
  758. (org-fix-decoded-time
  759. (org-parse-time-string (match-string 0 org-ts) t)))))
  760. date)
  761. (cond
  762. (fmt (format-time-string fmt time))
  763. (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
  764. (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
  765. ;;;; Frame
  766. (defun org-e-odt--frame (text width height style &optional extra
  767. anchor-type)
  768. (let ((frame-attrs
  769. (concat
  770. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  771. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  772. extra
  773. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  774. (format
  775. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  776. style frame-attrs
  777. (concat text
  778. (let ((title (get-text-property 0 :title text))
  779. (desc (get-text-property 0 :description text)))
  780. (concat (and title
  781. (format "<svg:title>%s</svg:title>"
  782. (org-e-odt-encode-plain-text title t)))
  783. (and desc
  784. (format "<svg:desc>%s</svg:desc>"
  785. (org-e-odt-encode-plain-text desc t)))))))))
  786. ;;;; Library wrappers
  787. (defun org-e-odt--adopt-elements (parent &rest children)
  788. (prog1 parent
  789. (mapc (lambda (child)
  790. (let ((parent-1 (org-element-adopt-element parent child nil)))
  791. (assert (eq parent-1 parent))))
  792. children)))
  793. (defun org-e-odt--zip-extract (archive members target)
  794. (when (atom members) (setq members (list members)))
  795. (mapc (lambda (archive member target)
  796. (require 'arc-mode)
  797. (let* ((--quote-file-name
  798. ;; This is shamelessly stolen from `archive-zip-extract'.
  799. (lambda (name)
  800. (if (or (not (memq system-type '(windows-nt ms-dos)))
  801. (and (boundp 'w32-quote-process-args)
  802. (null w32-quote-process-args)))
  803. (shell-quote-argument name)
  804. name)))
  805. (target (funcall --quote-file-name target))
  806. (archive (expand-file-name archive))
  807. (archive-zip-extract
  808. (list "unzip" "-qq" "-o" "-d" target))
  809. exit-code command-output)
  810. (setq command-output
  811. (with-temp-buffer
  812. (setq exit-code (archive-zip-extract archive member))
  813. (buffer-string)))
  814. (unless (zerop exit-code)
  815. (message command-output)
  816. (error "Extraction failed"))))
  817. members))
  818. ;;;; Textbox
  819. (defun org-e-odt--textbox (text width height style &optional
  820. extra anchor-type)
  821. (org-e-odt--frame
  822. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  823. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  824. (and (not width)
  825. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  826. text)
  827. width nil style extra anchor-type))
  828. ;;;; Table of Contents
  829. (defun org-e-odt-begin-toc (index-title depth)
  830. (concat
  831. (format "
  832. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  833. <text:table-of-content-source text:outline-level=\"%d\">
  834. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  835. " depth index-title)
  836. (let ((levels (number-sequence 1 10)))
  837. (mapconcat
  838. (lambda (level)
  839. (format
  840. "
  841. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  842. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  843. <text:index-entry-chapter/>
  844. <text:index-entry-text/>
  845. <text:index-entry-link-end/>
  846. </text:table-of-content-entry-template>
  847. " level level)) levels ""))
  848. (format "
  849. </text:table-of-content-source>
  850. <text:index-body>
  851. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  852. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  853. </text:index-title>
  854. " index-title)))
  855. (defun org-e-odt-end-toc ()
  856. (format "
  857. </text:index-body>
  858. </text:table-of-content>
  859. "))
  860. (defun* org-e-odt-format-toc-headline
  861. (todo todo-type priority text tags
  862. &key level section-number headline-label &allow-other-keys)
  863. (setq text (concat
  864. (and org-export-with-section-numbers
  865. (concat section-number ". "))
  866. text
  867. (and tags
  868. (concat
  869. "<text:tab/>"
  870. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  871. "OrgTag" tags)))))
  872. (when todo
  873. (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
  874. "OrgTodo" text)))
  875. (org-e-odt-format-link text (concat "#" headline-label) t))
  876. (defun org-e-odt-toc (depth info)
  877. (assert (wholenump depth))
  878. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  879. (headlines (org-export-collect-headlines info depth)))
  880. (when headlines
  881. (concat
  882. (org-e-odt-begin-toc title depth)
  883. (mapconcat
  884. (lambda (headline)
  885. (let* ((entry (org-e-odt-format-headline--wrap
  886. headline info 'org-e-odt-format-toc-headline))
  887. (level (org-export-get-relative-level headline info))
  888. (style (format "Contents_20_%d" level)))
  889. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  890. style entry)))
  891. headlines "\n")
  892. (org-e-odt-end-toc)))))
  893. ;;;; Document styles
  894. (defun org-e-odt-add-automatic-style (object-type &optional object-props)
  895. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  896. OBJECT-PROPS is (typically) a plist created by passing
  897. \"#+ATTR_ODT: \" option of the object in question to
  898. `org-e-odt-parse-block-attributes'.
  899. Use `org-e-odt-object-counters' to generate an automatic
  900. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  901. new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
  902. . STYLE-NAME)."
  903. (assert (stringp object-type))
  904. (let* ((object (intern object-type))
  905. (seqvar object)
  906. (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
  907. (object-name (format "%s%d" object-type seqno)) style-name)
  908. (setq org-e-odt-object-counters
  909. (plist-put org-e-odt-object-counters seqvar seqno))
  910. (when object-props
  911. (setq style-name (format "Org%s" object-name))
  912. (setq org-e-odt-automatic-styles
  913. (plist-put org-e-odt-automatic-styles object
  914. (append (list (list style-name object-props))
  915. (plist-get org-e-odt-automatic-styles object)))))
  916. (cons object-name style-name)))
  917. ;;;; Caption and Labels
  918. (defun org-e-odt--wrap-label (element output)
  919. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  920. This function shouldn't be used for floats. See
  921. `org-e-odt--caption/label-string'."
  922. ;; (let ((label (org-element-property :name element)))
  923. ;; (if (or (not output) (not label) (string= output "") (string= label ""))
  924. ;; output
  925. ;; (concat (format "\\label{%s}\n" label) output)))
  926. output)
  927. (defun org-e-odt--caption/label-string (caption label info)
  928. "Return caption and label HTML string for floats.
  929. CAPTION is a cons cell of secondary strings, the car being the
  930. standard caption and the cdr its short form. LABEL is a string
  931. representing the label. INFO is a plist holding contextual
  932. information.
  933. If there's no caption nor label, return the empty string.
  934. For non-floats, see `org-e-odt--wrap-label'."
  935. (setq label nil) ;; FIXME
  936. (let ((label-str (if label (format "\\label{%s}" label) "")))
  937. (cond
  938. ((and (not caption) (not label)) "")
  939. ((not caption) (format "\\label{%s}\n" label))
  940. ;; Option caption format with short name.
  941. ((cdr caption)
  942. (format "\\caption[%s]{%s%s}\n"
  943. (org-export-data (cdr caption) info)
  944. label-str
  945. (org-export-data (car caption) info)))
  946. ;; Standard caption format.
  947. ;; (t (format "\\caption{%s%s}\n"
  948. ;; label-str
  949. ;; (org-export-data (car caption) info)))
  950. (t (org-export-data (car caption) info)))))
  951. ;;;; Checkbox
  952. (defun org-e-odt--checkbox (item)
  953. "Return check-box string associated to ITEM."
  954. (let ((checkbox (org-element-property :checkbox item)))
  955. (if (not checkbox) ""
  956. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  957. "OrgCode" (case checkbox
  958. (on "[&#x2713;] ") ; CHECK MARK
  959. (off "[ ] ")
  960. (trans "[-] "))))))
  961. ;;; Template
  962. (defun org-e-odt-template (contents info)
  963. "Return complete document string after HTML conversion.
  964. CONTENTS is the transcoded contents string. RAW-DATA is the
  965. original parsed data. INFO is a plist holding export options."
  966. ;; Write meta file.
  967. (let ((title (org-export-data (plist-get info :title) info))
  968. (author (let ((author (plist-get info :author)))
  969. (if (not author) "" (org-export-data author info))))
  970. (date (org-e-odt--date
  971. (org-export-data (plist-get info :date) info)))
  972. (email (plist-get info :email))
  973. (keywords (plist-get info :keywords))
  974. (description (plist-get info :description)))
  975. (write-region
  976. (concat
  977. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  978. <office:document-meta
  979. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  980. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  981. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  982. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  983. xmlns:ooo=\"http://openoffice.org/2004/office\"
  984. office:version=\"1.2\">
  985. <office:meta>\n"
  986. (format "<dc:creator>%s</dc:creator>\n" author)
  987. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  988. (format "<dc:date>%s</dc:date>\n" date)
  989. (format "<meta:creation-date>%s</meta:creation-date>\n" date)
  990. (format "<meta:generator>%s</meta:generator>\n"
  991. (let ((creator-info (plist-get info :with-creator)))
  992. (if (or (not creator-info) (eq creator-info 'comment)) ""
  993. (plist-get info :creator))))
  994. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  995. (format "<dc:subject>%s</dc:subject>\n" description)
  996. (format "<dc:title>%s</dc:title>\n" title)
  997. "\n"
  998. " </office:meta>\n" "</office:document-meta>")
  999. nil (concat org-e-odt-zip-dir "meta.xml"))
  1000. ;; Add meta.xml in to manifest.
  1001. (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1002. ;; Update styles file.
  1003. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1004. ;; Write styles file.
  1005. (let* ((styles-file (plist-get info :odt-styles-file))
  1006. (styles-file (and styles-file (read (org-trim styles-file))))
  1007. ;; Non-availability of styles.xml is not a critical
  1008. ;; error. For now throw an error purely for aesthetic
  1009. ;; reasons.
  1010. (styles-file (or styles-file
  1011. org-e-odt-styles-file
  1012. (expand-file-name "OrgOdtStyles.xml"
  1013. org-e-odt-styles-dir)
  1014. (error "org-e-odt: Missing styles file?"))))
  1015. (cond
  1016. ((listp styles-file)
  1017. (let ((archive (nth 0 styles-file))
  1018. (members (nth 1 styles-file)))
  1019. (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
  1020. (mapc
  1021. (lambda (member)
  1022. (when (org-file-image-p member)
  1023. (let* ((image-type (file-name-extension member))
  1024. (media-type (format "image/%s" image-type)))
  1025. (org-e-odt-create-manifest-file-entry media-type member))))
  1026. members)))
  1027. ((and (stringp styles-file) (file-exists-p styles-file))
  1028. (let ((styles-file-type (file-name-extension styles-file)))
  1029. (cond
  1030. ((string= styles-file-type "xml")
  1031. (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
  1032. ((member styles-file-type '("odt" "ott"))
  1033. (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
  1034. (t
  1035. (error (format "Invalid specification of styles.xml file: %S"
  1036. org-e-odt-styles-file))))
  1037. ;; create a manifest entry for styles.xml
  1038. (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1039. ;; FIXME: Who is opening an empty styles.xml before this point?
  1040. (with-current-buffer
  1041. (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
  1042. (revert-buffer t t)
  1043. ;; Write custom styles for source blocks
  1044. ;; Save STYLES used for colorizing of source blocks.
  1045. ;; Update styles.xml with styles that were collected as part of
  1046. ;; `org-e-odt-hfy-face-to-css' callbacks.
  1047. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1048. hfy-user-sheet-assoc "")))
  1049. (when styles
  1050. (goto-char (point-min))
  1051. (when (re-search-forward "</office:styles>" nil t)
  1052. (goto-char (match-beginning 0))
  1053. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1054. ;; Update styles.xml - take care of outline numbering
  1055. ;; Don't make automatic backup of styles.xml file. This setting
  1056. ;; prevents the backed-up styles.xml file from being zipped in to
  1057. ;; odt file. This is more of a hackish fix. Better alternative
  1058. ;; would be to fix the zip command so that the output odt file
  1059. ;; includes only the needed files and excludes any auto-generated
  1060. ;; extra files like backups and auto-saves etc etc. Note that
  1061. ;; currently the zip command zips up the entire temp directory so
  1062. ;; that any auto-generated files created under the hood ends up in
  1063. ;; the resulting odt file.
  1064. (set (make-local-variable 'backup-inhibited) t)
  1065. ;; Outline numbering is retained only upto LEVEL.
  1066. ;; To disable outline numbering pass a LEVEL of 0.
  1067. (goto-char (point-min))
  1068. (let ((regex
  1069. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1070. (replacement
  1071. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1072. (while (re-search-forward regex nil t)
  1073. (unless (let ((sec-num (plist-get info :section-numbers))
  1074. (level (string-to-number (match-string 2))))
  1075. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1076. (replace-match replacement t nil))))
  1077. (save-buffer 0)))
  1078. ;; Update content.xml.
  1079. (with-temp-buffer
  1080. (insert-file-contents
  1081. (or org-e-odt-content-template-file
  1082. (expand-file-name "OrgOdtContentTemplate.xml"
  1083. org-e-odt-styles-dir)))
  1084. ;; Write automatic styles.
  1085. ;; - Position the cursor.
  1086. (goto-char (point-min))
  1087. (re-search-forward " </office:automatic-styles>" nil t)
  1088. (goto-char (match-beginning 0))
  1089. ;; - Dump automatic table styles
  1090. (loop for (style-name props) in
  1091. (plist-get org-e-odt-automatic-styles 'Table) do
  1092. (when (setq props (or (plist-get props :rel-width) 96))
  1093. (insert (format org-e-odt-table-style-format style-name props))))
  1094. ;; Update display level.
  1095. ;; - Remove existing sequence decls. Also position the cursor.
  1096. (goto-char (point-min))
  1097. (when (re-search-forward "<text:sequence-decls" nil t)
  1098. (delete-region (match-beginning 0)
  1099. (re-search-forward "</text:sequence-decls>" nil nil)))
  1100. ;; Update sequence decls according to user preference.
  1101. (insert
  1102. (format
  1103. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1104. (mapconcat
  1105. (lambda (x)
  1106. (format
  1107. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1108. org-e-odt-display-outline-level (nth 1 x)))
  1109. org-e-odt-category-map-alist "\n")))
  1110. ;; Position the cursor to document body.
  1111. (goto-char (point-min))
  1112. (re-search-forward "</office:text>" nil nil)
  1113. (goto-char (match-beginning 0))
  1114. ;; Preamble - Title, Author, Date etc.
  1115. (insert
  1116. (let* ((title (org-export-data (plist-get info :title) info))
  1117. (author (and (plist-get info :with-author)
  1118. (let ((auth (plist-get info :author)))
  1119. (and auth (org-export-data auth info)))))
  1120. (date (org-export-data (plist-get info :date) info))
  1121. (iso-date (org-e-odt--date date))
  1122. (date (org-e-odt--date date "%d %b %Y"))
  1123. (email (plist-get info :email))
  1124. ;; switch on or off above vars based on user settings
  1125. (author (and (plist-get info :with-author) (or author email)))
  1126. ;; (date (and (plist-get info :time-stamp-file) date))
  1127. (email (and (plist-get info :with-email) email)))
  1128. (concat
  1129. ;; title
  1130. (when title
  1131. (concat
  1132. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1133. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1134. ;; separator
  1135. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1136. (cond
  1137. ((and author (not email))
  1138. ;; author only
  1139. (concat
  1140. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1141. "OrgSubtitle"
  1142. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1143. ;; separator
  1144. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1145. ((and author email)
  1146. ;; author and email
  1147. (concat
  1148. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1149. "OrgSubtitle"
  1150. (org-e-odt-format-link
  1151. (format "<text:initial-creator>%s</text:initial-creator>" author)
  1152. (concat "mailto:" email)))
  1153. ;; separator
  1154. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1155. ;; date
  1156. (when date
  1157. (concat
  1158. (format
  1159. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1160. "OrgSubtitle"
  1161. (format
  1162. "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
  1163. "N75" iso-date date))
  1164. ;; separator
  1165. "<text:p text:style-name=\"OrgSubtitle\"/>")))))
  1166. ;; Table of Contents
  1167. (let ((depth (plist-get info :with-toc)))
  1168. (when (wholenump depth) (insert (org-e-odt-toc depth info))))
  1169. ;; Contents.
  1170. (insert contents)
  1171. ;; Return contents.
  1172. (buffer-substring-no-properties (point-min) (point-max))))
  1173. ;;; Transcode Functions
  1174. ;;;; Bold
  1175. (defun org-e-odt-bold (bold contents info)
  1176. "Transcode BOLD from Org to ODT.
  1177. CONTENTS is the text with bold markup. INFO is a plist holding
  1178. contextual information."
  1179. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1180. "Bold" contents))
  1181. ;;;; Center Block
  1182. (defun org-e-odt-center-block (center-block contents info)
  1183. "Transcode a CENTER-BLOCK element from Org to ODT.
  1184. CONTENTS holds the contents of the center block. INFO is a plist
  1185. holding contextual information."
  1186. (org-e-odt--wrap-label center-block contents))
  1187. ;;;; Clock
  1188. (defun org-e-odt-clock (clock contents info)
  1189. "Transcode a CLOCK element from Org to ODT.
  1190. CONTENTS is nil. INFO is a plist used as a communication
  1191. channel."
  1192. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1193. "OrgTimestampWrapper"
  1194. (concat
  1195. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1196. "OrgTimestampKeyword" org-clock-string)
  1197. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1198. "OrgTimestamp"
  1199. (concat (org-translate-time
  1200. (org-element-property :value clock))
  1201. (let ((time (org-element-property :time clock)))
  1202. (and time (format " (%s)" time))))))))
  1203. ;;;; Code
  1204. (defun org-e-odt-code (code contents info)
  1205. "Transcode a CODE object from Org to ODT.
  1206. CONTENTS is nil. INFO is a plist used as a communication
  1207. channel."
  1208. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1209. "OrgCode" (org-element-property :value code)))
  1210. ;;;; Comment
  1211. ;; Comments are ignored.
  1212. ;;;; Comment Block
  1213. ;; Comment Blocks are ignored.
  1214. ;;;; Drawer
  1215. (defun org-e-odt-drawer (drawer contents info)
  1216. "Transcode a DRAWER element from Org to ODT.
  1217. CONTENTS holds the contents of the block. INFO is a plist
  1218. holding contextual information."
  1219. (let* ((name (org-element-property :drawer-name drawer))
  1220. (output (if (functionp org-e-odt-format-drawer-function)
  1221. (funcall org-e-odt-format-drawer-function
  1222. name contents)
  1223. ;; If there's no user defined function: simply
  1224. ;; display contents of the drawer.
  1225. contents)))
  1226. (org-e-odt--wrap-label drawer output)))
  1227. ;;;; Dynamic Block
  1228. (defun org-e-odt-dynamic-block (dynamic-block contents info)
  1229. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1230. CONTENTS holds the contents of the block. INFO is a plist
  1231. holding contextual information. See `org-export-data'."
  1232. (org-e-odt--wrap-label dynamic-block contents))
  1233. ;;;; Entity
  1234. (defun org-e-odt-entity (entity contents info)
  1235. "Transcode an ENTITY object from Org to ODT.
  1236. CONTENTS are the definition itself. INFO is a plist holding
  1237. contextual information."
  1238. ;; (let ((ent (org-element-property :latex entity)))
  1239. ;; (if (org-element-property :latex-math-p entity)
  1240. ;; (format "$%s$" ent)
  1241. ;; ent))
  1242. (org-element-property :utf-8 entity))
  1243. ;;;; Example Block
  1244. (defun org-e-odt-example-block (example-block contents info)
  1245. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1246. CONTENTS is nil. INFO is a plist holding contextual information."
  1247. (org-e-odt--wrap-label
  1248. example-block (org-e-odt-format-code example-block info)))
  1249. ;;;; Export Snippet
  1250. (defun org-e-odt-export-snippet (export-snippet contents info)
  1251. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1252. CONTENTS is nil. INFO is a plist holding contextual information."
  1253. (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
  1254. (org-element-property :value export-snippet)))
  1255. ;;;; Export Block
  1256. (defun org-e-odt-export-block (export-block contents info)
  1257. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1258. CONTENTS is nil. INFO is a plist holding contextual information."
  1259. (when (string= (org-element-property :type export-block) "ODT")
  1260. (org-remove-indentation (org-element-property :value export-block))))
  1261. ;;;; Fixed Width
  1262. (defun org-e-odt-fixed-width (fixed-width contents info)
  1263. "Transcode a FIXED-WIDTH element from Org to ODT.
  1264. CONTENTS is nil. INFO is a plist holding contextual information."
  1265. (org-e-odt--wrap-label
  1266. fixed-width (org-e-odt-do-format-code
  1267. (org-element-property :value fixed-width))))
  1268. ;;;; Footnote Definition
  1269. ;; Footnote Definitions are ignored.
  1270. ;;;; Footnote Reference
  1271. (defun org-e-odt-footnote-reference (footnote-reference contents info)
  1272. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1273. CONTENTS is nil. INFO is a plist holding contextual information."
  1274. (let ((--format-footnote-definition
  1275. (function
  1276. (lambda (n def)
  1277. (setq n (format "%d" n))
  1278. (let ((id (concat "fn" n))
  1279. (note-class "footnote")
  1280. (par-style "Footnote"))
  1281. (format
  1282. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1283. id note-class
  1284. (concat
  1285. (format "<text:note-citation>%s</text:note-citation>" n)
  1286. (format "<text:note-body>%s</text:note-body>" def)))))))
  1287. (--format-footnote-reference
  1288. (function
  1289. (lambda (n)
  1290. (setq n (format "%d" n))
  1291. (let ((note-class "footnote")
  1292. (ref-format "text")
  1293. (ref-name (concat "fn" n)))
  1294. (format
  1295. "<text:span text:style-name=\"%s\">%s</text:span>"
  1296. "OrgSuperscript"
  1297. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1298. note-class ref-format ref-name n)))))))
  1299. (concat
  1300. ;; Insert separator between two footnotes in a row.
  1301. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1302. (and (eq (org-element-type prev) 'footnote-reference)
  1303. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1304. "OrgSuperscript" ",")))
  1305. ;; Trancode footnote reference.
  1306. (let ((n (org-export-get-footnote-number footnote-reference info)))
  1307. (cond
  1308. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1309. (funcall --format-footnote-reference n))
  1310. ;; Inline definitions are secondary strings.
  1311. ;; Non-inline footnotes definitions are full Org data.
  1312. (t
  1313. (let* ((raw (org-export-get-footnote-definition footnote-reference
  1314. info))
  1315. (def (let ((def (org-trim (org-export-data raw info))))
  1316. (if (eq (org-element-type raw) 'org-data) def
  1317. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1318. "Footnote" def)))))
  1319. (funcall --format-footnote-definition n def))))))))
  1320. ;;;; Headline
  1321. (defun* org-e-odt-format-headline
  1322. (todo todo-type priority text tags
  1323. &key level section-number headline-label &allow-other-keys)
  1324. (concat
  1325. ;; Todo.
  1326. (and todo
  1327. (concat
  1328. (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
  1329. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1330. style todo)) " "))
  1331. ;; Title.
  1332. text
  1333. ;; Tags.
  1334. (and tags
  1335. (concat "<text:tab/>"
  1336. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1337. "OrgTag" (mapconcat 'org-trim tags " : "))))))
  1338. (defun org-e-odt-format-headline--wrap (headline info
  1339. &optional format-function
  1340. &rest extra-keys)
  1341. "Transcode an HEADLINE element from Org to ODT.
  1342. CONTENTS holds the contents of the headline. INFO is a plist
  1343. holding contextual information."
  1344. (let* ((level (+ (org-export-get-relative-level headline info)))
  1345. (headline-number (org-export-get-headline-number headline info))
  1346. (section-number (and (org-export-numbered-headline-p headline info)
  1347. (mapconcat 'number-to-string
  1348. headline-number ".")))
  1349. (todo (and (plist-get info :with-todo-keywords)
  1350. (let ((todo (org-element-property :todo-keyword headline)))
  1351. (and todo (org-export-data todo info)))))
  1352. (todo-type (and todo (org-element-property :todo-type headline)))
  1353. (priority (and (plist-get info :with-priority)
  1354. (org-element-property :priority headline)))
  1355. (text (org-export-data (org-element-property :title headline) info))
  1356. (tags (and (plist-get info :with-tags)
  1357. (org-export-get-tags headline info)))
  1358. (headline-label (concat "sec-" (mapconcat 'number-to-string
  1359. headline-number "-")))
  1360. (format-function (cond
  1361. ((functionp format-function) format-function)
  1362. ((functionp org-e-odt-format-headline-function)
  1363. (function*
  1364. (lambda (todo todo-type priority text tags
  1365. &allow-other-keys)
  1366. (funcall org-e-odt-format-headline-function
  1367. todo todo-type priority text tags))))
  1368. (t 'org-e-odt-format-headline))))
  1369. (apply format-function
  1370. todo todo-type priority text tags
  1371. :headline-label headline-label :level level
  1372. :section-number section-number extra-keys)))
  1373. (defun org-e-odt-begin-plain-list (ltype &optional continue-numbering)
  1374. (unless (member ltype '(ordered unordered descriptive))
  1375. (error "Unknown list type: %s" ltype))
  1376. (let ((style-name (assoc-default ltype
  1377. '((ordered . "OrgNumberedList")
  1378. (unordered . "OrgBulletedList")
  1379. (descriptive . "OrgDescriptionList")))))
  1380. (format "<text:list text:style-name=\"%s\" text:continue-numbering=\"%s\">"
  1381. style-name (if continue-numbering "true" "false"))))
  1382. (defun org-e-odt-headline (headline contents info)
  1383. "Transcode an HEADLINE element from Org to ODT.
  1384. CONTENTS holds the contents of the headline. INFO is a plist
  1385. holding contextual information."
  1386. (let* ((numberedp (org-export-numbered-headline-p headline info))
  1387. ;; Get level relative to current parsed data.
  1388. (level (org-export-get-relative-level headline info))
  1389. (text (org-export-data (org-element-property :title headline) info))
  1390. ;; Create the headline text.
  1391. (full-text (org-e-odt-format-headline--wrap headline info)))
  1392. (cond
  1393. ;; Case 1: This is a footnote section: ignore it.
  1394. ((org-element-property :footnote-section-p headline) nil)
  1395. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1396. ;; Also export as items headlines for which no section
  1397. ;; format has been found.
  1398. ;; FIXME
  1399. ;; ((org-export-low-level-p headline info)
  1400. ;; ;; Build the real contents of the sub-tree.
  1401. ;; (let* ((type (if numberedp 'unordered 'unordered)) ; FIXME
  1402. ;; (itemized-body (org-e-odt-format-list-item
  1403. ;; contents type nil nil full-text)))
  1404. ;; (concat
  1405. ;; (and (org-export-first-sibling-p headline info)
  1406. ;; (org-e-odt-begin-plain-list type))
  1407. ;; itemized-body
  1408. ;; (and (org-export-last-sibling-p headline info)
  1409. ;; "</text:list>"))))
  1410. ;; Case 3. Standard headline. Export it as a section.
  1411. (t
  1412. (let* ((extra-ids (list (org-element-property :custom-id headline)
  1413. (org-element-property :id headline)))
  1414. (extra-ids nil) ; FIXME
  1415. (id (concat "sec-" (mapconcat 'number-to-string
  1416. (org-export-get-headline-number
  1417. headline info) "-"))))
  1418. (concat
  1419. (format
  1420. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s%s</text:h>"
  1421. (format "Heading_20_%s" level)
  1422. level
  1423. ;; Extra targets.
  1424. (mapconcat (lambda (x)
  1425. (when x
  1426. (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1427. (org-e-odt-format-target
  1428. "" (org-export-solidify-link-text x)))))
  1429. extra-ids "")
  1430. ;; Title.
  1431. (org-e-odt-format-target full-text id))
  1432. contents))))))
  1433. ;;;; Horizontal Rule
  1434. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  1435. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1436. CONTENTS is nil. INFO is a plist holding contextual information."
  1437. (org-e-odt--wrap-label
  1438. horizontal-rule
  1439. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1440. "Horizontal_20_Line" "")))
  1441. ;;;; Inline Babel Call
  1442. ;; Inline Babel Calls are ignored.
  1443. ;;;; Inline Src Block
  1444. (defun org-e-odt--find-verb-separator (s)
  1445. "Return a character not used in string S.
  1446. This is used to choose a separator for constructs like \\verb."
  1447. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1448. (loop for c across ll
  1449. when (not (string-match (regexp-quote (char-to-string c)) s))
  1450. return (char-to-string c))))
  1451. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  1452. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1453. CONTENTS holds the contents of the item. INFO is a plist holding
  1454. contextual information."
  1455. (let* ((org-lang (org-element-property :language inline-src-block))
  1456. (code (org-element-property :value inline-src-block))
  1457. (separator (org-e-odt--find-verb-separator code)))
  1458. (error "FIXME")))
  1459. ;;;; Inlinetask
  1460. (defun org-e-odt-inlinetask (inlinetask contents info)
  1461. "Transcode an INLINETASK element from Org to ODT.
  1462. CONTENTS holds the contents of the block. INFO is a plist
  1463. holding contextual information."
  1464. (cond
  1465. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  1466. ;; with appropriate arguments.
  1467. ((functionp org-e-odt-format-inlinetask-function)
  1468. (let ((format-function
  1469. (function*
  1470. (lambda (todo todo-type priority text tags
  1471. &key contents &allow-other-keys)
  1472. (funcall org-e-odt-format-inlinetask-function
  1473. todo todo-type priority text tags contents)))))
  1474. (org-e-odt-format-headline--wrap
  1475. inlinetask info format-function :contents contents)))
  1476. ;; Otherwise, use a default template.
  1477. (t (org-e-odt--wrap-label
  1478. inlinetask
  1479. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1480. "Text_20_body"
  1481. (org-e-odt--textbox
  1482. (concat
  1483. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1484. "OrgInlineTaskHeading"
  1485. (org-e-odt-format-headline--wrap
  1486. inlinetask info))
  1487. contents)
  1488. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
  1489. ;;;; Italic
  1490. (defun org-e-odt-italic (italic contents info)
  1491. "Transcode ITALIC from Org to ODT.
  1492. CONTENTS is the text with italic markup. INFO is a plist holding
  1493. contextual information."
  1494. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1495. "Emphasis" contents))
  1496. ;;;; Item
  1497. (defun org-e-odt-item (item contents info)
  1498. "Transcode an ITEM element from Org to ODT.
  1499. CONTENTS holds the contents of the item. INFO is a plist holding
  1500. contextual information."
  1501. (let* ((plain-list (org-export-get-parent item))
  1502. (type (org-element-property :type plain-list))
  1503. (counter (org-element-property :counter item))
  1504. (tag (let ((tag (org-element-property :tag item)))
  1505. (and tag
  1506. (concat (org-e-odt--checkbox item)
  1507. (org-export-data tag info))))))
  1508. (case type
  1509. ((ordered unordered)
  1510. (format "\n<text:list-item>\n%s\n%s"
  1511. contents
  1512. (let* ((--element-has-a-table-p
  1513. (function
  1514. (lambda (element info)
  1515. (loop for el in (org-element-contents element)
  1516. thereis (eq (org-element-type el) 'table))))))
  1517. (cond
  1518. ((funcall --element-has-a-table-p item info)
  1519. "</text:list-header>")
  1520. (t "</text:list-item>")))))
  1521. (descriptive
  1522. (concat
  1523. (let ((term (or tag "(no term)")))
  1524. (concat
  1525. (format "\n<text:list-item>\n%s\n</text:list-item>"
  1526. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1527. "Text_20_body_20_bold" term))
  1528. (format
  1529. "\n<text:list-item>\n%s\n</text:list-item>"
  1530. (format "\n<text:list text:style-name=\"%s\" %s>\n%s\n</text:list>"
  1531. "OrgDescriptionList"
  1532. "text:continue-numbering=\"false\""
  1533. (format "\n<text:list-item>\n%s\n</text:list-item>"
  1534. contents)))))))
  1535. (t (error "Unknown list type: %S" type)))))
  1536. ;;;; Keyword
  1537. (defun org-e-odt-keyword (keyword contents info)
  1538. "Transcode a KEYWORD element from Org to ODT.
  1539. CONTENTS is nil. INFO is a plist holding contextual information."
  1540. (let ((key (org-element-property :key keyword))
  1541. (value (org-element-property :value keyword)))
  1542. (cond
  1543. ((string= key "LATEX") value)
  1544. ((string= key "INDEX") (format "\\index{%s}" value))
  1545. ((string= key "TARGET") nil ; FIXME
  1546. ;; (format "\\label{%s}" (org-export-solidify-link-text value))
  1547. )
  1548. ((string= key "toc")
  1549. (let ((value (downcase value)))
  1550. (cond
  1551. ((string-match "\\<headlines\\>" value)
  1552. (let ((depth (or (and (string-match "[0-9]+" value)
  1553. (string-to-number (match-string 0 value)))
  1554. (plist-get info :with-toc))))
  1555. (when (wholenump depth) (org-e-odt-toc depth info))))
  1556. ((string= "tables" value) "FIXME")
  1557. ((string= "figures" value) "FIXME")
  1558. ((string= "listings" value)
  1559. (cond
  1560. ;; At the moment, src blocks with a caption are wrapped
  1561. ;; into a figure environment.
  1562. (t "FIXME")))))))))
  1563. ;;;; Latex Environment
  1564. (eval-after-load 'org-e-odt
  1565. '(ad-deactivate 'org-format-latex-as-mathml))
  1566. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1567. ;; (after org-e-odt-protect-latex-fragment activate)
  1568. ;; "Encode LaTeX fragment as XML.
  1569. ;; Do this when translation to MathML fails."
  1570. ;; (when (or (not (> (length ad-return-value) 0))
  1571. ;; (get-text-property 0 'org-protected ad-return-value))
  1572. ;; (setq ad-return-value
  1573. ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
  1574. ;; 'org-protected t))))
  1575. (defun org-e-odt-format-latex (latex-frag processing-type info)
  1576. (let* ((prefix (case processing-type
  1577. (dvipng "ltxpng/")
  1578. (mathml "ltxmathml/")))
  1579. (input-file (plist-get info :input-file))
  1580. (cache-subdir
  1581. (concat prefix (file-name-sans-extension
  1582. (file-name-nondirectory input-file))))
  1583. (cache-dir (file-name-directory input-file))
  1584. (display-msg (case processing-type
  1585. (dvipng "Creating LaTeX Image...")
  1586. (mathml "Creating MathML snippet..."))))
  1587. (with-temp-buffer
  1588. (insert latex-frag)
  1589. (org-format-latex cache-subdir cache-dir nil display-msg
  1590. nil nil processing-type)
  1591. (buffer-string))))
  1592. (defun org-e-odt-latex-environment (latex-environment contents info)
  1593. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1594. CONTENTS is nil. INFO is a plist holding contextual information."
  1595. (org-e-odt--wrap-label
  1596. latex-environment
  1597. (let* ((latex-frag
  1598. (org-remove-indentation
  1599. (org-element-property :value latex-environment)))
  1600. (processing-type (plist-get info :LaTeX-fragments))
  1601. (caption (org-element-property :caption latex-environment))
  1602. (short-caption (and (cdr caption)
  1603. (org-export-data (cdr caption) info)))
  1604. (caption (and (car caption) (org-export-data (car caption) info)))
  1605. (label (org-element-property :name latex-environment))
  1606. (attr nil) ; FIXME
  1607. (label (org-element-property :name latex-environment)))
  1608. (when (memq processing-type '(t mathjax))
  1609. (unless (and (fboundp 'org-format-latex-mathml-available-p)
  1610. (org-format-latex-mathml-available-p))
  1611. (message "LaTeX to MathML converter not available. Trying dvinpng...")
  1612. (setq processing-type 'dvipng)))
  1613. (when (eq processing-type 'dvipng)
  1614. (unless (and (org-check-external-command "latex" "" t)
  1615. (org-check-external-command "dvipng" "" t))
  1616. (message "LaTeX to PNG converter not available. Using verbatim.")
  1617. (setq processing-type 'verbatim)))
  1618. (case processing-type
  1619. ((t mathjax)
  1620. (org-e-odt-format-formula latex-environment info))
  1621. (dvipng
  1622. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1623. "Text_20_body"
  1624. (org-e-odt-link--inline-image latex-environment info)))
  1625. (t (org-e-odt-do-format-code latex-frag))))))
  1626. ;;;; Latex Fragment
  1627. ;; (when latex-frag ; FIXME
  1628. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1629. ;; :description latex-frag)))
  1630. ;; handle verbatim
  1631. ;; provide descriptions
  1632. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  1633. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1634. CONTENTS is nil. INFO is a plist holding contextual information."
  1635. (let* ((latex-frag (org-element-property :value latex-fragment))
  1636. (processing-type (plist-get info :LaTeX-fragments)))
  1637. (cond
  1638. ((member processing-type '(t mathjax))
  1639. (org-e-odt-format-formula latex-fragment info))
  1640. ((eq processing-type 'dvipng)
  1641. (org-e-odt-link--inline-image latex-fragment info))
  1642. (t (org-e-odt-encode-plain-text latex-frag t)))))
  1643. ;;;; Line Break
  1644. (defun org-e-odt-line-break (line-break contents info)
  1645. "Transcode a LINE-BREAK object from Org to ODT.
  1646. CONTENTS is nil. INFO is a plist holding contextual information."
  1647. "<text:line-break/>\n")
  1648. ;;;; Link
  1649. ;;;; Links :: Generic
  1650. (defun org-e-odt-format-link (desc href &optional suppress-xref)
  1651. (cond
  1652. ((and (= (string-to-char href) ?#) (not suppress-xref))
  1653. (setq href (substring href 1))
  1654. (let ((xref-format "text"))
  1655. (when (numberp desc)
  1656. (setq desc (format "%d" desc) xref-format "number"))
  1657. (when (listp desc)
  1658. (setq desc (mapconcat 'number-to-string desc ".") xref-format "chapter"))
  1659. (setq href (concat org-e-odt-bookmark-prefix href))
  1660. (format
  1661. "<text:bookmark-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
  1662. xref-format href desc)))
  1663. ;; (org-lparse-link-description-is-image
  1664. ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  1665. ;; href desc))
  1666. (t (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1667. href desc))))
  1668. (defun org-e-odt-format-internal-link (text href)
  1669. (org-e-odt-format-link text (concat "#" href)))
  1670. ;;;; Links :: Label references
  1671. (defun org-e-odt-enumerate-element (element info &optional predicate n)
  1672. (let* ((--numbered-parent-headline-at-<=-n
  1673. (function
  1674. (lambda (element n info)
  1675. (loop for x in (org-export-get-genealogy element)
  1676. thereis (and (eq (org-element-type x) 'headline)
  1677. (<= (org-export-get-relative-level x info) n)
  1678. (org-export-numbered-headline-p x info)
  1679. x)))))
  1680. (--enumerate
  1681. (function
  1682. (lambda (element scope info &optional predicate)
  1683. (let ((counter 0))
  1684. (org-element-map
  1685. (or scope (plist-get info :parse-tree))
  1686. (org-element-type element)
  1687. (lambda (el)
  1688. (and (or (not predicate) (funcall predicate el info))
  1689. (incf counter)
  1690. (eq element el)
  1691. counter))
  1692. info 'first-match)))))
  1693. (scope (funcall --numbered-parent-headline-at-<=-n
  1694. element (or n org-e-odt-display-outline-level) info))
  1695. (ordinal (funcall --enumerate element scope info predicate))
  1696. (tag
  1697. (concat
  1698. ;; Section number.
  1699. (and scope
  1700. (mapconcat 'number-to-string
  1701. (org-export-get-headline-number scope info) "."))
  1702. ;; Separator.
  1703. (and scope ".")
  1704. ;; Ordinal.
  1705. (number-to-string ordinal))))
  1706. tag))
  1707. (defun org-e-odt-format-label (element info op)
  1708. (let* ((caption-from
  1709. (case (org-element-type element)
  1710. (link (org-export-get-parent-element element))
  1711. (t element)))
  1712. ;; get label and caption.
  1713. (label (org-element-property :name caption-from))
  1714. (caption (org-element-property :caption caption-from))
  1715. (short-caption (cdr caption))
  1716. ;; transcode captions.
  1717. (caption (and (car caption) (org-export-data (car caption) info)))
  1718. (short-caption (and short-caption
  1719. (org-export-data short-caption info))))
  1720. (when (or label caption)
  1721. (let* ((default-category
  1722. (cond
  1723. ((eq (org-element-type element) 'table)
  1724. "__Table__")
  1725. ((org-e-odt-standalone-image-p element info)
  1726. "__Figure__")
  1727. ((member (org-element-type element)
  1728. '(latex-environment latex-fragment))
  1729. (let ((processing-type (plist-get info :LaTeX-fragments)))
  1730. (cond
  1731. ((eq processing-type 'dvipng) "__DvipngImage__")
  1732. ((eq processing-type 'mathjax) "__MathFormula__")
  1733. ((eq processing-type 't) "__MathFormula__")
  1734. (t (error "Handle LaTeX:verbatim")))))
  1735. ((eq (org-element-type element) 'src-block)
  1736. "__Listing__")
  1737. (t (error "Handle enumeration of %S" element))))
  1738. (predicate
  1739. (cond
  1740. ((member (org-element-type element)
  1741. '(table latex-environment src-block))
  1742. nil)
  1743. ((org-e-odt-standalone-image-p element info)
  1744. 'org-e-odt-standalone-image-p)
  1745. (t (error "Handle enumeration of %S" element))))
  1746. (seqno (org-e-odt-enumerate-element
  1747. element info predicate)) ; FIXME
  1748. ;; handle label props.
  1749. (label-props (assoc default-category org-e-odt-category-map-alist))
  1750. ;; identify opendocument counter
  1751. (counter (nth 1 label-props))
  1752. ;; identify label style
  1753. (label-style (nth 2 label-props))
  1754. ;; retrieve localized category sting
  1755. (category (org-export-translate (nth 3 label-props) :utf-8 info)))
  1756. (case op
  1757. (definition
  1758. ;; assign an internal label, if user has not provided one
  1759. (setq label (or label (format "%s-%s" default-category seqno)))
  1760. (setq label (org-export-solidify-link-text label))
  1761. (cons
  1762. (format-spec
  1763. (cadr (assoc-string label-style org-e-odt-label-styles t))
  1764. `((?e . ,category)
  1765. (?n . ,(format
  1766. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1767. label counter counter seqno))
  1768. (?c . ,(or caption ""))))
  1769. short-caption))
  1770. (reference
  1771. (assert label)
  1772. (setq label (org-export-solidify-link-text label))
  1773. (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
  1774. (fmt1 (car fmt))
  1775. (fmt2 (cadr fmt)))
  1776. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1777. fmt1 label (format-spec fmt2 `((?e . ,category)
  1778. (?n . ,seqno))))))
  1779. (t (error "Unknow %S on label" op)))))))
  1780. ;;;; Links :: Embedded images
  1781. (defun org-e-odt-copy-image-file (path)
  1782. "Returns the internal name of the file"
  1783. (let* ((image-type (file-name-extension path))
  1784. (media-type (format "image/%s" image-type))
  1785. (target-dir "Images/")
  1786. (target-file
  1787. (format "%s%04d.%s" target-dir
  1788. (incf org-e-odt-embedded-images-count) image-type)))
  1789. (message "Embedding %s as %s ..."
  1790. (substring-no-properties path) target-file)
  1791. (when (= 1 org-e-odt-embedded-images-count)
  1792. (make-directory (concat org-e-odt-zip-dir target-dir))
  1793. (org-e-odt-create-manifest-file-entry "" target-dir))
  1794. (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
  1795. (org-e-odt-create-manifest-file-entry media-type target-file)
  1796. target-file))
  1797. (defun org-e-odt-image-size-from-file (file &optional user-width
  1798. user-height scale dpi embed-as)
  1799. (let* ((--pixels-to-cms
  1800. (function (lambda (pixels dpi)
  1801. (let ((cms-per-inch 2.54)
  1802. (inches (/ pixels dpi)))
  1803. (* cms-per-inch inches)))))
  1804. (--size-in-cms
  1805. (function
  1806. (lambda (size-in-pixels dpi)
  1807. (and size-in-pixels
  1808. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  1809. (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
  1810. (dpi (or dpi org-e-odt-pixels-per-inch))
  1811. (anchor-type (or embed-as "paragraph"))
  1812. (user-width (and (not scale) user-width))
  1813. (user-height (and (not scale) user-height))
  1814. (size
  1815. (and
  1816. (not (and user-height user-width))
  1817. (or
  1818. ;; Use Imagemagick.
  1819. (and (executable-find "identify")
  1820. (let ((size-in-pixels
  1821. (let ((dim (shell-command-to-string
  1822. (format "identify -format \"%%w:%%h\" \"%s\""
  1823. file))))
  1824. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1825. (cons (string-to-number (match-string 1 dim))
  1826. (string-to-number (match-string 2 dim)))))))
  1827. (funcall --size-in-cms size-in-pixels dpi)))
  1828. ;; Use Emacs.
  1829. (let ((size-in-pixels
  1830. (ignore-errors ; Emacs could be in batch mode
  1831. (clear-image-cache)
  1832. (image-size (create-image file) 'pixels))))
  1833. (funcall --size-in-cms size-in-pixels dpi))
  1834. ;; Use hard-coded values.
  1835. (cdr (assoc-string anchor-type
  1836. org-e-odt-default-image-sizes-alist))
  1837. ;; Error out.
  1838. (error "Cannot determine image size, aborting"))))
  1839. (width (car size)) (height (cdr size)))
  1840. (cond
  1841. (scale
  1842. (setq width (* width scale) height (* height scale)))
  1843. ((and user-height user-width)
  1844. (setq width user-width height user-height))
  1845. (user-height
  1846. (setq width (* user-height (/ width height)) height user-height))
  1847. (user-width
  1848. (setq height (* user-width (/ height width)) width user-width))
  1849. (t (ignore)))
  1850. ;; ensure that an embedded image fits comfortably within a page
  1851. (let ((max-width (car org-e-odt-max-image-size))
  1852. (max-height (cdr org-e-odt-max-image-size)))
  1853. (when (or (> width max-width) (> height max-height))
  1854. (let* ((scale1 (/ max-width width))
  1855. (scale2 (/ max-height height))
  1856. (scale (min scale1 scale2)))
  1857. (setq width (* scale width) height (* scale height)))))
  1858. (cons width height)))
  1859. ;;;; Links :: Math formula
  1860. (defun org-e-odt-format-formula (element info)
  1861. (let* ((src (cond
  1862. ((eq (org-element-type element) 'link) ; FIXME
  1863. (let* ((type (org-element-property :type element))
  1864. (raw-path (org-element-property :path element)))
  1865. (cond
  1866. ((file-name-absolute-p raw-path)
  1867. (expand-file-name raw-path))
  1868. (t raw-path))))
  1869. ((member (org-element-type element)
  1870. '(latex-fragment latex-environment))
  1871. (let* ((latex-frag (org-remove-indentation
  1872. (org-element-property :value element)))
  1873. (formula-link (org-e-odt-format-latex
  1874. latex-frag 'mathml info)))
  1875. (and formula-link
  1876. (string-match "file:\\([^]]*\\)" formula-link)
  1877. (match-string 1 formula-link))))
  1878. (t (error "what is this?"))))
  1879. (full-src (if (file-name-absolute-p src) src
  1880. (expand-file-name src (file-name-directory
  1881. (plist-get info :input-file)))))
  1882. (caption-from
  1883. (case (org-element-type element)
  1884. (link (org-export-get-parent-element element))
  1885. (t element)))
  1886. (captions (org-e-odt-format-label caption-from info 'definition))
  1887. (caption (car captions))
  1888. (href
  1889. (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  1890. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  1891. (file-name-directory (org-e-odt-copy-formula-file full-src))))
  1892. (embed-as (if caption 'paragraph 'character))
  1893. width height)
  1894. (cond
  1895. ((eq embed-as 'character)
  1896. (org-e-odt-format-entity "InlineFormula" href width height))
  1897. (t
  1898. (let* ((equation (org-e-odt-format-entity
  1899. "CaptionedDisplayFormula" href width height captions))
  1900. (label
  1901. (let* ((org-e-odt-category-map-alist
  1902. '(("__Table__" "Table" "value")
  1903. ("__Figure__" "Illustration" "value")
  1904. ("__MathFormula__" "Text" "math-label")
  1905. ("__DvipngImage__" "Equation" "value")
  1906. ("__Listing__" "Listing" "value"))))
  1907. (car (org-e-odt-format-label caption-from info 'definition))))
  1908. (formula-tree
  1909. (org-e-odt--adopt-elements
  1910. `(table (:type org :attr_odt (":style \"OrgEquation\"")))
  1911. (org-e-odt--adopt-elements
  1912. `(table-row (:type standard))
  1913. `(table-cell nil "<c8>") `(table-cell nil "<c1>"))
  1914. (org-e-odt--adopt-elements
  1915. `(table-row (:type standard))
  1916. (org-e-odt--adopt-elements
  1917. `(table-cell nil) `(export-block
  1918. (:type "ODT" :value ,equation)))
  1919. (org-e-odt--adopt-elements
  1920. `(table-cell nil) `(export-block
  1921. (:type "ODT" :value ,label))))))
  1922. (formula-info
  1923. (org-export-collect-tree-properties
  1924. formula-tree (org-export-get-environment 'e-odt))))
  1925. (org-export-data formula-tree formula-info))))))
  1926. (defun org-e-odt-copy-formula-file (src-file)
  1927. "Returns the internal name of the file"
  1928. (let* ((target-dir (format "Formula-%04d/"
  1929. (incf org-e-odt-embedded-formulas-count)))
  1930. (target-file (concat target-dir "content.xml")))
  1931. ;; Create a directory for holding formula file. Also enter it in
  1932. ;; to manifest.
  1933. (make-directory (concat org-e-odt-zip-dir target-dir))
  1934. (org-e-odt-create-manifest-file-entry
  1935. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  1936. ;; Copy over the formula file from user directory to zip
  1937. ;; directory.
  1938. (message "Embedding %s as %s ..." src-file target-file)
  1939. (let ((case-fold-search nil))
  1940. (cond
  1941. ;; Case 1: Mathml.
  1942. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  1943. (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
  1944. ;; Case 2: OpenDocument formula.
  1945. ((string-match "\\.odf\\'" src-file)
  1946. (org-e-odt--zip-extract src-file "content.xml"
  1947. (concat org-e-odt-zip-dir target-dir)))
  1948. (t (error "%s is not a formula file" src-file))))
  1949. ;; Enter the formula file in to manifest.
  1950. (org-e-odt-create-manifest-file-entry "text/xml" target-file)
  1951. target-file))
  1952. ;;;; Targets
  1953. (defun org-e-odt-format-target (text id)
  1954. (let ((name (concat org-e-odt-bookmark-prefix id)))
  1955. (concat
  1956. (and id (format "\n<text:bookmark-start text:name=\"%s\"/>" name))
  1957. (concat (and id (format "\n<text:bookmark text:name=\"%s\"/>" id)) text)
  1958. (and id (format "\n<text:bookmark-end text:name=\"%s\"/>" name)))))
  1959. (defun org-e-odt-link--inline-image (element info)
  1960. "Return HTML code for an inline image.
  1961. LINK is the link pointing to the inline image. INFO is a plist
  1962. used as a communication channel."
  1963. (let* ((src (cond
  1964. ((eq (org-element-type element) 'link)
  1965. (let* ((type (org-element-property :type element))
  1966. (raw-path (org-element-property :path element)))
  1967. (cond ((member type '("http" "https"))
  1968. (concat type ":" raw-path))
  1969. ((file-name-absolute-p raw-path)
  1970. (expand-file-name raw-path))
  1971. (t raw-path))))
  1972. ((member (org-element-type element)
  1973. '(latex-fragment latex-environment))
  1974. (let* ((latex-frag (org-remove-indentation
  1975. (org-element-property :value element)))
  1976. (formula-link (org-e-odt-format-latex
  1977. latex-frag 'dvipng info)))
  1978. (and formula-link
  1979. (string-match "file:\\([^]]*\\)" formula-link)
  1980. (match-string 1 formula-link))))
  1981. (t (error "what is this?"))))
  1982. (src-expanded (if (file-name-absolute-p src) src
  1983. (expand-file-name src (file-name-directory
  1984. (plist-get info :input-file)))))
  1985. (href (format
  1986. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  1987. (org-e-odt-copy-image-file src-expanded)))
  1988. ;; extract attributes from #+ATTR_ODT line.
  1989. (attr-from (case (org-element-type element)
  1990. (link (org-export-get-parent-element element))
  1991. (t element)))
  1992. ;; convert attributes to a plist.
  1993. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  1994. ;; handle `:anchor', `:style' and `:attributes' properties.
  1995. (user-frame-anchor
  1996. (car (assoc-string (plist-get attr-plist :anchor)
  1997. '(("as-char") ("paragraph") ("page")) t)))
  1998. (user-frame-style
  1999. (and user-frame-anchor (plist-get attr-plist :style)))
  2000. (user-frame-attrs
  2001. (and user-frame-anchor (plist-get attr-plist :attributes)))
  2002. (user-frame-params
  2003. (list user-frame-style user-frame-attrs user-frame-anchor))
  2004. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  2005. ;; extrac
  2006. ;; handle `:width', `:height' and `:scale' properties.
  2007. (size (org-e-odt-image-size-from-file
  2008. src-expanded (plist-get attr-plist :width)
  2009. (plist-get attr-plist :height)
  2010. (plist-get attr-plist :scale) nil ;; embed-as
  2011. "paragraph" ; FIXME
  2012. ))
  2013. (width (car size)) (height (cdr size))
  2014. (embed-as
  2015. (case (org-element-type element)
  2016. ((org-e-odt-standalone-image-p element info) "paragraph")
  2017. (latex-fragment "as-char")
  2018. (latex-environment "paragraph")
  2019. (t "paragraph")))
  2020. (captions (org-e-odt-format-label element info 'definition))
  2021. (caption (car captions)) (short-caption (cdr captions))
  2022. (entity (concat (and caption "Captioned") embed-as "Image")))
  2023. (org-e-odt-format-entity entity href width height
  2024. captions user-frame-params )))
  2025. (defun org-e-odt-format-entity (entity href width height &optional
  2026. captions user-frame-params)
  2027. (let* ((caption (car captions)) (short-caption (cdr captions))
  2028. (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
  2029. default-frame-params frame-params
  2030. (--merge-frame-params
  2031. (function
  2032. (lambda (default-frame-params user-frame-params)
  2033. (if (not user-frame-params) default-frame-params
  2034. (assert (= (length default-frame-params) 3))
  2035. (assert (= (length user-frame-params) 3))
  2036. (loop for user-frame-param in user-frame-params
  2037. for default-frame-param in default-frame-params
  2038. collect (or user-frame-param default-frame-param)))))))
  2039. (cond
  2040. ((not caption)
  2041. (setq default-frame-params (nth 2 entity-style))
  2042. (setq frame-params (funcall --merge-frame-params
  2043. default-frame-params user-frame-params))
  2044. (apply 'org-e-odt--frame href width height frame-params))
  2045. (t
  2046. (setq default-frame-params (nth 3 entity-style))
  2047. (setq frame-params (funcall --merge-frame-params
  2048. default-frame-params user-frame-params))
  2049. (apply 'org-e-odt--textbox
  2050. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2051. "Illustration"
  2052. (concat
  2053. (apply 'org-e-odt--frame href width height
  2054. (let ((entity-style-1 (copy-sequence
  2055. (nth 2 entity-style))))
  2056. (setcar (cdr entity-style-1)
  2057. (concat
  2058. (cadr entity-style-1)
  2059. (and short-caption
  2060. (format " draw:name=\"%s\" "
  2061. short-caption))))
  2062. entity-style-1))
  2063. caption))
  2064. width height frame-params)))))
  2065. (defun org-e-odt-standalone-image-p (element info)
  2066. "Test if ELEMENT is a standalone image for the purpose ODT export.
  2067. INFO is a plist holding contextual information.
  2068. Return non-nil, if ELEMENT is of type paragraph and it's sole
  2069. content, save for whitespaces, is a link that qualifies as an
  2070. inline image.
  2071. Return non-nil, if ELEMENT is of type link and it's containing
  2072. paragraph has no other content save for leading and trailing
  2073. whitespaces.
  2074. Return nil, otherwise.
  2075. Bind `org-e-odt-standalone-image-predicate' to constrain
  2076. paragraph further. For example, to check for only captioned
  2077. standalone images, do the following.
  2078. \(setq org-e-odt-standalone-image-predicate
  2079. \(lambda \(paragraph\)
  2080. \(org-element-property :caption paragraph\)\)\)
  2081. "
  2082. (let ((--standalone-image-predicate
  2083. (function (lambda (paragraph)
  2084. (or (org-element-property :caption paragraph)
  2085. (org-element-property :name paragraph)))))
  2086. (paragraph (case (org-element-type element)
  2087. (paragraph element)
  2088. (link (and (org-export-inline-image-p
  2089. element org-e-odt-inline-image-rules)
  2090. (org-export-get-parent element)))
  2091. (t nil))))
  2092. (when paragraph
  2093. (assert (eq (org-element-type paragraph) 'paragraph))
  2094. (when (funcall --standalone-image-predicate paragraph)
  2095. (let ((contents (org-element-contents paragraph)))
  2096. (loop for x in contents
  2097. with inline-image-count = 0
  2098. always (cond
  2099. ((eq (org-element-type x) 'plain-text)
  2100. (not (org-string-nw-p x)))
  2101. ((eq (org-element-type x) 'link)
  2102. (when (org-export-inline-image-p
  2103. x org-e-odt-inline-image-rules)
  2104. (= (incf inline-image-count) 1)))
  2105. (t nil))))))))
  2106. (defun org-e-odt-link (link desc info)
  2107. "Transcode a LINK object from Org to ODT.
  2108. DESC is the description part of the link, or the empty string.
  2109. INFO is a plist holding contextual information. See
  2110. `org-export-data'."
  2111. (let* ((type (org-element-property :type link))
  2112. (raw-path (org-element-property :path link))
  2113. ;; Ensure DESC really exists, or set it to nil.
  2114. (desc (and (not (string= desc "")) desc))
  2115. (imagep (org-export-inline-image-p
  2116. link org-e-odt-inline-image-rules))
  2117. (path (cond
  2118. ((member type '("http" "https" "ftp" "mailto"))
  2119. (concat type ":" raw-path))
  2120. ((string= type "file")
  2121. (when (string-match "\\(.+\\)::.+" raw-path)
  2122. (setq raw-path (match-string 1 raw-path)))
  2123. (if (file-name-absolute-p raw-path)
  2124. (concat "file://" (expand-file-name raw-path))
  2125. (concat "file://" raw-path)))
  2126. (t raw-path)))
  2127. protocol)
  2128. (cond
  2129. ;; Image file.
  2130. ((and (not desc) (org-export-inline-image-p
  2131. link org-e-odt-inline-image-rules))
  2132. (org-e-odt-link--inline-image link info))
  2133. ;; Radio target: Transcode target's contents and use them as
  2134. ;; link's description.
  2135. ((string= type "radio")
  2136. (let ((destination (org-export-resolve-radio-link link info)))
  2137. (when destination
  2138. (org-e-odt-format-internal-link
  2139. (org-export-data (org-element-contents destination) info)
  2140. (org-export-solidify-link-text path)))))
  2141. ;; Links pointing to an headline: Find destination and build
  2142. ;; appropriate referencing command.
  2143. ((member type '("custom-id" "fuzzy" "id"))
  2144. (let ((destination (if (string= type "fuzzy")
  2145. (org-export-resolve-fuzzy-link link info)
  2146. (org-export-resolve-id-link link info))))
  2147. (case (org-element-type destination)
  2148. ;; Fuzzy link points nowhere.
  2149. ('nil
  2150. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2151. "Emphasis" (or desc (org-export-data
  2152. (org-element-property
  2153. :raw-link link) info))))
  2154. ;; Fuzzy link points to an invisible target.
  2155. (keyword nil)
  2156. ;; LINK points to an headline. Check if LINK should display
  2157. ;; section numbers.
  2158. (headline
  2159. (let* ((headline-no (org-export-get-headline-number destination info))
  2160. (label (format "sec-%s" (mapconcat 'number-to-string
  2161. headline-no "-")))
  2162. (desc
  2163. ;; Case 1: Headline is numbered and LINK has no
  2164. ;; description or LINK's description matches
  2165. ;; headline's title. Display section number.
  2166. (if (and (org-export-numbered-headline-p destination info)
  2167. (or (not desc)
  2168. (string= desc (org-element-property
  2169. :raw-value destination))))
  2170. headline-no
  2171. ;; Case 2: Either the headline is un-numbered or
  2172. ;; LINK has a custom description. Display LINK's
  2173. ;; description or headline's title.
  2174. (or desc (org-export-data (org-element-property
  2175. :title destination) info)))))
  2176. (org-e-odt-format-internal-link desc label)))
  2177. ;; Fuzzy link points to a target. Do as above.
  2178. (otherwise
  2179. ;; (unless desc
  2180. ;; (setq number (cond
  2181. ;; ((org-e-odt-standalone-image-p destination info)
  2182. ;; (org-export-get-ordinal
  2183. ;; (assoc 'link (org-element-contents destination))
  2184. ;; info 'link 'org-e-odt-standalone-image-p))
  2185. ;; (t (org-export-get-ordinal destination info))))
  2186. ;; (setq desc (when number
  2187. ;; (if (atom number) (number-to-string number)
  2188. ;; (mapconcat 'number-to-string number ".")))))
  2189. (let ((label-reference
  2190. (org-e-odt-format-label destination info 'reference)))
  2191. (assert label-reference)
  2192. label-reference)))))
  2193. ;; Coderef: replace link with the reference name or the
  2194. ;; equivalent line number.
  2195. ((string= type "coderef")
  2196. (let* ((fmt (org-export-get-coderef-format path desc))
  2197. (res (org-export-resolve-coderef path info))
  2198. (href (concat "#coderef-" path)))
  2199. (format fmt (org-e-odt-format-link res href))))
  2200. ;; Link type is handled by a special function.
  2201. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2202. (funcall protocol (org-link-unescape path) desc 'odt))
  2203. ;; External link with a description part.
  2204. ((and path desc) (org-e-odt-format-link desc path))
  2205. ;; External link without a description part.
  2206. (path (org-e-odt-format-link path path))
  2207. ;; No path, only description. Try to do something useful.
  2208. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2209. "Emphasis" desc)))))
  2210. ;;;; Babel Call
  2211. ;; Babel Calls are ignored.
  2212. ;;;; Macro
  2213. (defun org-e-odt-macro (macro contents info)
  2214. "Transcode a MACRO element from Org to ODT.
  2215. CONTENTS is nil. INFO is a plist holding contextual information."
  2216. ;; Use available tools.
  2217. (org-export-expand-macro macro info))
  2218. ;;;; Paragraph
  2219. (defun org-e-odt-paragraph (paragraph contents info)
  2220. "Transcode a PARAGRAPH element from Org to ODT.
  2221. CONTENTS is the contents of the paragraph, as a string. INFO is
  2222. the plist used as a communication channel."
  2223. (let* ((parent (org-export-get-parent paragraph))
  2224. (parent-type (org-element-type parent))
  2225. (style (case parent-type
  2226. (quote-block "Quotations")
  2227. (center-block "OrgCenter")
  2228. (footnote-definition "Footnote")
  2229. (t "Text_20_body"))))
  2230. ;; If this paragraph is a leading paragraph in a non-descriptive
  2231. ;; item and the item has a checkbox, splice the checkbox and
  2232. ;; paragraph contents together.
  2233. (when (and (eq (org-element-type parent) 'item)
  2234. (not (eq (org-element-property :type
  2235. (org-export-get-parent parent))
  2236. 'descriptive))
  2237. (eq paragraph (car (org-element-contents parent))))
  2238. (setq contents (concat (org-e-odt--checkbox parent) contents)))
  2239. (assert style)
  2240. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2241. ;;;; Plain List
  2242. (defun org-e-odt-plain-list (plain-list contents info)
  2243. "Transcode a PLAIN-LIST element from Org to ODT.
  2244. CONTENTS is the contents of the list. INFO is a plist holding
  2245. contextual information."
  2246. (let* ((type (org-element-property :type plain-list))
  2247. (continue-numbering nil))
  2248. (assert (member type '(ordered unordered descriptive)))
  2249. (org-e-odt--wrap-label
  2250. plain-list
  2251. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2252. (assoc-default type '((ordered . "OrgNumberedList")
  2253. (unordered . "OrgBulletedList")
  2254. (descriptive . "OrgDescriptionList")))
  2255. ;; If top-level list, re-start numbering. Otherwise,
  2256. ;; continue numbering.
  2257. (format "text:continue-numbering=\"%s\""
  2258. (let* ((parent (org-export-get-parent plain-list)))
  2259. (if (and parent (eq (org-element-type parent) 'item))
  2260. "true" "false")))
  2261. contents))))
  2262. ;;;; Plain Text
  2263. (defun org-e-odt-fill-tabs-and-spaces (line)
  2264. (replace-regexp-in-string
  2265. "\\([\t]\\|\\([ ]+\\)\\)"
  2266. (lambda (s)
  2267. (cond
  2268. ((string= s "\t") "<text:tab/>")
  2269. (t (let ((n (length s)))
  2270. (cond
  2271. ((= n 1) " ")
  2272. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2273. (t ""))))))
  2274. line))
  2275. (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling)
  2276. (mapc
  2277. (lambda (pair)
  2278. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2279. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2280. (if no-whitespace-filling text
  2281. (org-e-odt-fill-tabs-and-spaces text)))
  2282. (defun org-e-odt--quotation-marks (text info)
  2283. "Export quotation marks depending on language conventions.
  2284. TEXT is a string containing quotation marks to be replaced. INFO
  2285. is a plist used as a communication channel."
  2286. (mapc (lambda(l)
  2287. (let ((start 0))
  2288. (while (setq start (string-match (car l) text start))
  2289. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  2290. (setq text (replace-match new-quote t t text))))))
  2291. (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
  2292. ;; Falls back on English.
  2293. (assoc "en" org-e-odt-quotes))))
  2294. text)
  2295. (defun org-e-odt-plain-text (text info)
  2296. "Transcode a TEXT string from Org to ODT.
  2297. TEXT is the string to transcode. INFO is a plist holding
  2298. contextual information."
  2299. ;; Protect &, < and >.
  2300. (setq text (org-e-odt-encode-plain-text text t))
  2301. ;; Handle quotation marks
  2302. (setq text (org-e-odt--quotation-marks text info))
  2303. ;; Convert special strings.
  2304. (when (plist-get info :with-special-strings)
  2305. (mapc
  2306. (lambda (pair)
  2307. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
  2308. org-e-odt-special-string-regexps))
  2309. ;; Handle break preservation if required.
  2310. (when (plist-get info :preserve-breaks)
  2311. (setq text (replace-regexp-in-string
  2312. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
  2313. ;; Return value.
  2314. text)
  2315. ;;;; Planning
  2316. (defun org-e-odt-planning (planning contents info)
  2317. "Transcode a PLANNING element from Org to ODT.
  2318. CONTENTS is nil. INFO is a plist used as a communication
  2319. channel."
  2320. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2321. "OrgTimestampWrapper"
  2322. (concat
  2323. (let ((closed (org-element-property :closed planning)))
  2324. (when closed
  2325. (concat
  2326. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2327. "OrgTimestampKeyword" org-closed-string)
  2328. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2329. "OrgTimestamp" (org-translate-time closed)))))
  2330. (let ((deadline (org-element-property :deadline planning)))
  2331. (when deadline
  2332. (concat
  2333. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2334. "OrgTimestampKeyword" org-deadline-string)
  2335. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2336. "OrgTimestamp" (org-translate-time deadline)))))
  2337. (let ((scheduled (org-element-property :scheduled planning)))
  2338. (when scheduled
  2339. (concat
  2340. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2341. "OrgTimestampKeyword" org-scheduled-string)
  2342. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2343. "OrgTimestamp" (org-translate-time scheduled))))))))
  2344. ;;;; Property Drawer
  2345. (defun org-e-odt-property-drawer (property-drawer contents info)
  2346. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2347. CONTENTS is nil. INFO is a plist holding contextual
  2348. information."
  2349. ;; The property drawer isn't exported but we want separating blank
  2350. ;; lines nonetheless.
  2351. "")
  2352. ;;;; Quote Block
  2353. (defun org-e-odt-quote-block (quote-block contents info)
  2354. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2355. CONTENTS holds the contents of the block. INFO is a plist
  2356. holding contextual information."
  2357. (org-e-odt--wrap-label quote-block contents))
  2358. ;;;; Quote Section
  2359. (defun org-e-odt-quote-section (quote-section contents info)
  2360. "Transcode a QUOTE-SECTION element from Org to ODT.
  2361. CONTENTS is nil. INFO is a plist holding contextual information."
  2362. (let ((value (org-remove-indentation
  2363. (org-element-property :value quote-section))))
  2364. (when value (org-e-odt-do-format-code value))))
  2365. ;;;; Section
  2366. (defun org-e-odt-format-section (text style &optional name)
  2367. (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
  2368. (format "\n<text:section text:style-name=\"%s\" %s>\n%s</text:section>"
  2369. style
  2370. (format "text:name=\"%s\"" (or name default-name))
  2371. text)))
  2372. (defun org-e-odt-section (section contents info) ; FIXME
  2373. "Transcode a SECTION element from Org to ODT.
  2374. CONTENTS holds the contents of the section. INFO is a plist
  2375. holding contextual information."
  2376. contents)
  2377. ;;;; Radio Target
  2378. (defun org-e-odt-radio-target (radio-target text info)
  2379. "Transcode a RADIO-TARGET object from Org to ODT.
  2380. TEXT is the text of the target. INFO is a plist holding
  2381. contextual information."
  2382. (org-e-odt-format-target
  2383. text (org-export-solidify-link-text
  2384. (org-element-property :value radio-target))))
  2385. ;;;; Special Block
  2386. (defun org-e-odt-special-block (special-block contents info)
  2387. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2388. CONTENTS holds the contents of the block. INFO is a plist
  2389. holding contextual information."
  2390. (let ((type (downcase (org-element-property :type special-block)))
  2391. (attributes (org-export-read-attribute :attr_odt special-block)))
  2392. (org-e-odt--wrap-label
  2393. special-block
  2394. (cond
  2395. ;; Annotation.
  2396. ((string= type "annotation")
  2397. (let ((author (or (plist-get attributes :author)
  2398. (let ((author (plist-get info :author)))
  2399. (and author (org-export-data author info)))))
  2400. (date (or (plist-get attributes :date)
  2401. (plist-get info :date))))
  2402. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2403. "Text_20_body"
  2404. (format "<office:annotation>\n%s\n</office:annotation>"
  2405. (concat
  2406. (and author
  2407. (format "<dc:creator>%s</dc:creator>" author))
  2408. (and date
  2409. (format "<dc:date>%s</dc:date>"
  2410. (org-e-odt--date date)))
  2411. contents)))))
  2412. ;; Textbox.
  2413. ((string= type "textbox")
  2414. (let ((width (plist-get attributes :width))
  2415. (height (plist-get attributes :height))
  2416. (style (plist-get attributes :style))
  2417. (extra (plist-get attributes :extra))
  2418. (anchor (plist-get attributes :anchor)))
  2419. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2420. "Text_20_body" (org-e-odt--textbox contents width height
  2421. style extra anchor))))
  2422. (t contents)))))
  2423. ;;;; Src Block
  2424. (defun org-e-odt-hfy-face-to-css (fn)
  2425. "Create custom style for face FN.
  2426. When FN is the default face, use it's foreground and background
  2427. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2428. use it's color attribute to create a character style whose name
  2429. is obtained from FN. Currently all attributes of FN other than
  2430. color are ignored.
  2431. The style name for a face FN is derived using the following
  2432. operations on the face name in that order - de-dash, CamelCase
  2433. and prefix with \"OrgSrc\". For example,
  2434. `font-lock-function-name-face' is associated with
  2435. \"OrgSrcFontLockFunctionNameFace\"."
  2436. (let* ((css-list (hfy-face-to-style fn))
  2437. (style-name ((lambda (fn)
  2438. (concat "OrgSrc"
  2439. (mapconcat
  2440. 'capitalize (split-string
  2441. (hfy-face-or-def-to-name fn) "-")
  2442. ""))) fn))
  2443. (color-val (cdr (assoc "color" css-list)))
  2444. (background-color-val (cdr (assoc "background" css-list)))
  2445. (style (and org-e-odt-create-custom-styles-for-srcblocks
  2446. (cond
  2447. ((eq fn 'default)
  2448. (format org-e-odt-src-block-paragraph-format
  2449. background-color-val color-val))
  2450. (t
  2451. (format
  2452. "
  2453. <style:style style:name=\"%s\" style:family=\"text\">
  2454. <style:text-properties fo:color=\"%s\"/>
  2455. </style:style>" style-name color-val))))))
  2456. (cons style-name style)))
  2457. (defun org-e-odt-htmlfontify-string (line)
  2458. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2459. (hfy-html-quote-map '(("\"" "&quot;")
  2460. ("<" "&lt;")
  2461. ("&" "&amp;")
  2462. (">" "&gt;")
  2463. (" " "<text:s/>")
  2464. (" " "<text:tab/>")))
  2465. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2466. (hfy-optimisations-1 (copy-seq hfy-optimisations))
  2467. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2468. 'body-text-only))
  2469. (hfy-begin-span-handler
  2470. (lambda (style text-block text-id text-begins-block-p)
  2471. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2472. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2473. (with-no-warnings (htmlfontify-string line))))
  2474. (defun org-e-odt-do-format-code
  2475. (code &optional lang refs retain-labels num-start)
  2476. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2477. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2478. (code-lines (org-split-string code "\n"))
  2479. (code-length (length code-lines))
  2480. (use-htmlfontify-p (and (functionp lang-mode)
  2481. org-e-odt-fontify-srcblocks
  2482. (require 'htmlfontify nil t)
  2483. (fboundp 'htmlfontify-string)))
  2484. (code (if (not use-htmlfontify-p) code
  2485. (with-temp-buffer
  2486. (insert code)
  2487. (funcall lang-mode)
  2488. (font-lock-fontify-buffer)
  2489. (buffer-string))))
  2490. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2491. 'org-e-odt-encode-plain-text))
  2492. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2493. "OrgFixedWidthBlock"))
  2494. (i 0))
  2495. (assert (= code-length (length (org-split-string code "\n"))))
  2496. (setq code
  2497. (org-export-format-code
  2498. code
  2499. (lambda (loc line-num ref)
  2500. (setq par-style
  2501. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2502. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2503. (setq loc (funcall fontifier loc))
  2504. (when ref
  2505. (setq loc (org-e-odt-format-target loc (concat "coderef-" ref))))
  2506. (assert par-style)
  2507. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2508. par-style loc))
  2509. (if (not line-num) loc
  2510. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2511. num-start refs))
  2512. (cond
  2513. ((not num-start) code)
  2514. ((= num-start 0)
  2515. (format
  2516. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2517. " text:continue-numbering=\"false\"" code))
  2518. (t
  2519. (format
  2520. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2521. " text:continue-numbering=\"true\"" code)))))
  2522. (defun org-e-odt-format-code (element info)
  2523. (let* ((lang (org-element-property :language element))
  2524. ;; Extract code and references.
  2525. (code-info (org-export-unravel-code element))
  2526. (code (car code-info))
  2527. (refs (cdr code-info))
  2528. ;; Does the src block contain labels?
  2529. (retain-labels (org-element-property :retain-labels element))
  2530. ;; Does it have line numbers?
  2531. (num-start (case (org-element-property :number-lines element)
  2532. (continued (org-export-get-loc element info))
  2533. (new 0))))
  2534. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2535. (defun org-e-odt-src-block (src-block contents info)
  2536. "Transcode a SRC-BLOCK element from Org to ODT.
  2537. CONTENTS holds the contents of the item. INFO is a plist holding
  2538. contextual information."
  2539. (let* ((lang (org-element-property :language src-block))
  2540. (caption (org-element-property :caption src-block))
  2541. (short-caption (and (cdr caption)
  2542. (org-export-data (cdr caption) info)))
  2543. (caption (and (car caption) (org-export-data (car caption) info)))
  2544. (label (org-element-property :name src-block))
  2545. (attributes (org-export-read-attribute :attr_odt src-block)))
  2546. ;; FIXME: Handle caption
  2547. ;; caption-str (when caption)
  2548. ;; (main (org-export-data (car caption) info))
  2549. ;; (secondary (org-export-data (cdr caption) info))
  2550. ;; (caption-str (org-e-odt--caption/label-string caption label info))
  2551. (let* ((captions (org-e-odt-format-label src-block info 'definition))
  2552. (caption (car captions)) (short-caption (cdr captions)))
  2553. (concat
  2554. (and caption
  2555. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2556. "Listing" caption))
  2557. (let ((--src-block (org-e-odt-format-code src-block info)))
  2558. (if (not (plist-get attributes :textbox)) --src-block
  2559. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2560. "Text_20_body"
  2561. (org-e-odt--textbox --src-block nil nil nil))))))))
  2562. ;;;; Statistics Cookie
  2563. (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
  2564. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2565. CONTENTS is nil. INFO is a plist holding contextual information."
  2566. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2567. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2568. "OrgCode" cookie-value)))
  2569. ;;;; Strike-Through
  2570. (defun org-e-odt-strike-through (strike-through contents info)
  2571. "Transcode STRIKE-THROUGH from Org to ODT.
  2572. CONTENTS is the text with strike-through markup. INFO is a plist
  2573. holding contextual information."
  2574. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2575. "Strikethrough" contents))
  2576. ;;;; Subscript
  2577. (defun org-e-odt-subscript (subscript contents info)
  2578. "Transcode a SUBSCRIPT object from Org to ODT.
  2579. CONTENTS is the contents of the object. INFO is a plist holding
  2580. contextual information."
  2581. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2582. "OrgSubscript" contents))
  2583. ;;;; Superscript
  2584. (defun org-e-odt-superscript (superscript contents info)
  2585. "Transcode a SUPERSCRIPT object from Org to ODT.
  2586. CONTENTS is the contents of the object. INFO is a plist holding
  2587. contextual information."
  2588. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2589. "OrgSuperscript" contents))
  2590. ;;;; Table Cell
  2591. (defun org-e-odt-table-style-spec (element info)
  2592. (let* ((table (org-export-get-parent-table element))
  2593. (table-attributes (org-export-read-attribute :attr_odt table))
  2594. (table-style (plist-get table-attributes :style)))
  2595. (assoc table-style org-e-odt-table-styles)))
  2596. (defun org-e-odt-get-table-cell-styles (table-cell info)
  2597. "Retrieve styles applicable to a table cell.
  2598. R and C are (zero-based) row and column numbers of the table
  2599. cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
  2600. applicable to the current table. It is `nil' if the table is not
  2601. associated with any style attributes.
  2602. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2603. When STYLE-SPEC is nil, style the table cell the conventional way
  2604. - choose cell borders based on row and column groupings and
  2605. choose paragraph alignment based on `org-col-cookies' text
  2606. property. See also
  2607. `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
  2608. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2609. styles congruent with the ODF-1.2 specification."
  2610. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2611. (r (car table-cell-address)) (c (cdr table-cell-address))
  2612. (style-spec (org-e-odt-table-style-spec table-cell info))
  2613. (table-dimensions (org-export-table-dimensions
  2614. (org-export-get-parent-table table-cell)
  2615. info)))
  2616. (when style-spec
  2617. ;; LibreOffice - particularly the Writer - honors neither table
  2618. ;; templates nor custom table-cell styles. Inorder to retain
  2619. ;; inter-operability with LibreOffice, only automatic styles are
  2620. ;; used for styling of table-cells. The current implementation is
  2621. ;; congruent with ODF-1.2 specification and hence is
  2622. ;; future-compatible.
  2623. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2624. ;; which recognizes as many as 16 different cell types - is much
  2625. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2626. ;; by hand.
  2627. (let* ((template-name (nth 1 style-spec))
  2628. (cell-style-selectors (nth 2 style-spec))
  2629. (cell-type
  2630. (cond
  2631. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2632. (= c 0)) "FirstColumn")
  2633. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2634. (= (1+ c) (cdr table-dimensions)))
  2635. "LastColumn")
  2636. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2637. (= r 0)) "FirstRow")
  2638. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2639. (= (1+ r) (car table-dimensions)))
  2640. "LastRow")
  2641. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2642. (= (% r 2) 1)) "EvenRow")
  2643. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2644. (= (% r 2) 0)) "OddRow")
  2645. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2646. (= (% c 2) 1)) "EvenColumn")
  2647. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2648. (= (% c 2) 0)) "OddColumn")
  2649. (t ""))))
  2650. (concat template-name cell-type)))))
  2651. (defun org-e-odt-table-cell (table-cell contents info)
  2652. "Transcode a TABLE-CELL element from Org to ODT.
  2653. CONTENTS is nil. INFO is a plist used as a communication
  2654. channel."
  2655. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2656. (r (car table-cell-address))
  2657. (c (cdr table-cell-address))
  2658. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2659. (table-row (org-export-get-parent table-cell))
  2660. (custom-style-prefix (org-e-odt-get-table-cell-styles
  2661. table-cell info))
  2662. (paragraph-style
  2663. (or
  2664. (and custom-style-prefix
  2665. (format "%sTableParagraph" custom-style-prefix))
  2666. (concat
  2667. (cond
  2668. ((and (= 1 (org-export-table-row-group table-row info))
  2669. (org-export-table-has-header-p
  2670. (org-export-get-parent-table table-row) info))
  2671. "OrgTableHeading")
  2672. ((let* ((table (org-export-get-parent-table table-cell))
  2673. (table-attrs (org-export-read-attribute :attr_odt table))
  2674. (table-header-columns (plist-get table-attrs
  2675. :header-columns)))
  2676. (<= c (cond ((wholenump table-header-columns)
  2677. (- table-header-columns 1))
  2678. (table-header-columns 0)
  2679. (t -1))))
  2680. "OrgTableHeading")
  2681. (t "OrgTableContents"))
  2682. (capitalize (symbol-name (org-export-table-cell-alignment
  2683. table-cell info))))))
  2684. (cell-style-name
  2685. (or
  2686. (and custom-style-prefix (format "%sTableCell"
  2687. custom-style-prefix))
  2688. (concat
  2689. "OrgTblCell"
  2690. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2691. (zerop r)) "T")
  2692. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2693. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2694. (not (zerop c)) ) "L"))))
  2695. (cell-attributes
  2696. (concat
  2697. (format " table:style-name=\"%s\"" cell-style-name)
  2698. (and (> horiz-span 0)
  2699. (format " table:number-columns-spanned=\"%d\""
  2700. (1+ horiz-span))))))
  2701. (unless contents (setq contents ""))
  2702. (concat
  2703. (assert paragraph-style)
  2704. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2705. cell-attributes
  2706. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2707. paragraph-style contents))
  2708. (let (s)
  2709. (dotimes (i horiz-span s)
  2710. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2711. "\n")))
  2712. ;;;; Table Row
  2713. (defun org-e-odt-table-row (table-row contents info)
  2714. "Transcode a TABLE-ROW element from Org to ODT.
  2715. CONTENTS is the contents of the row. INFO is a plist used as a
  2716. communication channel."
  2717. ;; Rules are ignored since table separators are deduced from
  2718. ;; borders of the current row.
  2719. (when (eq (org-element-property :type table-row) 'standard)
  2720. (let* ((rowgroup-tags
  2721. (if (and (= 1 (org-export-table-row-group table-row info))
  2722. (org-export-table-has-header-p
  2723. (org-export-get-parent-table table-row) info))
  2724. ;; If the row belongs to the first rowgroup and the
  2725. ;; table has more than one row groups, then this row
  2726. ;; belongs to the header row group.
  2727. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2728. ;; Otherwise, it belongs to non-header row group.
  2729. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2730. (concat
  2731. ;; Does this row begin a rowgroup?
  2732. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2733. (car rowgroup-tags))
  2734. ;; Actual table row
  2735. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2736. ;; Does this row end a rowgroup?
  2737. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2738. (cdr rowgroup-tags))))))
  2739. ;;;; Table
  2740. (defun org-e-odt-table-first-row-data-cells (table info)
  2741. (let ((table-row
  2742. (org-element-map
  2743. table 'table-row
  2744. (lambda (row)
  2745. (unless (eq (org-element-property :type row) 'rule) row))
  2746. info 'first-match))
  2747. (special-column-p (org-export-table-has-special-column-p table)))
  2748. (if (not special-column-p) (org-element-contents table-row)
  2749. (cdr (org-element-contents table-row)))))
  2750. (defun org-e-odt--table (table contents info)
  2751. "Transcode a TABLE element from Org to ODT.
  2752. CONTENTS is the contents of the table. INFO is a plist holding
  2753. contextual information."
  2754. (case (org-element-property :type table)
  2755. ;; Case 1: table.el doesn't support export to OD format. Strip
  2756. ;; such tables from export.
  2757. (table.el
  2758. (prog1 nil
  2759. (message
  2760. (concat
  2761. "(org-e-odt): Found table.el-type table in the source Org file."
  2762. " table.el doesn't support export to ODT format."
  2763. " Stripping the table from export."))))
  2764. ;; Case 2: Native Org tables.
  2765. (otherwise
  2766. (let* ((captions (org-e-odt-format-label table info 'definition))
  2767. (caption (car captions)) (short-caption (cdr captions))
  2768. (attributes (org-export-read-attribute :attr_odt table))
  2769. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  2770. (table-column-specs
  2771. (function
  2772. (lambda (table info)
  2773. (let* ((table-style (or custom-table-style "OrgTable"))
  2774. (column-style (format "%sColumn" table-style)))
  2775. (mapconcat
  2776. (lambda (table-cell)
  2777. (let ((width (1+ (or (org-export-table-cell-width
  2778. table-cell info) 0)))
  2779. (s (format
  2780. "\n<table:table-column table:style-name=\"%s\"/>"
  2781. column-style))
  2782. out)
  2783. (dotimes (i width out) (setq out (concat s out)))))
  2784. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  2785. (concat
  2786. ;; caption.
  2787. (when caption
  2788. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2789. "Table" caption))
  2790. ;; begin table.
  2791. (let* ((automatic-name
  2792. (org-e-odt-add-automatic-style "Table" attributes)))
  2793. (format
  2794. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
  2795. (or short-caption (car automatic-name))
  2796. (or custom-table-style (cdr automatic-name) "OrgTable")))
  2797. ;; column specification.
  2798. (funcall table-column-specs table info)
  2799. ;; actual contents.
  2800. "\n" contents
  2801. ;; end table.
  2802. "</table:table>")))))
  2803. (defun org-e-odt-table (table contents info)
  2804. "Transcode a TABLE element from Org to ODT.
  2805. CONTENTS is the contents of the table. INFO is a plist holding
  2806. contextual information."
  2807. (let* ((--get-previous-elements
  2808. (function
  2809. (lambda (blob info)
  2810. (let ((parent (org-export-get-parent blob)))
  2811. (cdr (member blob (reverse (org-element-contents parent))))))))
  2812. (--element-preceded-by-table-p
  2813. (function
  2814. (lambda (element info)
  2815. (loop for el in (funcall --get-previous-elements element info)
  2816. thereis (eq (org-element-type el) 'table)))))
  2817. (--walk-list-genealogy-and-collect-tags
  2818. (function
  2819. (lambda (table info)
  2820. (let* ((genealogy (org-export-get-genealogy table))
  2821. (list-genealogy
  2822. (when (eq (org-element-type (car genealogy)) 'item)
  2823. (loop for el in genealogy
  2824. when (member (org-element-type el)
  2825. '(item plain-list))
  2826. collect el))))
  2827. (loop for el in list-genealogy
  2828. with parent-list collect
  2829. (case (org-element-type el)
  2830. (plain-list
  2831. (setq parent-list el)
  2832. `("</text:list>"
  2833. . ,(let ((type (org-element-property :type el)))
  2834. (format
  2835. "<text:list text:style-name=\"%s\" %s>"
  2836. (assoc-default
  2837. type '((ordered . "OrgNumberedList")
  2838. (unordered . "OrgBulletedList")
  2839. (descriptive . "OrgDescriptionList")))
  2840. "text:continue-numbering=\"true\""))))
  2841. (item
  2842. (cond
  2843. ((not parent-list)
  2844. (if (funcall --element-preceded-by-table-p table info)
  2845. '("</text:list-header>" . "<text:list-header>")
  2846. '("</text:list-item>" . "<text:list-header>")))
  2847. ((funcall --element-preceded-by-table-p
  2848. parent-list info)
  2849. '("</text:list-header>" . "<text:list-header>"))
  2850. (t '("</text:list-item>" . "<text:list-item>"))))))))))
  2851. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  2852. table info)))
  2853. ;; OpenDocument schema does not permit table to occur within a
  2854. ;; list item. So, to typeset an indented table, we make use of
  2855. ;; list continuations.
  2856. (concat "\n"
  2857. ;; Discontinue the list.
  2858. (mapconcat 'car close-open-tags "\n")
  2859. ;; Put the table in an indented section.
  2860. (let* ((table (org-e-odt--table table contents info))
  2861. (level (/ (length (mapcar 'car close-open-tags)) 2))
  2862. (style (format "OrgIndentedSection-Level-%d" level)))
  2863. (when table (org-e-odt-format-section table style)))
  2864. ;; Continue the list.
  2865. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  2866. ;;;; Target
  2867. (defun org-e-odt-target (target contents info)
  2868. "Transcode a TARGET object from Org to ODT.
  2869. CONTENTS is nil. INFO is a plist holding contextual
  2870. information."
  2871. (org-e-odt-format-target
  2872. "" (org-export-solidify-link-text (org-element-property :value target))))
  2873. ;;;; Timestamp
  2874. (defun org-e-odt-timestamp (timestamp contents info)
  2875. "Transcode a TIMESTAMP object from Org to ODT.
  2876. CONTENTS is nil. INFO is a plist used as a communication
  2877. channel."
  2878. (let ((timestamp-1 (org-element-property :value timestamp))
  2879. (timestamp-2 (org-element-property :range-end timestamp)))
  2880. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2881. "OrgTimestampWrapper"
  2882. (concat
  2883. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2884. "OrgTimestamp" (org-translate-time timestamp-1))
  2885. (and timestamp-2
  2886. "&#x2013;"
  2887. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2888. "OrgTimestamp" (org-translate-time timestamp-2)))))))
  2889. ;;;; Underline
  2890. (defun org-e-odt-underline (underline contents info)
  2891. "Transcode UNDERLINE from Org to ODT.
  2892. CONTENTS is the text with underline markup. INFO is a plist
  2893. holding contextual information."
  2894. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2895. "Underline" contents))
  2896. ;;;; Verbatim
  2897. (defun org-e-odt-verbatim (verbatim contents info)
  2898. "Transcode a VERBATIM object from Org to ODT.
  2899. CONTENTS is nil. INFO is a plist used as a communication
  2900. channel."
  2901. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2902. "OrgCode" (org-element-property :value verbatim)))
  2903. ;;;; Verse Block
  2904. (defun org-e-odt-verse-block (verse-block contents info)
  2905. "Transcode a VERSE-BLOCK element from Org to ODT.
  2906. CONTENTS is verse block contents. INFO is a plist holding
  2907. contextual information."
  2908. ;; Add line breaks to each line of verse.
  2909. (setq contents (replace-regexp-in-string
  2910. "\\(<text:line-break/>\\)?[ \t]*\n"
  2911. "<text:line-break/>" contents))
  2912. ;; Replace tabs and spaces.
  2913. (setq contents (org-e-odt-fill-tabs-and-spaces contents))
  2914. ;; Surround it in a verse environment.
  2915. (org-e-odt--wrap-label
  2916. verse-block
  2917. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2918. "OrgVerse" contents)))
  2919. ;;; Interactive functions
  2920. (defun org-e-odt-create-manifest-file-entry (&rest args)
  2921. (push args org-e-odt-manifest-file-entries))
  2922. (defun org-e-odt-write-manifest-file ()
  2923. (make-directory (concat org-e-odt-zip-dir "META-INF"))
  2924. (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
  2925. (with-current-buffer
  2926. (let ((nxml-auto-insert-xml-declaration-flag nil))
  2927. (find-file-noselect manifest-file t))
  2928. (insert
  2929. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  2930. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  2931. (mapc
  2932. (lambda (file-entry)
  2933. (let* ((version (nth 2 file-entry))
  2934. (extra (if (not version) ""
  2935. (format " manifest:version=\"%s\"" version))))
  2936. (insert
  2937. (format org-e-odt-manifest-file-entry-tag
  2938. (nth 0 file-entry) (nth 1 file-entry) extra))))
  2939. org-e-odt-manifest-file-entries)
  2940. (insert "\n</manifest:manifest>"))))
  2941. (defmacro org-e-odt--export-wrap (out-file &rest body)
  2942. `(let* ((--out-file ,out-file)
  2943. (out-file-type (file-name-extension --out-file))
  2944. (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  2945. "meta.xml" "styles.xml"))
  2946. ;; Initialize workarea. All files that end up in the
  2947. ;; exported get created here.
  2948. (org-e-odt-zip-dir (file-name-as-directory
  2949. (make-temp-file (format "%s-" out-file-type) t)))
  2950. (org-e-odt-manifest-file-entries nil)
  2951. (--cleanup-xml-buffers
  2952. (function
  2953. (lambda nil
  2954. ;; Kill all XML buffers.
  2955. (mapc (lambda (file)
  2956. (let ((buf (get-file-buffer
  2957. (concat org-e-odt-zip-dir file))))
  2958. (when buf
  2959. (set-buffer-modified-p nil)
  2960. (kill-buffer buf))))
  2961. org-e-odt-xml-files)
  2962. ;; Delete temporary directory and also other embedded
  2963. ;; files that get copied there.
  2964. (delete-directory org-e-odt-zip-dir t)))))
  2965. (org-condition-case-unless-debug
  2966. err
  2967. (progn
  2968. (unless (executable-find "zip")
  2969. ;; Not at all OSes ship with zip by default
  2970. (error "Executable \"zip\" needed for creating OpenDocument files"))
  2971. ;; Do export. This creates a bunch of xml files ready to be
  2972. ;; saved and zipped.
  2973. (progn ,@body)
  2974. ;; Create a manifest entry for content.xml.
  2975. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  2976. ;; Write mimetype file
  2977. (let* ((mimetypes
  2978. '(("odt" . "application/vnd.oasis.opendocument.text")
  2979. ("odf" . "application/vnd.oasis.opendocument.formula")))
  2980. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  2981. (unless mimetype
  2982. (error "Unknown OpenDocument backend %S" out-file-type))
  2983. (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
  2984. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  2985. ;; Write out the manifest entries before zipping
  2986. (org-e-odt-write-manifest-file)
  2987. ;; Save all XML files.
  2988. (mapc (lambda (file)
  2989. (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
  2990. (when buf
  2991. (with-current-buffer buf
  2992. ;; Prettify output if needed.
  2993. (when org-e-odt-prettify-xml
  2994. (indent-region (point-min) (point-max)))
  2995. (save-buffer 0)))))
  2996. org-e-odt-xml-files)
  2997. ;; Run zip.
  2998. (let* ((target --out-file)
  2999. (target-name (file-name-nondirectory target))
  3000. (target-dir (file-name-directory target))
  3001. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3002. ("zip" "-rmTq" ,target-name "."))))
  3003. ;; If a file with same name as the desired output file
  3004. ;; exists, remove it.
  3005. (when (file-exists-p target)
  3006. (delete-file target))
  3007. ;; Zip up the xml files.
  3008. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3009. (message "Creating ODT file...")
  3010. ;; Switch temporarily to content.xml. This way Zip
  3011. ;; process will inherit `org-e-odt-zip-dir' as the current
  3012. ;; directory.
  3013. (with-current-buffer
  3014. (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
  3015. (mapc
  3016. (lambda (cmd)
  3017. (message "Running %s" (mapconcat 'identity cmd " "))
  3018. (setq err-string
  3019. (with-output-to-string
  3020. (setq exitcode
  3021. (apply 'call-process (car cmd)
  3022. nil standard-output nil (cdr cmd)))))
  3023. (or (zerop exitcode)
  3024. (error (concat "Unable to create OpenDocument file."
  3025. (format " Zip failed with error (%s)"
  3026. err-string)))))
  3027. cmds)
  3028. ;; Zip file is now in the rightful place.
  3029. (rename-file target-name target)))
  3030. (message "Created %s" target)
  3031. ;; Cleanup work directory and work files.
  3032. (funcall --cleanup-xml-buffers)
  3033. ;; Open the OpenDocument file in archive-mode for
  3034. ;; examination.
  3035. (find-file-noselect target t)
  3036. ;; Return exported file.
  3037. (cond
  3038. ;; Case 1: Conversion desired on exported file. Run the
  3039. ;; converter on the OpenDocument file. Return the
  3040. ;; converted file.
  3041. (org-e-odt-preferred-output-format
  3042. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3043. target))
  3044. ;; Case 2: No further conversion. Return exported
  3045. ;; OpenDocument file.
  3046. (t target))))
  3047. ((quit error)
  3048. ;; Cleanup work directory and work files.
  3049. (funcall --cleanup-xml-buffers)
  3050. (message "OpenDocument export failed: %s"
  3051. (error-message-string err))))))
  3052. ;;;###autoload
  3053. (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
  3054. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3055. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3056. MathML. When invoked as an interactive command, use
  3057. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3058. region. If no LaTeX fragments are found, prompt for it. Push
  3059. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  3060. non-nil."
  3061. (interactive
  3062. `(,(let (frag)
  3063. (setq frag (and (setq frag (and (region-active-p)
  3064. (buffer-substring (region-beginning)
  3065. (region-end))))
  3066. (loop for e in org-latex-regexps
  3067. thereis (when (string-match (nth 1 e) frag)
  3068. (match-string (nth 2 e) frag)))))
  3069. (read-string "LaTeX Fragment: " frag nil frag))
  3070. ,(let ((odf-filename (expand-file-name
  3071. (concat
  3072. (file-name-sans-extension
  3073. (or (file-name-nondirectory buffer-file-name)))
  3074. "." "odf")
  3075. (file-name-directory buffer-file-name))))
  3076. (read-file-name "ODF filename: " nil odf-filename nil
  3077. (file-name-nondirectory odf-filename)))))
  3078. (let ((filename (or odf-file
  3079. (expand-file-name
  3080. (concat
  3081. (file-name-sans-extension
  3082. (or (file-name-nondirectory buffer-file-name)))
  3083. "." "odf")
  3084. (file-name-directory buffer-file-name)))))
  3085. (org-e-odt--export-wrap
  3086. filename
  3087. (let* ((buffer (progn
  3088. (require 'nxml-mode)
  3089. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3090. (find-file-noselect (concat org-e-odt-zip-dir
  3091. "content.xml") t))))
  3092. (coding-system-for-write 'utf-8)
  3093. (save-buffer-coding-system 'utf-8))
  3094. (set-buffer buffer)
  3095. (set-buffer-file-coding-system coding-system-for-write)
  3096. (let ((mathml (org-create-math-formula latex-frag)))
  3097. (unless mathml (error "No Math formula created"))
  3098. (insert mathml)
  3099. ;; Add MathML to kill ring, if needed.
  3100. (when org-export-copy-to-kill-ring
  3101. (org-kill-new (buffer-string))))))))
  3102. ;;;###autoload
  3103. (defun org-e-odt-export-as-odf-and-open ()
  3104. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3105. Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3106. formula file."
  3107. (interactive)
  3108. (org-open-file (call-interactively 'org-e-odt-export-as-odf)))
  3109. ;;;###autoload
  3110. (defun org-e-odt-export-to-odt
  3111. (&optional subtreep visible-only body-only ext-plist pub-dir)
  3112. "Export current buffer to a HTML file.
  3113. If narrowing is active in the current buffer, only export its
  3114. narrowed part.
  3115. If a region is active, export that region.
  3116. When optional argument SUBTREEP is non-nil, export the sub-tree
  3117. at point, extracting information from the headline properties
  3118. first.
  3119. When optional argument VISIBLE-ONLY is non-nil, don't export
  3120. contents of hidden elements.
  3121. When optional argument BODY-ONLY is non-nil, only write code
  3122. between \"\\begin{document}\" and \"\\end{document}\".
  3123. EXT-PLIST, when provided, is a property list with external
  3124. parameters overriding Org default settings, but still inferior to
  3125. file-local settings.
  3126. When optional argument PUB-DIR is set, use it as the publishing
  3127. directory.
  3128. Return output file's name."
  3129. (interactive)
  3130. (org-e-odt--export-wrap
  3131. (org-export-output-file-name ".odt" subtreep pub-dir)
  3132. (let* ((org-e-odt-embedded-images-count 0)
  3133. (org-e-odt-embedded-formulas-count 0)
  3134. (org-e-odt-automatic-styles nil)
  3135. (org-e-odt-object-counters nil)
  3136. ;; Let `htmlfontify' know that we are interested in collecting
  3137. ;; styles.
  3138. (hfy-user-sheet-assoc nil))
  3139. ;; Initialize content.xml and kick-off the export process.
  3140. (let ((out-buf (progn
  3141. (require 'nxml-mode)
  3142. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3143. (find-file-noselect
  3144. (concat org-e-odt-zip-dir "content.xml") t)))))
  3145. (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only)))))
  3146. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3147. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3148. (catch 'done
  3149. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3150. (dolist (e reachable-formats)
  3151. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3152. (when out-fmt-spec
  3153. (throw 'done (cons (car e) out-fmt-spec))))))))
  3154. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3155. "Workhorse routine for `org-e-odt-convert'."
  3156. (require 'browse-url)
  3157. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3158. (dummy (or (file-readable-p in-file)
  3159. (error "Cannot read %s" in-file)))
  3160. (in-fmt (file-name-extension in-file))
  3161. (out-fmt (or out-fmt (error "Output format unspecified")))
  3162. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3163. (error "Cannot convert from %s format to %s format?"
  3164. in-fmt out-fmt)))
  3165. (convert-process (car how))
  3166. (out-file (concat (file-name-sans-extension in-file) "."
  3167. (nth 1 (or (cdr how) out-fmt))))
  3168. (extra-options (or (nth 2 (cdr how)) ""))
  3169. (out-dir (file-name-directory in-file))
  3170. (cmd (format-spec convert-process
  3171. `((?i . ,(shell-quote-argument in-file))
  3172. (?I . ,(browse-url-file-url in-file))
  3173. (?f . ,out-fmt)
  3174. (?o . ,out-file)
  3175. (?O . ,(browse-url-file-url out-file))
  3176. (?d . , (shell-quote-argument out-dir))
  3177. (?D . ,(browse-url-file-url out-dir))
  3178. (?x . ,extra-options)))))
  3179. (when (file-exists-p out-file)
  3180. (delete-file out-file))
  3181. (message "Executing %s" cmd)
  3182. (let ((cmd-output (shell-command-to-string cmd)))
  3183. (message "%s" cmd-output))
  3184. (cond
  3185. ((file-exists-p out-file)
  3186. (message "Exported to %s" out-file)
  3187. (when prefix-arg
  3188. (message "Opening %s..." out-file)
  3189. (org-open-file out-file))
  3190. out-file)
  3191. (t
  3192. (message "Export to %s failed" out-file)
  3193. nil))))
  3194. (defun org-e-odt-do-reachable-formats (in-fmt)
  3195. "Return verbose info about formats to which IN-FMT can be converted.
  3196. Return a list where each element is of the
  3197. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3198. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3199. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3200. (let* ((converter
  3201. (and org-e-odt-convert-process
  3202. (cadr (assoc-string org-e-odt-convert-process
  3203. org-e-odt-convert-processes t))))
  3204. (capabilities
  3205. (and org-e-odt-convert-process
  3206. (cadr (assoc-string org-e-odt-convert-process
  3207. org-e-odt-convert-processes t))
  3208. org-e-odt-convert-capabilities))
  3209. reachable-formats)
  3210. (when converter
  3211. (dolist (c capabilities)
  3212. (when (member in-fmt (nth 1 c))
  3213. (push (cons converter (nth 2 c)) reachable-formats))))
  3214. reachable-formats))
  3215. (defun org-e-odt-reachable-formats (in-fmt)
  3216. "Return list of formats to which IN-FMT can be converted.
  3217. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3218. (let (l)
  3219. (mapc (lambda (e) (add-to-list 'l e))
  3220. (apply 'append (mapcar
  3221. (lambda (e) (mapcar 'car (cdr e)))
  3222. (org-e-odt-do-reachable-formats in-fmt))))
  3223. l))
  3224. (defun org-e-odt-convert-read-params ()
  3225. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3226. This is a helper routine for interactive use."
  3227. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3228. (in-file (read-file-name "File to be converted: "
  3229. nil buffer-file-name t))
  3230. (in-fmt (file-name-extension in-file))
  3231. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3232. (out-fmt
  3233. (or (and out-fmt-choices
  3234. (funcall input "Output format: "
  3235. out-fmt-choices nil nil nil))
  3236. (error
  3237. "No known converter or no known output formats for %s files"
  3238. in-fmt))))
  3239. (list in-file out-fmt)))
  3240. ;;;###autoload
  3241. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3242. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3243. IN-FILE is the file to be converted. If unspecified, it defaults
  3244. to variable `buffer-file-name'. OUT-FMT is the desired output
  3245. format. Use `org-e-odt-convert-process' as the converter.
  3246. If PREFIX-ARG is non-nil then the newly converted file is opened
  3247. using `org-open-file'."
  3248. (interactive
  3249. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3250. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3251. ;;; Library Initializations
  3252. (mapc
  3253. (lambda (desc)
  3254. ;; Let Org open all OpenDocument files using system-registered app
  3255. (add-to-list 'org-file-apps
  3256. (cons (concat "\\." (car desc) "\\'") 'system))
  3257. ;; Let Emacs open all OpenDocument files in archive mode
  3258. (add-to-list 'auto-mode-alist
  3259. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3260. org-e-odt-file-extensions)
  3261. (provide 'org-e-odt)
  3262. ;;; org-e-odt.el ends here