ox-odt.el 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340
  1. ;;; ox-odt.el --- OpenDocument Text Exporter for Org Mode -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://orgmode.org
  6. ;; This file is 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 <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'cl-lib)
  20. (require 'format-spec)
  21. (require 'org-compat)
  22. (require 'org-macs)
  23. (require 'ox)
  24. (require 'table nil 'noerror)
  25. ;;; Define Back-End
  26. (org-export-define-backend 'odt
  27. '((bold . org-odt-bold)
  28. (center-block . org-odt-center-block)
  29. (clock . org-odt-clock)
  30. (code . org-odt-code)
  31. (drawer . org-odt-drawer)
  32. (dynamic-block . org-odt-dynamic-block)
  33. (entity . org-odt-entity)
  34. (example-block . org-odt-example-block)
  35. (export-block . org-odt-export-block)
  36. (export-snippet . org-odt-export-snippet)
  37. (fixed-width . org-odt-fixed-width)
  38. (footnote-definition . org-odt-footnote-definition)
  39. (footnote-reference . org-odt-footnote-reference)
  40. (headline . org-odt-headline)
  41. (horizontal-rule . org-odt-horizontal-rule)
  42. (inline-src-block . org-odt-inline-src-block)
  43. (inlinetask . org-odt-inlinetask)
  44. (italic . org-odt-italic)
  45. (item . org-odt-item)
  46. (keyword . org-odt-keyword)
  47. (latex-environment . org-odt-latex-environment)
  48. (latex-fragment . org-odt-latex-fragment)
  49. (line-break . org-odt-line-break)
  50. (link . org-odt-link)
  51. (node-property . org-odt-node-property)
  52. (paragraph . org-odt-paragraph)
  53. (plain-list . org-odt-plain-list)
  54. (plain-text . org-odt-plain-text)
  55. (planning . org-odt-planning)
  56. (property-drawer . org-odt-property-drawer)
  57. (quote-block . org-odt-quote-block)
  58. (radio-target . org-odt-radio-target)
  59. (section . org-odt-section)
  60. (special-block . org-odt-special-block)
  61. (src-block . org-odt-src-block)
  62. (statistics-cookie . org-odt-statistics-cookie)
  63. (strike-through . org-odt-strike-through)
  64. (subscript . org-odt-subscript)
  65. (superscript . org-odt-superscript)
  66. (table . org-odt-table)
  67. (table-cell . org-odt-table-cell)
  68. (table-row . org-odt-table-row)
  69. (target . org-odt-target)
  70. (template . org-odt-template)
  71. (timestamp . org-odt-timestamp)
  72. (underline . org-odt-underline)
  73. (verbatim . org-odt-verbatim)
  74. (verse-block . org-odt-verse-block))
  75. :filters-alist '((:filter-parse-tree
  76. . (org-odt--translate-latex-fragments
  77. org-odt--translate-description-lists
  78. org-odt--translate-list-tables
  79. org-odt--translate-image-links)))
  80. :menu-entry
  81. '(?o "Export to ODT"
  82. ((?o "As ODT file" org-odt-export-to-odt)
  83. (?O "As ODT file and open"
  84. (lambda (a s v b)
  85. (if a (org-odt-export-to-odt t s v)
  86. (org-open-file (org-odt-export-to-odt nil s v) 'system))))))
  87. :options-alist
  88. '((:odt-styles-file "ODT_STYLES_FILE" nil org-odt-styles-file t)
  89. (:description "DESCRIPTION" nil nil newline)
  90. (:keywords "KEYWORDS" nil nil space)
  91. (:subtitle "SUBTITLE" nil nil parse)
  92. ;; Other variables.
  93. (:odt-content-template-file nil nil org-odt-content-template-file)
  94. (:odt-display-outline-level nil nil org-odt-display-outline-level)
  95. (:odt-fontify-srcblocks nil nil org-odt-fontify-srcblocks)
  96. (:odt-format-drawer-function nil nil org-odt-format-drawer-function)
  97. (:odt-format-headline-function nil nil org-odt-format-headline-function)
  98. (:odt-format-inlinetask-function nil nil org-odt-format-inlinetask-function)
  99. (:odt-inline-formula-rules nil nil org-odt-inline-formula-rules)
  100. (:odt-inline-image-rules nil nil org-odt-inline-image-rules)
  101. (:odt-pixels-per-inch nil nil org-odt-pixels-per-inch)
  102. (:odt-table-styles nil nil org-odt-table-styles)
  103. (:odt-use-date-fields nil nil org-odt-use-date-fields)
  104. ;; Redefine regular option.
  105. (:with-latex nil "tex" org-odt-with-latex)
  106. ;; Retrieve LaTeX header for fragments.
  107. (:latex-header "LATEX_HEADER" nil nil newline)))
  108. ;;; Dependencies
  109. ;;; Hooks
  110. ;;; Function and Dynamically Scoped Variables Declarations
  111. (declare-function hfy-face-to-style "htmlfontify" (fn))
  112. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  113. (declare-function archive-zip-extract "arc-mode" (archive name))
  114. (declare-function org-create-math-formula "org" (latex-frag &optional mathml-file))
  115. (declare-function browse-url-file-url "browse-url" (file))
  116. (defvar nxml-auto-insert-xml-declaration-flag) ; nxml-mode.el
  117. (defvar archive-zip-extract) ; arc-mode.el
  118. (defvar hfy-end-span-handler) ; htmlfontify.el
  119. (defvar hfy-begin-span-handler) ; htmlfontify.el
  120. (defvar hfy-face-to-css) ; htmlfontify.el
  121. (defvar hfy-html-quote-map) ; htmlfontify.el
  122. (defvar hfy-html-quote-regex) ; htmlfontify.el
  123. ;;; Internal Variables
  124. (defconst org-odt-lib-dir
  125. (file-name-directory (or load-file-name (buffer-file-name)))
  126. "Location of ODT exporter.
  127. Use this to infer values of `org-odt-styles-dir' and
  128. `org-odt-schema-dir'.")
  129. (defvar org-odt-data-dir (expand-file-name "../../etc/" org-odt-lib-dir)
  130. "Data directory for ODT exporter.
  131. Use this to infer values of `org-odt-styles-dir' and
  132. `org-odt-schema-dir'.")
  133. (defconst org-odt-special-string-regexps
  134. '(("\\\\-" . "&#x00ad;\\1") ; shy
  135. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  136. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  137. ("\\.\\.\\." . "&#x2026;")) ; hellip
  138. "Regular expressions for special string conversion.")
  139. (defconst org-odt-schema-dir-list
  140. (list (expand-file-name "./schema/" org-odt-data-dir))
  141. "List of directories to search for OpenDocument schema files.
  142. Use this list to set the default value of `org-odt-schema-dir'.
  143. The entries in this list are populated heuristically based on the
  144. values of `org-odt-lib-dir' and `org-odt-data-dir'.")
  145. (defconst org-odt-styles-dir-list
  146. (list
  147. (and org-odt-data-dir
  148. (expand-file-name "./styles/" org-odt-data-dir)) ; bail out
  149. (expand-file-name "./styles/" org-odt-data-dir)
  150. (expand-file-name "../etc/styles/" org-odt-lib-dir) ; git
  151. (expand-file-name "./etc/styles/" org-odt-lib-dir) ; elpa
  152. (expand-file-name "./org/" data-directory) ; system
  153. )
  154. "List of directories to search for OpenDocument styles files.
  155. See `org-odt-styles-dir'. The entries in this list are populated
  156. heuristically based on the values of `org-odt-lib-dir' and
  157. `org-odt-data-dir'.")
  158. (defconst org-odt-styles-dir
  159. (let ((styles-dir
  160. (cl-find-if
  161. (lambda (dir)
  162. (and dir
  163. (file-readable-p
  164. (expand-file-name "OrgOdtContentTemplate.xml" dir))
  165. (file-readable-p (expand-file-name "OrgOdtStyles.xml" dir))))
  166. org-odt-styles-dir-list)))
  167. (unless styles-dir
  168. (error "Error (ox-odt): Cannot find factory styles files, aborting"))
  169. styles-dir)
  170. "Directory that holds auxiliary XML files used by the ODT exporter.
  171. This directory contains the following XML files -
  172. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
  173. XML files are used as the default values of
  174. `org-odt-styles-file' and `org-odt-content-template-file'.
  175. The default value of this variable varies depending on the
  176. version of Org in use and is initialized from
  177. `org-odt-styles-dir-list'. Note that the user could be using Org
  178. from one of: Org own private git repository, GNU ELPA tar or
  179. standard Emacs.")
  180. (defconst org-odt-bookmark-prefix "OrgXref.")
  181. (defconst org-odt-manifest-file-entry-tag
  182. "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  183. (defconst org-odt-file-extensions
  184. '(("odt" . "OpenDocument Text")
  185. ("ott" . "OpenDocument Text Template")
  186. ("odm" . "OpenDocument Master Document")
  187. ("ods" . "OpenDocument Spreadsheet")
  188. ("ots" . "OpenDocument Spreadsheet Template")
  189. ("odg" . "OpenDocument Drawing (Graphics)")
  190. ("otg" . "OpenDocument Drawing Template")
  191. ("odp" . "OpenDocument Presentation")
  192. ("otp" . "OpenDocument Presentation Template")
  193. ("odi" . "OpenDocument Image")
  194. ("odf" . "OpenDocument Formula")
  195. ("odc" . "OpenDocument Chart")))
  196. (defconst org-odt-table-style-format
  197. "
  198. <style:style style:name=\"%s\" style:family=\"table\">
  199. <style:table-properties style:rel-width=\"%s%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
  200. </style:style>
  201. "
  202. "Template for auto-generated Table styles.")
  203. (defvar org-odt-automatic-styles '()
  204. "Registry of automatic styles for various OBJECT-TYPEs.
  205. The variable has the following form:
  206. ((OBJECT-TYPE-A
  207. ((OBJECT-NAME-A.1 OBJECT-PROPS-A.1)
  208. (OBJECT-NAME-A.2 OBJECT-PROPS-A.2) ...))
  209. (OBJECT-TYPE-B
  210. ((OBJECT-NAME-B.1 OBJECT-PROPS-B.1)
  211. (OBJECT-NAME-B.2 OBJECT-PROPS-B.2) ...))
  212. ...).
  213. OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
  214. OBJECT-PROPS is (typically) a plist created by passing
  215. \"#+ATTR_ODT: \" option to `org-odt-parse-block-attributes'.
  216. Use `org-odt-add-automatic-style' to add update this variable.'")
  217. (defvar org-odt-object-counters nil
  218. "Running counters for various OBJECT-TYPEs.
  219. Use this to generate automatic names and style-names. See
  220. `org-odt-add-automatic-style'.")
  221. (defvar org-odt-src-block-paragraph-format
  222. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  223. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  224. <style:background-image/>
  225. </style:paragraph-properties>
  226. <style:text-properties fo:color=\"%s\"/>
  227. </style:style>"
  228. "Custom paragraph style for colorized source and example blocks.
  229. This style is much the same as that of \"OrgFixedWidthBlock\"
  230. except that the foreground and background colors are set
  231. according to the default face identified by the `htmlfontify'.")
  232. (defvar hfy-optimizations)
  233. (defvar org-odt-embedded-formulas-count 0)
  234. (defvar org-odt-embedded-images-count 0)
  235. (defvar org-odt-image-size-probe-method
  236. (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
  237. '(emacs fixed))
  238. "Ordered list of methods for determining image sizes.")
  239. (defvar org-odt-default-image-sizes-alist
  240. '(("as-char" . (5 . 0.4))
  241. ("paragraph" . (5 . 5)))
  242. "Hardcoded image dimensions one for each of the anchor
  243. methods.")
  244. ;; A4 page size is 21.0 by 29.7 cms
  245. ;; The default page settings has 2cm margin on each of the sides. So
  246. ;; the effective text area is 17.0 by 25.7 cm
  247. (defvar org-odt-max-image-size '(17.0 . 20.0)
  248. "Limiting dimensions for an embedded image.")
  249. (defconst org-odt-label-styles
  250. '(("math-formula" "%c" "text" "(%n)")
  251. ("math-label" "(%n)" "text" "(%n)")
  252. ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
  253. ("value" "%e %n: %c" "value" "%n"))
  254. "Specify how labels are applied and referenced.
  255. This is an alist where each element is of the form:
  256. (STYLE-NAME ATTACH-FMT REF-MODE REF-FMT)
  257. ATTACH-FMT controls how labels and captions are attached to an
  258. entity. It may contain following specifiers - %e and %c. %e is
  259. replaced with the CATEGORY-NAME. %n is replaced with
  260. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  261. with CAPTION.
  262. REF-MODE and REF-FMT controls how label references are generated.
  263. The following XML is generated for a label reference -
  264. \"<text:sequence-ref text:reference-format=\"REF-MODE\" ...>
  265. REF-FMT </text:sequence-ref>\". REF-FMT may contain following
  266. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  267. %n is replaced with SEQNO.
  268. See also `org-odt-format-label'.")
  269. (defvar org-odt-category-map-alist
  270. '(("__Table__" "Table" "value" "Table" org-odt--enumerable-p)
  271. ("__Figure__" "Illustration" "value" "Figure" org-odt--enumerable-image-p)
  272. ("__MathFormula__" "Text" "math-formula" "Equation" org-odt--enumerable-formula-p)
  273. ("__DvipngImage__" "Equation" "value" "Equation" org-odt--enumerable-latex-image-p)
  274. ("__Listing__" "Listing" "value" "Listing" org-odt--enumerable-p))
  275. "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
  276. This is a list where each entry is of the form:
  277. (CATEGORY-HANDLE OD-VARIABLE LABEL-STYLE CATEGORY-NAME ENUMERATOR-PREDICATE)
  278. CATEGORY_HANDLE identifies the captionable entity in question.
  279. OD-VARIABLE is the OpenDocument sequence counter associated with
  280. the entity. These counters are declared within
  281. \"<text:sequence-decls>...</text:sequence-decls>\" block of
  282. `org-odt-content-template-file'.
  283. LABEL-STYLE is a key into `org-odt-label-styles' and specifies
  284. how a given entity should be captioned and referenced.
  285. CATEGORY-NAME is used for qualifying captions on export.
  286. ENUMERATOR-PREDICATE is used for assigning a sequence number to
  287. the entity. See `org-odt--enumerate'.")
  288. (defvar org-odt-manifest-file-entries nil)
  289. (defvar hfy-user-sheet-assoc)
  290. (defvar org-odt-zip-dir nil
  291. "Temporary work directory for OpenDocument exporter.")
  292. ;;; User Configuration Variables
  293. (defgroup org-export-odt nil
  294. "Options for exporting Org mode files to ODT."
  295. :tag "Org Export ODT"
  296. :group 'org-export)
  297. ;;;; Debugging
  298. (defcustom org-odt-prettify-xml nil
  299. "Specify whether or not the xml output should be prettified.
  300. When this option is turned on, `indent-region' is run on all
  301. component xml buffers before they are saved. Turn this off for
  302. regular use. Turn this on if you need to examine the xml
  303. visually."
  304. :group 'org-export-odt
  305. :version "24.1"
  306. :type 'boolean)
  307. ;;;; Document schema
  308. (require 'rng-loc)
  309. (defcustom org-odt-schema-dir
  310. (cl-find-if
  311. (lambda (dir)
  312. (and dir
  313. (file-expand-wildcards
  314. (expand-file-name "od-manifest-schema*.rnc" dir))
  315. (file-expand-wildcards (expand-file-name "od-schema*.rnc" dir))
  316. (file-readable-p (expand-file-name "schemas.xml" dir))))
  317. org-odt-schema-dir-list)
  318. "Directory that contains OpenDocument schema files.
  319. This directory contains:
  320. 1. rnc files for OpenDocument schema
  321. 2. a \"schemas.xml\" file that specifies locating rules needed
  322. for auto validation of OpenDocument XML files.
  323. Use the customize interface to set this variable. This ensures
  324. that `rng-schema-locating-files' is updated and auto-validation
  325. of OpenDocument XML takes place based on the value
  326. `rng-nxml-auto-validate-flag'.
  327. The default value of this variable varies depending on the
  328. version of org in use and is initialized from
  329. `org-odt-schema-dir-list'. The OASIS schema files are available
  330. only in the org's private git repository. It is *not* bundled
  331. with GNU ELPA tar or standard Emacs distribution."
  332. :type '(choice
  333. (const :tag "Not set" nil)
  334. (directory :tag "Schema directory"))
  335. :group 'org-export-odt
  336. :version "24.1"
  337. :set
  338. (lambda (var value)
  339. "Set `org-odt-schema-dir'.
  340. Also add it to `rng-schema-locating-files'."
  341. (let ((schema-dir value))
  342. (set var
  343. (if (and
  344. (file-expand-wildcards
  345. (expand-file-name "od-manifest-schema*.rnc" schema-dir))
  346. (file-expand-wildcards
  347. (expand-file-name "od-schema*.rnc" schema-dir))
  348. (file-readable-p
  349. (expand-file-name "schemas.xml" schema-dir)))
  350. schema-dir
  351. (when value
  352. (message "Error (ox-odt): %s has no OpenDocument schema files"
  353. value))
  354. nil)))
  355. (when org-odt-schema-dir
  356. (eval-after-load 'rng-loc
  357. '(add-to-list 'rng-schema-locating-files
  358. (expand-file-name "schemas.xml"
  359. org-odt-schema-dir))))))
  360. ;;;; Document styles
  361. (defcustom org-odt-content-template-file nil
  362. "Template file for \"content.xml\".
  363. The exporter embeds the exported content just before
  364. \"</office:text>\" element.
  365. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  366. under `org-odt-styles-dir' is used."
  367. :type '(choice (const nil)
  368. (file))
  369. :group 'org-export-odt
  370. :version "24.3")
  371. (defcustom org-odt-styles-file nil
  372. "Default styles file for use with ODT export.
  373. Valid values are one of:
  374. 1. nil
  375. 2. path to a styles.xml file
  376. 3. path to a *.odt or a *.ott file
  377. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  378. ...))
  379. In case of option 1, an in-built styles.xml is used. See
  380. `org-odt-styles-dir' for more information.
  381. In case of option 3, the specified file is unzipped and the
  382. styles.xml embedded therein is used.
  383. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  384. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  385. generated odt file. Use relative path for specifying the
  386. FILE-MEMBERS. styles.xml must be specified as one of the
  387. FILE-MEMBERS.
  388. Use options 1, 2 or 3 only if styles.xml alone suffices for
  389. achieving the desired formatting. Use option 4, if the styles.xml
  390. references additional files like header and footer images for
  391. achieving the desired formatting.
  392. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  393. a per-file basis. For example,
  394. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  395. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  396. :group 'org-export-odt
  397. :version "24.1"
  398. :type
  399. '(choice
  400. (const :tag "Factory settings" nil)
  401. (file :must-match t :tag "styles.xml")
  402. (file :must-match t :tag "ODT or OTT file")
  403. (list :tag "ODT or OTT file + Members"
  404. (file :must-match t :tag "ODF Text or Text Template file")
  405. (cons :tag "Members"
  406. (file :tag " Member" "styles.xml")
  407. (repeat (file :tag "Member"))))))
  408. (defcustom org-odt-display-outline-level 2
  409. "Outline levels considered for enumerating captioned entities."
  410. :group 'org-export-odt
  411. :version "24.4"
  412. :package-version '(Org . "8.0")
  413. :type 'integer)
  414. ;;;; Document conversion
  415. (defcustom org-odt-convert-processes
  416. '(("LibreOffice"
  417. "soffice --headless --convert-to %f%x --outdir %d %i")
  418. ("unoconv"
  419. "unoconv -f %f -o %d %i"))
  420. "Specify a list of document converters and their usage.
  421. The converters in this list are offered as choices while
  422. customizing `org-odt-convert-process'.
  423. This variable is a list where each element is of the
  424. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  425. of the converter. CONVERTER-CMD is the shell command for the
  426. converter and can contain format specifiers. These format
  427. specifiers are interpreted as below:
  428. %i input file name in full
  429. %I input file name as a URL
  430. %f format of the output file
  431. %o output file name in full
  432. %O output file name as a URL
  433. %d output dir in full
  434. %D output dir as a URL.
  435. %x extra options as set in `org-odt-convert-capabilities'."
  436. :group 'org-export-odt
  437. :version "24.1"
  438. :type
  439. '(choice
  440. (const :tag "None" nil)
  441. (alist :tag "Converters"
  442. :key-type (string :tag "Converter Name")
  443. :value-type (group (string :tag "Command line")))))
  444. (defcustom org-odt-convert-process "LibreOffice"
  445. "Use this converter to convert from \"odt\" format to other formats.
  446. During customization, the list of converter names are populated
  447. from `org-odt-convert-processes'."
  448. :group 'org-export-odt
  449. :version "24.1"
  450. :type '(choice :convert-widget
  451. (lambda (w)
  452. (apply 'widget-convert (widget-type w)
  453. (eval (car (widget-get w :args)))))
  454. `((const :tag "None" nil)
  455. ,@(mapcar (lambda (c)
  456. `(const :tag ,(car c) ,(car c)))
  457. org-odt-convert-processes))))
  458. (defcustom org-odt-convert-capabilities
  459. '(("Text"
  460. ("odt" "ott" "doc" "rtf" "docx")
  461. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  462. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  463. ("Web"
  464. ("html")
  465. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  466. ("Spreadsheet"
  467. ("ods" "ots" "xls" "csv" "xlsx")
  468. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  469. ("xls" "xls") ("xlsx" "xlsx")))
  470. ("Presentation"
  471. ("odp" "otp" "ppt" "pptx")
  472. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  473. ("pptx" "pptx") ("odg" "odg"))))
  474. "Specify input and output formats of `org-odt-convert-process'.
  475. More correctly, specify the set of input and output formats that
  476. the user is actually interested in.
  477. This variable is an alist where each element is of the
  478. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  479. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  480. alist where each element is of the form (OUTPUT-FMT
  481. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  482. The variable is interpreted as follows:
  483. `org-odt-convert-process' can take any document that is in
  484. INPUT-FMT-LIST and produce any document that is in the
  485. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  486. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  487. serves dual purposes:
  488. - It is used for populating completion candidates during
  489. `org-odt-convert' commands.
  490. - It is used as the value of \"%f\" specifier in
  491. `org-odt-convert-process'.
  492. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  493. `org-odt-convert-process'.
  494. DOCUMENT-CLASS is used to group a set of file formats in
  495. INPUT-FMT-LIST in to a single class.
  496. Note that this variable inherently captures how LibreOffice based
  497. converters work. LibreOffice maps documents of various formats
  498. to classes like Text, Web, Spreadsheet, Presentation etc and
  499. allow document of a given class (irrespective of its source
  500. format) to be converted to any of the export formats associated
  501. with that class.
  502. See default setting of this variable for a typical configuration."
  503. :group 'org-export-odt
  504. :version "24.1"
  505. :type
  506. '(choice
  507. (const :tag "None" nil)
  508. (alist :tag "Capabilities"
  509. :key-type (string :tag "Document Class")
  510. :value-type
  511. (group (repeat :tag "Input formats" (string :tag "Input format"))
  512. (alist :tag "Output formats"
  513. :key-type (string :tag "Output format")
  514. :value-type
  515. (group (string :tag "Output file extension")
  516. (choice
  517. (const :tag "None" nil)
  518. (string :tag "Extra options"))))))))
  519. (defcustom org-odt-preferred-output-format nil
  520. "Automatically post-process to this format after exporting to \"odt\".
  521. Command `org-odt-export-to-odt' exports first to \"odt\" format
  522. and then uses `org-odt-convert-process' to convert the
  523. resulting document to this format. During customization of this
  524. variable, the list of valid values are populated based on
  525. `org-odt-convert-capabilities'.
  526. You can set this option on per-file basis using file local
  527. values. See Info node `(emacs) File Variables'."
  528. :group 'org-export-odt
  529. :version "24.1"
  530. :type '(choice :convert-widget
  531. (lambda (w)
  532. (apply 'widget-convert (widget-type w)
  533. (eval (car (widget-get w :args)))))
  534. `((const :tag "None" nil)
  535. ,@(mapcar (lambda (c)
  536. `(const :tag ,c ,c))
  537. (org-odt-reachable-formats "odt")))))
  538. ;;;###autoload
  539. (put 'org-odt-preferred-output-format 'safe-local-variable 'stringp)
  540. ;;;; Drawers
  541. (defcustom org-odt-format-drawer-function (lambda (_name contents) contents)
  542. "Function called to format a drawer in ODT code.
  543. The function must accept two parameters:
  544. NAME the drawer name, like \"LOGBOOK\"
  545. CONTENTS the contents of the drawer.
  546. The function should return the string to be exported.
  547. The default value simply returns the value of CONTENTS."
  548. :group 'org-export-odt
  549. :version "26.1"
  550. :package-version '(Org . "8.3")
  551. :type 'function)
  552. ;;;; Headline
  553. (defcustom org-odt-format-headline-function
  554. 'org-odt-format-headline-default-function
  555. "Function to format headline text.
  556. This function will be called with 5 arguments:
  557. TODO the todo keyword (string or nil).
  558. TODO-TYPE the type of todo (symbol: `todo', `done', nil)
  559. PRIORITY the priority of the headline (integer or nil)
  560. TEXT the main headline text (string).
  561. TAGS the tags string, separated with colons (string or nil).
  562. The function result will be used as headline text."
  563. :group 'org-export-odt
  564. :version "26.1"
  565. :package-version '(Org . "8.3")
  566. :type 'function)
  567. ;;;; Inlinetasks
  568. (defcustom org-odt-format-inlinetask-function
  569. 'org-odt-format-inlinetask-default-function
  570. "Function called to format an inlinetask in ODT code.
  571. The function must accept six parameters:
  572. TODO the todo keyword, as a string
  573. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  574. PRIORITY the inlinetask priority, as a string
  575. NAME the inlinetask name, as a string.
  576. TAGS the inlinetask tags, as a string.
  577. CONTENTS the contents of the inlinetask, as a string.
  578. The function should return the string to be exported."
  579. :group 'org-export-odt
  580. :version "26.1"
  581. :package-version '(Org . "8.3")
  582. :type 'function)
  583. ;;;; LaTeX
  584. (defcustom org-odt-with-latex org-export-with-latex
  585. "Non-nil means process LaTeX math snippets.
  586. When set, the exporter will process LaTeX environments and
  587. fragments.
  588. This option can also be set with the +OPTIONS line,
  589. e.g. \"tex:mathjax\". Allowed values are:
  590. nil Ignore math snippets.
  591. `verbatim' Keep everything in verbatim
  592. `dvipng' Process the LaTeX fragments to images. This will also
  593. include processing of non-math environments.
  594. `imagemagick' Convert the LaTeX fragments to pdf files and use
  595. imagemagick to convert pdf files to png files.
  596. `mathjax' Do MathJax preprocessing and arrange for MathJax.js to
  597. be loaded.
  598. Any other symbol is a synonym for `mathjax'."
  599. :group 'org-export-odt
  600. :version "24.4"
  601. :package-version '(Org . "8.0")
  602. :type '(choice
  603. (const :tag "Do not process math in any way" nil)
  604. (const :tag "Leave math verbatim" verbatim)
  605. (const :tag "Use dvipng to make images" dvipng)
  606. (const :tag "Use imagemagick to make images" imagemagick)
  607. (other :tag "Use MathJax to display math" mathjax)))
  608. ;;;; Links
  609. (defcustom org-odt-inline-formula-rules
  610. '(("file" . "\\.\\(mathml\\|mml\\|odf\\)\\'"))
  611. "Rules characterizing formula files that can be inlined into ODT.
  612. A rule consists in an association whose key is the type of link
  613. to consider, and value is a regexp that will be matched against
  614. link's path."
  615. :group 'org-export-odt
  616. :version "24.4"
  617. :package-version '(Org . "8.0")
  618. :type '(alist :key-type (string :tag "Type")
  619. :value-type (regexp :tag "Path")))
  620. (defcustom org-odt-inline-image-rules
  621. `(("file" . ,(regexp-opt '(".jpeg" ".jpg" ".png" ".gif" ".svg"))))
  622. "Rules characterizing image files that can be inlined into ODT.
  623. A rule consists in an association whose key is the type of link
  624. to consider, and value is a regexp that will be matched against
  625. link's path."
  626. :group 'org-export-odt
  627. :version "26.1"
  628. :package-version '(Org . "8.3")
  629. :type '(alist :key-type (string :tag "Type")
  630. :value-type (regexp :tag "Path")))
  631. (defcustom org-odt-pixels-per-inch 96.0
  632. "Scaling factor for converting images pixels to inches.
  633. Use this for sizing of embedded images. See Info node `(org)
  634. Images in ODT export' for more information."
  635. :type 'float
  636. :group 'org-export-odt
  637. :version "24.4"
  638. :package-version '(Org . "8.1"))
  639. ;;;; Src Block
  640. (defcustom org-odt-create-custom-styles-for-srcblocks t
  641. "Whether custom styles for colorized source blocks be automatically created.
  642. When this option is turned on, the exporter creates custom styles
  643. for source blocks based on the advice of `htmlfontify'. Creation
  644. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  645. When this option is turned off exporter does not create such
  646. styles.
  647. Use the latter option if you do not want the custom styles to be
  648. based on your current display settings. It is necessary that the
  649. styles.xml already contains needed styles for colorizing to work.
  650. This variable is effective only if `org-odt-fontify-srcblocks' is
  651. turned on."
  652. :group 'org-export-odt
  653. :version "24.1"
  654. :type 'boolean)
  655. (defcustom org-odt-fontify-srcblocks t
  656. "Specify whether or not source blocks need to be fontified.
  657. Turn this option on if you want to colorize the source code
  658. blocks in the exported file. For colorization to work, you need
  659. to make available an enhanced version of `htmlfontify' library."
  660. :type 'boolean
  661. :group 'org-export-odt
  662. :version "24.1")
  663. ;;;; Table
  664. (defcustom org-odt-table-styles
  665. '(("OrgEquation" "OrgEquation"
  666. ((use-first-column-styles . t)
  667. (use-last-column-styles . t)))
  668. ("TableWithHeaderRowAndColumn" "Custom"
  669. ((use-first-row-styles . t)
  670. (use-first-column-styles . t)))
  671. ("TableWithFirstRowandLastRow" "Custom"
  672. ((use-first-row-styles . t)
  673. (use-last-row-styles . t)))
  674. ("GriddedTable" "Custom" nil))
  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 - up to 9 - automatic
  681. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  682. below) that is included in `org-odt-content-template-file'.
  683. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  684. \"TableCell\"
  685. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  686. \"TableParagraph\"
  687. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  688. \"FirstRow\" | \"LastRow\" |
  689. \"EvenRow\" | \"OddRow\" |
  690. \"EvenColumn\" | \"OddColumn\" | \"\"
  691. where \"+\" above denotes string concatenation.
  692. TABLE-CELL-OPTIONS is an alist where each element is of the
  693. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  694. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  695. `use-last-row-styles' |
  696. `use-first-column-styles' |
  697. `use-last-column-styles' |
  698. `use-banding-rows-styles' |
  699. `use-banding-columns-styles' |
  700. `use-first-row-styles'
  701. ON-OR-OFF := t | nil
  702. For example, with the following configuration
  703. \(setq org-odt-table-styles
  704. \\='((\"TableWithHeaderRowsAndColumns\" \"Custom\"
  705. ((use-first-row-styles . t)
  706. (use-first-column-styles . t)))
  707. (\"TableWithHeaderColumns\" \"Custom\"
  708. ((use-first-column-styles . t)))))
  709. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  710. style will use the following table-cell styles -
  711. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  712. \"CustomTableCell\" and the following paragraph styles
  713. \"CustomFirstRowTableParagraph\",
  714. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  715. as appropriate.
  716. 2. A table associated with \"TableWithHeaderColumns\" style will
  717. use the following table-cell styles -
  718. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  719. following paragraph styles
  720. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  721. as appropriate..
  722. Note that TABLE-TEMPLATE-NAME corresponds to the
  723. \"<table:table-template>\" elements contained within
  724. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  725. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  726. \"table:template-name\" and \"table:use-first-row-styles\" etc
  727. attributes of \"<table:table>\" element. Refer ODF-1.2
  728. specification for more information. Also consult the
  729. implementation filed under `org-odt-get-table-cell-styles'.
  730. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  731. formatting of numbered display equations. Do not delete this
  732. style from the list."
  733. :group 'org-export-odt
  734. :version "24.1"
  735. :type '(choice
  736. (const :tag "None" nil)
  737. (repeat :tag "Table Styles"
  738. (list :tag "Table Style Specification"
  739. (string :tag "Table Style Name")
  740. (string :tag "Table Template Name")
  741. (alist :options (use-first-row-styles
  742. use-last-row-styles
  743. use-first-column-styles
  744. use-last-column-styles
  745. use-banding-rows-styles
  746. use-banding-columns-styles)
  747. :key-type symbol
  748. :value-type (const :tag "True" t))))))
  749. ;;;; Timestamps
  750. (defcustom org-odt-use-date-fields nil
  751. "Non-nil, if timestamps should be exported as date fields.
  752. When nil, export timestamps as plain text.
  753. When non-nil, map `org-time-stamp-custom-formats' to a pair of
  754. OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
  755. respectively. A timestamp with no time component is formatted
  756. with style \"OrgDate1\" while one with explicit hour and minutes
  757. is formatted with style \"OrgDate2\".
  758. This feature is experimental. Most (but not all) of the common
  759. %-specifiers in `format-time-string' are supported.
  760. Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
  761. formatted as canonical Org timestamps. For finer control, avoid
  762. these %-specifiers.
  763. Textual specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
  764. etc., are displayed by the application in the default language
  765. and country specified in `org-odt-styles-file'. Note that the
  766. default styles file uses language \"en\" and country \"GB\". You
  767. can localize the week day and month strings in the exported
  768. document by setting the default language and country either using
  769. the application UI or through a custom styles file.
  770. See `org-odt--build-date-styles' for implementation details."
  771. :group 'org-export-odt
  772. :version "24.4"
  773. :package-version '(Org . "8.0")
  774. :type 'boolean)
  775. ;;; Internal functions
  776. ;;;; Date
  777. (defun org-odt--format-timestamp (timestamp &optional end iso-date-p)
  778. (let* ((format-timestamp
  779. (lambda (timestamp format &optional end utc)
  780. (if timestamp
  781. (org-timestamp-format timestamp format end utc)
  782. (format-time-string format nil utc))))
  783. (has-time-p (or (not timestamp)
  784. (org-timestamp-has-time-p timestamp)))
  785. (iso-date (let ((format (if has-time-p "%Y-%m-%dT%H:%M:%S"
  786. "%Y-%m-%d")))
  787. (funcall format-timestamp timestamp format end))))
  788. (if iso-date-p iso-date
  789. (let* ((style (if has-time-p "OrgDate2" "OrgDate1"))
  790. ;; LibreOffice does not care about end goes as content
  791. ;; within the "<text:date>...</text:date>" field. The
  792. ;; displayed date is automagically corrected to match the
  793. ;; format requested by "style:data-style-name" attribute. So
  794. ;; don't bother about formatting the date contents to be
  795. ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
  796. ;; simple Org-style date should suffice.
  797. (date (let* ((formats
  798. (if org-display-custom-times
  799. (cons (substring
  800. (car org-time-stamp-custom-formats) 1 -1)
  801. (substring
  802. (cdr org-time-stamp-custom-formats) 1 -1))
  803. '("%Y-%m-%d %a" . "%Y-%m-%d %a %H:%M")))
  804. (format (if has-time-p (cdr formats) (car formats))))
  805. (funcall format-timestamp timestamp format end)))
  806. (repeater (let ((repeater-type (org-element-property
  807. :repeater-type timestamp))
  808. (repeater-value (org-element-property
  809. :repeater-value timestamp))
  810. (repeater-unit (org-element-property
  811. :repeater-unit timestamp)))
  812. (concat
  813. (cl-case repeater-type
  814. (catchup "++") (restart ".+") (cumulate "+"))
  815. (when repeater-value
  816. (number-to-string repeater-value))
  817. (cl-case repeater-unit
  818. (hour "h") (day "d") (week "w") (month "m")
  819. (year "y"))))))
  820. (concat
  821. (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
  822. iso-date style date)
  823. (and (not (string= repeater "")) " ")
  824. repeater)))))
  825. ;;;; Frame
  826. (defun org-odt--frame (text width height style &optional extra
  827. anchor-type &rest title-and-desc)
  828. (let ((frame-attrs
  829. (concat
  830. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  831. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  832. extra
  833. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph"))
  834. (format " draw:name=\"%s\""
  835. (car (org-odt-add-automatic-style "Frame"))))))
  836. (format
  837. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  838. style frame-attrs
  839. (concat text
  840. (let ((title (car title-and-desc))
  841. (desc (cadr title-and-desc)))
  842. (concat (when title
  843. (format "<svg:title>%s</svg:title>"
  844. (org-odt--encode-plain-text title t)))
  845. (when desc
  846. (format "<svg:desc>%s</svg:desc>"
  847. (org-odt--encode-plain-text desc t)))))))))
  848. ;;;; Library wrappers
  849. (defun org-odt--zip-extract (archive members target)
  850. (when (atom members) (setq members (list members)))
  851. (require 'arc-mode)
  852. (dolist (member members)
  853. (let* ((--quote-file-name
  854. ;; This is shamelessly stolen from `archive-zip-extract'.
  855. (lambda (name)
  856. (if (or (not (memq system-type '(windows-nt ms-dos)))
  857. (and (boundp 'w32-quote-process-args)
  858. (null w32-quote-process-args)))
  859. (shell-quote-argument name)
  860. name)))
  861. (target (funcall --quote-file-name target))
  862. (archive (expand-file-name archive))
  863. (archive-zip-extract
  864. (list "unzip" "-qq" "-o" "-d" target))
  865. exit-code command-output)
  866. (setq command-output
  867. (with-temp-buffer
  868. (setq exit-code (archive-zip-extract archive member))
  869. (buffer-string)))
  870. (unless (zerop exit-code)
  871. (message command-output)
  872. (error "Extraction failed")))))
  873. ;;;; Target
  874. (defun org-odt--target (text id)
  875. (if (not id) text
  876. (concat
  877. (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
  878. (format "\n<text:bookmark text:name=\"%s\"/>" id) text
  879. (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
  880. ;;;; Textbox
  881. (defun org-odt--textbox (text width height style &optional
  882. extra anchor-type)
  883. (org-odt--frame
  884. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  885. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  886. (and (not width)
  887. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  888. text)
  889. width nil style extra anchor-type))
  890. ;;;; Table of Contents
  891. (defun org-odt--format-toc (title entries depth)
  892. "Return a table of contents.
  893. TITLE is the title of the table, as a string, or nil. ENTRIES is
  894. the contents of the table, as a string. DEPTH is an integer
  895. specifying the depth of the table."
  896. (concat
  897. "
  898. <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">\n"
  899. (format " <text:table-of-content-source text:outline-level=\"%d\">" depth)
  900. (and title
  901. (format "
  902. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  903. "
  904. title))
  905. (let ((levels (number-sequence 1 10)))
  906. (mapconcat
  907. (lambda (level)
  908. (format
  909. "
  910. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  911. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  912. <text:index-entry-chapter/>
  913. <text:index-entry-text/>
  914. <text:index-entry-link-end/>
  915. </text:table-of-content-entry-template>\n"
  916. level level)) levels ""))
  917. "
  918. </text:table-of-content-source>
  919. <text:index-body>"
  920. (and title
  921. (format "
  922. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  923. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  924. </text:index-title>\n"
  925. title))
  926. entries
  927. "
  928. </text:index-body>
  929. </text:table-of-content>"))
  930. (cl-defun org-odt-format-toc-headline
  931. (todo _todo-type priority text tags
  932. &key _level section-number headline-label &allow-other-keys)
  933. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  934. headline-label
  935. (concat
  936. ;; Section number.
  937. (and section-number (concat section-number ". "))
  938. ;; Todo.
  939. (when todo
  940. (let ((style (if (member todo org-done-keywords)
  941. "OrgDone" "OrgTodo")))
  942. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  943. style todo)))
  944. (when priority
  945. (let* ((style (format "OrgPriority-%s" priority))
  946. (priority (format "[#%c]" priority)))
  947. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  948. style priority)))
  949. ;; Title.
  950. text
  951. ;; Tags.
  952. (when tags
  953. (concat
  954. (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
  955. "OrgTags"
  956. (mapconcat
  957. (lambda (tag)
  958. (format
  959. "<text:span text:style-name=\"%s\">%s</text:span>"
  960. "OrgTag" tag)) tags " : ")))))))
  961. (defun org-odt-toc (depth info &optional scope)
  962. "Build a table of contents.
  963. DEPTH is an integer specifying the depth of the table. INFO is
  964. a plist containing current export properties. Optional argument
  965. SCOPE, when non-nil, defines the scope of the table. Return the
  966. table of contents as a string, or nil."
  967. (cl-assert (wholenump depth))
  968. ;; When a headline is marked as a radio target, as in the example below:
  969. ;;
  970. ;; ** <<<Some Heading>>>
  971. ;; Some text.
  972. ;;
  973. ;; suppress generation of radio targets. i.e., Radio targets are to
  974. ;; be marked as targets within /document body/ and *not* within
  975. ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
  976. ;; and one in the document body.
  977. ;;
  978. ;; Likewise, links, footnote references and regular targets are also
  979. ;; suppressed.
  980. (let* ((headlines (org-export-collect-headlines info depth scope))
  981. (backend (org-export-toc-entry-backend
  982. (org-export-backend-name (plist-get info :back-end)))))
  983. (when headlines
  984. (org-odt--format-toc
  985. (and (not scope) (org-export-translate "Table of Contents" :utf-8 info))
  986. (mapconcat
  987. (lambda (headline)
  988. (let* ((entry (org-odt-format-headline--wrap
  989. headline backend info 'org-odt-format-toc-headline))
  990. (level (org-export-get-relative-level headline info))
  991. (style (format "Contents_20_%d" level)))
  992. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  993. style entry)))
  994. headlines "\n")
  995. depth))))
  996. ;;;; Document styles
  997. (defun org-odt-add-automatic-style (object-type &optional object-props)
  998. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  999. OBJECT-PROPS is (typically) a plist created by passing
  1000. \"#+ATTR_ODT: \" option of the object in question to
  1001. `org-odt-parse-block-attributes'.
  1002. Use `org-odt-object-counters' to generate an automatic
  1003. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  1004. new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
  1005. . STYLE-NAME)."
  1006. (cl-assert (stringp object-type))
  1007. (let* ((object (intern object-type))
  1008. (seqvar object)
  1009. (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
  1010. (object-name (format "%s%d" object-type seqno)) style-name)
  1011. (setq org-odt-object-counters
  1012. (plist-put org-odt-object-counters seqvar seqno))
  1013. (when object-props
  1014. (setq style-name (format "Org%s" object-name))
  1015. (setq org-odt-automatic-styles
  1016. (plist-put org-odt-automatic-styles object
  1017. (append (list (list style-name object-props))
  1018. (plist-get org-odt-automatic-styles object)))))
  1019. (cons object-name style-name)))
  1020. ;;;; Checkbox
  1021. (defun org-odt--checkbox (item)
  1022. "Return check-box string associated to ITEM."
  1023. (let ((checkbox (org-element-property :checkbox item)))
  1024. (if (not checkbox) ""
  1025. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1026. "OrgCode" (cl-case checkbox
  1027. (on "[&#x2713;] ") ; CHECK MARK
  1028. (off "[ ] ")
  1029. (trans "[-] "))))))
  1030. ;;; Template
  1031. (defun org-odt--build-date-styles (fmt style)
  1032. ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
  1033. ;; to modify the date fields. A date could be modified by
  1034. ;; offsetting in days. That's about it. Also, date and time may
  1035. ;; have to be emitted as two fields - a date field and a time field
  1036. ;; - separately.
  1037. ;; One can add Form Controls to date and time fields so that they
  1038. ;; can be easily modified. But then, the exported document will
  1039. ;; become tightly coupled with LibreOffice and may not function
  1040. ;; properly with other OpenDocument applications.
  1041. ;; I have a strange feeling that Date styles are a bit flaky at the
  1042. ;; moment.
  1043. ;; The feature is experimental.
  1044. (when (and fmt style)
  1045. (let* ((fmt-alist
  1046. '(("%A" . "<number:day-of-week number:style=\"long\"/>")
  1047. ("%B" . "<number:month number:textual=\"true\" number:style=\"long\"/>")
  1048. ("%H" . "<number:hours number:style=\"long\"/>")
  1049. ("%M" . "<number:minutes number:style=\"long\"/>")
  1050. ("%S" . "<number:seconds number:style=\"long\"/>")
  1051. ("%V" . "<number:week-of-year/>")
  1052. ("%Y" . "<number:year number:style=\"long\"/>")
  1053. ("%a" . "<number:day-of-week number:style=\"short\"/>")
  1054. ("%b" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1055. ("%d" . "<number:day number:style=\"long\"/>")
  1056. ("%e" . "<number:day number:style=\"short\"/>")
  1057. ("%h" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1058. ("%k" . "<number:hours number:style=\"short\"/>")
  1059. ("%m" . "<number:month number:style=\"long\"/>")
  1060. ("%p" . "<number:am-pm/>")
  1061. ("%y" . "<number:year number:style=\"short\"/>")))
  1062. (case-fold-search nil)
  1063. (re (mapconcat 'identity (mapcar 'car fmt-alist) "\\|"))
  1064. match rpl (start 0) (filler-beg 0) filler-end filler output)
  1065. (dolist (pair
  1066. '(("\\(?:%[[:digit:]]*N\\)" . "") ; strip ns, us and ns
  1067. ("%C" . "Y") ; replace century with year
  1068. ("%D" . "%m/%d/%y")
  1069. ("%G" . "Y") ; year corresponding to iso week
  1070. ("%I" . "%H") ; hour on a 12-hour clock
  1071. ("%R" . "%H:%M")
  1072. ("%T" . "%H:%M:%S")
  1073. ("%U\\|%W" . "%V") ; week no. starting on Sun./Mon.
  1074. ("%Z" . "") ; time zone name
  1075. ("%c" . "%Y-%M-%d %a %H:%M" ) ; locale's date and time format
  1076. ("%g" . "%y")
  1077. ("%X" . "%x" ) ; locale's pref. time format
  1078. ("%j" . "") ; day of the year
  1079. ("%l" . "%k") ; like %I blank-padded
  1080. ("%s" . "") ; no. of secs since 1970-01-01 00:00:00 +0000
  1081. ("%n" . "<text:line-break/>")
  1082. ("%r" . "%I:%M:%S %p")
  1083. ("%t" . "<text:tab/>")
  1084. ("%u\\|%w" . "") ; numeric day of week - Mon (1-7), Sun(0-6)
  1085. ("%x" . "%Y-%M-%d %a") ; locale's pref. time format
  1086. ("%z" . "") ; time zone in numeric form
  1087. ))
  1088. (setq fmt (replace-regexp-in-string (car pair) (cdr pair) fmt t t)))
  1089. (while (string-match re fmt start)
  1090. (setq match (match-string 0 fmt))
  1091. (setq rpl (assoc-default match fmt-alist))
  1092. (setq start (match-end 0))
  1093. (setq filler-end (match-beginning 0))
  1094. (setq filler (substring fmt (prog1 filler-beg
  1095. (setq filler-beg (match-end 0)))
  1096. filler-end))
  1097. (setq filler (and (not (string= filler ""))
  1098. (format "<number:text>%s</number:text>"
  1099. (org-odt--encode-plain-text filler))))
  1100. (setq output (concat output "\n" filler "\n" rpl)))
  1101. (setq filler (substring fmt filler-beg))
  1102. (unless (string= filler "")
  1103. (setq output (concat output
  1104. (format "\n<number:text>%s</number:text>"
  1105. (org-odt--encode-plain-text filler)))))
  1106. (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
  1107. style
  1108. (concat " number:automatic-order=\"true\""
  1109. " number:format-source=\"fixed\"")
  1110. output ))))
  1111. (defun org-odt-template (contents info)
  1112. "Return complete document string after ODT conversion.
  1113. CONTENTS is the transcoded contents string. RAW-DATA is the
  1114. original parsed data. INFO is a plist holding export options."
  1115. ;; Write meta file.
  1116. (let ((title (org-export-data (plist-get info :title) info))
  1117. (subtitle (org-export-data (plist-get info :subtitle) info))
  1118. (author (let ((author (plist-get info :author)))
  1119. (if (not author) "" (org-export-data author info))))
  1120. (keywords (or (plist-get info :keywords) ""))
  1121. (description (or (plist-get info :description) "")))
  1122. (write-region
  1123. (concat
  1124. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1125. <office:document-meta
  1126. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1127. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1128. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1129. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1130. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1131. office:version=\"1.2\">
  1132. <office:meta>\n"
  1133. (format "<dc:creator>%s</dc:creator>\n" author)
  1134. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1135. ;; Date, if required.
  1136. (when (plist-get info :with-date)
  1137. ;; Check if DATE is specified as an Org-timestamp. If yes,
  1138. ;; include it as meta information. Otherwise, just use
  1139. ;; today's date.
  1140. (let* ((date (let ((date (plist-get info :date)))
  1141. (and (not (cdr date))
  1142. (eq (org-element-type (car date)) 'timestamp)
  1143. (car date)))))
  1144. (let ((iso-date (org-odt--format-timestamp date nil 'iso-date)))
  1145. (concat
  1146. (format "<dc:date>%s</dc:date>\n" iso-date)
  1147. (format "<meta:creation-date>%s</meta:creation-date>\n"
  1148. iso-date)))))
  1149. (format "<meta:generator>%s</meta:generator>\n"
  1150. (plist-get info :creator))
  1151. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1152. (format "<dc:subject>%s</dc:subject>\n" description)
  1153. (format "<dc:title>%s</dc:title>\n" title)
  1154. (when (org-string-nw-p subtitle)
  1155. (format
  1156. "<meta:user-defined meta:name=\"subtitle\">%s</meta:user-defined>\n"
  1157. subtitle))
  1158. "\n"
  1159. " </office:meta>\n" "</office:document-meta>")
  1160. nil (concat org-odt-zip-dir "meta.xml"))
  1161. ;; Add meta.xml in to manifest.
  1162. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1163. ;; Update styles file.
  1164. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1165. ;; Write styles file.
  1166. (let* ((styles-file
  1167. (pcase (plist-get info :odt-styles-file)
  1168. (`nil (expand-file-name "OrgOdtStyles.xml" org-odt-styles-dir))
  1169. ((and s (pred (string-match-p "\\`(.*)\\'")))
  1170. (condition-case nil
  1171. (read s)
  1172. (error (user-error "Invalid styles file specification: %S" s))))
  1173. (filename (org-strip-quotes filename)))))
  1174. (cond
  1175. ;; Non-availability of styles.xml is not a critical error. For
  1176. ;; now, throw an error.
  1177. ((null styles-file) (error "Missing styles file"))
  1178. ((listp styles-file)
  1179. (let ((archive (nth 0 styles-file))
  1180. (members (nth 1 styles-file)))
  1181. (org-odt--zip-extract archive members org-odt-zip-dir)
  1182. (dolist (member members)
  1183. (when (org-file-image-p member)
  1184. (let* ((image-type (file-name-extension member))
  1185. (media-type (format "image/%s" image-type)))
  1186. (org-odt-create-manifest-file-entry media-type member))))))
  1187. ((file-exists-p styles-file)
  1188. (let ((styles-file-type (file-name-extension styles-file)))
  1189. (cond
  1190. ((string= styles-file-type "xml")
  1191. (copy-file styles-file (concat org-odt-zip-dir "styles.xml") t))
  1192. ((member styles-file-type '("odt" "ott"))
  1193. (org-odt--zip-extract styles-file "styles.xml" org-odt-zip-dir)))))
  1194. (t
  1195. (error "Invalid specification of styles.xml file: %S"
  1196. (plist-get info :odt-styles-file))))
  1197. ;; create a manifest entry for styles.xml
  1198. (org-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1199. ;; Ensure we have write permissions to this file.
  1200. (set-file-modes (concat org-odt-zip-dir "styles.xml") #o600)
  1201. ;; FIXME: Who is opening an empty styles.xml before this point?
  1202. (with-current-buffer
  1203. (find-file-noselect (concat org-odt-zip-dir "styles.xml") t)
  1204. (revert-buffer t t)
  1205. ;; Write custom styles for source blocks
  1206. ;; Save STYLES used for colorizing of source blocks.
  1207. ;; Update styles.xml with styles that were collected as part of
  1208. ;; `org-odt-hfy-face-to-css' callbacks.
  1209. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1210. hfy-user-sheet-assoc "")))
  1211. (when styles
  1212. (goto-char (point-min))
  1213. (when (re-search-forward "</office:styles>" nil t)
  1214. (goto-char (match-beginning 0))
  1215. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1216. ;; Update styles.xml - take care of outline numbering
  1217. ;; Don't make automatic backup of styles.xml file. This setting
  1218. ;; prevents the backed-up styles.xml file from being zipped in to
  1219. ;; odt file. This is more of a hackish fix. Better alternative
  1220. ;; would be to fix the zip command so that the output odt file
  1221. ;; includes only the needed files and excludes any auto-generated
  1222. ;; extra files like backups and auto-saves etc etc. Note that
  1223. ;; currently the zip command zips up the entire temp directory so
  1224. ;; that any auto-generated files created under the hood ends up in
  1225. ;; the resulting odt file.
  1226. (setq-local backup-inhibited t)
  1227. ;; Outline numbering is retained only up to LEVEL.
  1228. ;; To disable outline numbering pass a LEVEL of 0.
  1229. (goto-char (point-min))
  1230. (let ((regex
  1231. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1232. (replacement
  1233. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1234. (while (re-search-forward regex nil t)
  1235. (unless (let ((sec-num (plist-get info :section-numbers))
  1236. (level (string-to-number (match-string 2))))
  1237. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1238. (replace-match replacement t nil))))
  1239. (save-buffer 0)))
  1240. ;; Update content.xml.
  1241. (let* ( ;; `org-display-custom-times' should be accessed right
  1242. ;; within the context of the Org buffer. So obtain its
  1243. ;; value before moving on to temp-buffer context down below.
  1244. (custom-time-fmts
  1245. (if org-display-custom-times
  1246. (cons (substring (car org-time-stamp-custom-formats) 1 -1)
  1247. (substring (cdr org-time-stamp-custom-formats) 1 -1))
  1248. '("%Y-%M-%d %a" . "%Y-%M-%d %a %H:%M"))))
  1249. (with-temp-buffer
  1250. (insert-file-contents
  1251. (or (plist-get info :odt-content-template-file)
  1252. (expand-file-name "OrgOdtContentTemplate.xml"
  1253. org-odt-styles-dir)))
  1254. ;; Write automatic styles.
  1255. ;; - Position the cursor.
  1256. (goto-char (point-min))
  1257. (re-search-forward " </office:automatic-styles>" nil t)
  1258. (goto-char (match-beginning 0))
  1259. ;; - Dump automatic table styles.
  1260. (cl-loop for (style-name props) in
  1261. (plist-get org-odt-automatic-styles 'Table) do
  1262. (when (setq props (or (plist-get props :rel-width) "96"))
  1263. (insert (format org-odt-table-style-format style-name props))))
  1264. ;; - Dump date-styles.
  1265. (when (plist-get info :odt-use-date-fields)
  1266. (insert (org-odt--build-date-styles (car custom-time-fmts)
  1267. "OrgDate1")
  1268. (org-odt--build-date-styles (cdr custom-time-fmts)
  1269. "OrgDate2")))
  1270. ;; Update display level.
  1271. ;; - Remove existing sequence decls. Also position the cursor.
  1272. (goto-char (point-min))
  1273. (when (re-search-forward "<text:sequence-decls" nil t)
  1274. (delete-region (match-beginning 0)
  1275. (re-search-forward "</text:sequence-decls>" nil nil)))
  1276. ;; Update sequence decls according to user preference.
  1277. (insert
  1278. (format
  1279. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1280. (mapconcat
  1281. (lambda (x)
  1282. (format
  1283. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1284. (plist-get info :odt-display-outline-level)
  1285. (nth 1 x)))
  1286. org-odt-category-map-alist "\n")))
  1287. ;; Position the cursor to document body.
  1288. (goto-char (point-min))
  1289. (re-search-forward "</office:text>" nil nil)
  1290. (goto-char (match-beginning 0))
  1291. ;; Preamble - Title, Author, Date etc.
  1292. (insert
  1293. (let* ((title (and (plist-get info :with-title)
  1294. (org-export-data (plist-get info :title) info)))
  1295. (subtitle (when title
  1296. (org-export-data (plist-get info :subtitle) info)))
  1297. (author (and (plist-get info :with-author)
  1298. (let ((auth (plist-get info :author)))
  1299. (and auth (org-export-data auth info)))))
  1300. (email (plist-get info :email))
  1301. ;; Switch on or off above vars based on user settings
  1302. (author (and (plist-get info :with-author) (or author email)))
  1303. (email (and (plist-get info :with-email) email)))
  1304. (concat
  1305. ;; Title.
  1306. (when (org-string-nw-p title)
  1307. (concat
  1308. (format "\n<text:p text:style-name=\"%s\">%s</text:p>\n"
  1309. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1310. ;; Separator.
  1311. "\n<text:p text:style-name=\"OrgTitle\"/>\n"
  1312. ;; Subtitle.
  1313. (when (org-string-nw-p subtitle)
  1314. (concat
  1315. (format "<text:p text:style-name=\"OrgSubtitle\">\n%s\n</text:p>\n"
  1316. (concat
  1317. "<text:user-defined style:data-style-name=\"N0\" text:name=\"subtitle\">\n"
  1318. subtitle
  1319. "</text:user-defined>\n"))
  1320. ;; Separator.
  1321. "<text:p text:style-name=\"OrgSubtitle\"/>\n"))))
  1322. (cond
  1323. ((and author (not email))
  1324. ;; Author only.
  1325. (concat
  1326. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1327. "OrgSubtitle"
  1328. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1329. ;; Separator.
  1330. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1331. ((and author email)
  1332. ;; Author and E-mail.
  1333. (concat
  1334. (format
  1335. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1336. "OrgSubtitle"
  1337. (format
  1338. "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1339. (concat "mailto:" email)
  1340. (format "<text:initial-creator>%s</text:initial-creator>" author)))
  1341. ;; Separator.
  1342. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1343. ;; Date, if required.
  1344. (when (plist-get info :with-date)
  1345. (let* ((date (plist-get info :date))
  1346. ;; Check if DATE is specified as a timestamp.
  1347. (timestamp (and (not (cdr date))
  1348. (eq (org-element-type (car date)) 'timestamp)
  1349. (car date))))
  1350. (when date
  1351. (concat
  1352. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1353. "OrgSubtitle"
  1354. (if (and (plist-get info :odt-use-date-fields) timestamp)
  1355. (org-odt--format-timestamp (car date))
  1356. (org-export-data date info)))
  1357. ;; Separator
  1358. "<text:p text:style-name=\"OrgSubtitle\"/>")))))))
  1359. ;; Table of Contents
  1360. (let* ((with-toc (plist-get info :with-toc))
  1361. (depth (and with-toc (if (wholenump with-toc)
  1362. with-toc
  1363. (plist-get info :headline-levels)))))
  1364. (when depth (insert (or (org-odt-toc depth info) ""))))
  1365. ;; Contents.
  1366. (insert contents)
  1367. ;; Return contents.
  1368. (buffer-substring-no-properties (point-min) (point-max)))))
  1369. ;;; Transcode Functions
  1370. ;;;; Bold
  1371. (defun org-odt-bold (_bold contents _info)
  1372. "Transcode BOLD from Org to ODT.
  1373. CONTENTS is the text with bold markup. INFO is a plist holding
  1374. contextual information."
  1375. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1376. "Bold" contents))
  1377. ;;;; Center Block
  1378. (defun org-odt-center-block (_center-block contents _info)
  1379. "Transcode a CENTER-BLOCK element from Org to ODT.
  1380. CONTENTS holds the contents of the center block. INFO is a plist
  1381. holding contextual information."
  1382. contents)
  1383. ;;;; Clock
  1384. (defun org-odt-clock (clock contents info)
  1385. "Transcode a CLOCK element from Org to ODT.
  1386. CONTENTS is nil. INFO is a plist used as a communication
  1387. channel."
  1388. (let ((timestamp (org-element-property :value clock))
  1389. (duration (org-element-property :duration clock)))
  1390. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1391. (if (eq (org-element-type (org-export-get-next-element clock info))
  1392. 'clock) "OrgClock" "OrgClockLastLine")
  1393. (concat
  1394. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1395. "OrgClockKeyword" org-clock-string)
  1396. (org-odt-timestamp timestamp contents info)
  1397. (and duration (format " (%s)" duration))))))
  1398. ;;;; Code
  1399. (defun org-odt-code (code _contents _info)
  1400. "Transcode a CODE object from Org to ODT.
  1401. CONTENTS is nil. INFO is a plist used as a communication
  1402. channel."
  1403. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1404. "OrgCode" (org-odt--encode-plain-text
  1405. (org-element-property :value code))))
  1406. ;;;; Drawer
  1407. (defun org-odt-drawer (drawer contents info)
  1408. "Transcode a DRAWER element from Org to ODT.
  1409. CONTENTS holds the contents of the block. INFO is a plist
  1410. holding contextual information."
  1411. (let* ((name (org-element-property :drawer-name drawer))
  1412. (output (funcall (plist-get info :odt-format-drawer-function)
  1413. name contents)))
  1414. output))
  1415. ;;;; Dynamic Block
  1416. (defun org-odt-dynamic-block (_dynamic-block contents _info)
  1417. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1418. CONTENTS holds the contents of the block. INFO is a plist
  1419. holding contextual information. See `org-export-data'."
  1420. contents)
  1421. ;;;; Entity
  1422. (defun org-odt-entity (entity _contents _info)
  1423. "Transcode an ENTITY object from Org to ODT.
  1424. CONTENTS are the definition itself. INFO is a plist holding
  1425. contextual information."
  1426. (org-element-property :utf-8 entity))
  1427. ;;;; Example Block
  1428. (defun org-odt-example-block (example-block _contents info)
  1429. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1430. CONTENTS is nil. INFO is a plist holding contextual information."
  1431. (org-odt-format-code example-block info))
  1432. ;;;; Export Snippet
  1433. (defun org-odt-export-snippet (export-snippet _contents _info)
  1434. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1435. CONTENTS is nil. INFO is a plist holding contextual information."
  1436. (when (eq (org-export-snippet-backend export-snippet) 'odt)
  1437. (org-element-property :value export-snippet)))
  1438. ;;;; Export Block
  1439. (defun org-odt-export-block (export-block _contents _info)
  1440. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1441. CONTENTS is nil. INFO is a plist holding contextual information."
  1442. (when (string= (org-element-property :type export-block) "ODT")
  1443. (org-remove-indentation (org-element-property :value export-block))))
  1444. ;;;; Fixed Width
  1445. (defun org-odt-fixed-width (fixed-width _contents info)
  1446. "Transcode a FIXED-WIDTH element from Org to ODT.
  1447. CONTENTS is nil. INFO is a plist holding contextual information."
  1448. (org-odt-do-format-code (org-element-property :value fixed-width) info))
  1449. ;;;; Footnote Definition
  1450. ;; Footnote Definitions are ignored.
  1451. ;;;; Footnote Reference
  1452. (defun org-odt-footnote-reference (footnote-reference _contents info)
  1453. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1454. CONTENTS is nil. INFO is a plist holding contextual information."
  1455. (let ((--format-footnote-definition
  1456. (lambda (n def)
  1457. (setq n (format "%d" n))
  1458. (let ((id (concat "fn" n))
  1459. (note-class "footnote"))
  1460. (format
  1461. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1462. id note-class
  1463. (concat
  1464. (format "<text:note-citation>%s</text:note-citation>" n)
  1465. (format "<text:note-body>%s</text:note-body>" def))))))
  1466. (--format-footnote-reference
  1467. (lambda (n)
  1468. (setq n (format "%d" n))
  1469. (let ((note-class "footnote")
  1470. (ref-format "text")
  1471. (ref-name (concat "fn" n)))
  1472. (format
  1473. "<text:span text:style-name=\"%s\">%s</text:span>"
  1474. "OrgSuperscript"
  1475. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1476. note-class ref-format ref-name n))))))
  1477. (concat
  1478. ;; Insert separator between two footnotes in a row.
  1479. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1480. (and (eq (org-element-type prev) 'footnote-reference)
  1481. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1482. "OrgSuperscript" ",")))
  1483. ;; Transcode footnote reference.
  1484. (let ((n (org-export-get-footnote-number footnote-reference info nil t)))
  1485. (cond
  1486. ((not
  1487. (org-export-footnote-first-reference-p footnote-reference info nil t))
  1488. (funcall --format-footnote-reference n))
  1489. (t
  1490. (let* ((raw (org-export-get-footnote-definition
  1491. footnote-reference info))
  1492. (def
  1493. (let ((def (org-trim
  1494. (org-export-data-with-backend
  1495. raw
  1496. (org-export-create-backend
  1497. :parent 'odt
  1498. :transcoders
  1499. '((paragraph . (lambda (p c i)
  1500. (org-odt--format-paragraph
  1501. p c i
  1502. "Footnote"
  1503. "OrgFootnoteCenter"
  1504. "OrgFootnoteQuotations")))))
  1505. info))))
  1506. ;; Inline definitions are secondary strings. We
  1507. ;; need to wrap them within a paragraph.
  1508. (if (eq (org-element-class (car (org-element-contents raw)))
  1509. 'element)
  1510. def
  1511. (format
  1512. "\n<text:p text:style-name=\"Footnote\">%s</text:p>"
  1513. def)))))
  1514. (funcall --format-footnote-definition n def))))))))
  1515. ;;;; Headline
  1516. (defun org-odt-format-headline--wrap (headline backend info
  1517. &optional format-function
  1518. &rest extra-keys)
  1519. "Transcode a HEADLINE element using BACKEND.
  1520. INFO is a plist holding contextual information."
  1521. (setq backend (or backend (plist-get info :back-end)))
  1522. (let* ((level (+ (org-export-get-relative-level headline info)))
  1523. (headline-number (org-export-get-headline-number headline info))
  1524. (section-number (and (org-export-numbered-headline-p headline info)
  1525. (mapconcat 'number-to-string
  1526. headline-number ".")))
  1527. (todo (and (plist-get info :with-todo-keywords)
  1528. (let ((todo (org-element-property :todo-keyword headline)))
  1529. (and todo
  1530. (org-export-data-with-backend todo backend info)))))
  1531. (todo-type (and todo (org-element-property :todo-type headline)))
  1532. (priority (and (plist-get info :with-priority)
  1533. (org-element-property :priority headline)))
  1534. (text (org-export-data-with-backend
  1535. (org-element-property :title headline) backend info))
  1536. (tags (and (plist-get info :with-tags)
  1537. (org-export-get-tags headline info)))
  1538. (headline-label (org-export-get-reference headline info))
  1539. (format-function
  1540. (if (functionp format-function) format-function
  1541. (cl-function
  1542. (lambda (todo todo-type priority text tags
  1543. &key _level _section-number _headline-label
  1544. &allow-other-keys)
  1545. (funcall (plist-get info :odt-format-headline-function)
  1546. todo todo-type priority text tags))))))
  1547. (apply format-function
  1548. todo todo-type priority text tags
  1549. :headline-label headline-label
  1550. :level level
  1551. :section-number section-number extra-keys)))
  1552. (defun org-odt-headline (headline contents info)
  1553. "Transcode a HEADLINE element from Org to ODT.
  1554. CONTENTS holds the contents of the headline. INFO is a plist
  1555. holding contextual information."
  1556. ;; Case 1: This is a footnote section: ignore it.
  1557. (unless (org-element-property :footnote-section-p headline)
  1558. (let* ((full-text (org-odt-format-headline--wrap headline nil info))
  1559. ;; Get level relative to current parsed data.
  1560. (level (org-export-get-relative-level headline info))
  1561. (numbered (org-export-numbered-headline-p headline info))
  1562. ;; Get canonical label for the headline.
  1563. (id (org-export-get-reference headline info))
  1564. ;; Extra targets.
  1565. (extra-targets
  1566. (let ((id (org-element-property :ID headline)))
  1567. (if id (org-odt--target "" (concat "ID-" id)) "")))
  1568. ;; Title.
  1569. (anchored-title (org-odt--target full-text id)))
  1570. (cond
  1571. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1572. ;; Also export as items headlines for which no section
  1573. ;; format has been found.
  1574. ((org-export-low-level-p headline info)
  1575. ;; Build the real contents of the sub-tree.
  1576. (concat
  1577. (and (org-export-first-sibling-p headline info)
  1578. (format "\n<text:list text:style-name=\"%s\" %s>"
  1579. ;; Choose style based on list type.
  1580. (if numbered "OrgNumberedList" "OrgBulletedList")
  1581. ;; If top-level list, re-start numbering. Otherwise,
  1582. ;; continue numbering.
  1583. (format "text:continue-numbering=\"%s\""
  1584. (let* ((parent (org-export-get-parent-headline
  1585. headline)))
  1586. (if (and parent
  1587. (org-export-low-level-p parent info))
  1588. "true" "false")))))
  1589. (let ((headline-has-table-p
  1590. (let ((section (assq 'section (org-element-contents headline))))
  1591. (assq 'table (and section (org-element-contents section))))))
  1592. (format "\n<text:list-item>\n%s\n%s"
  1593. (concat
  1594. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1595. "Text_20_body"
  1596. (concat extra-targets anchored-title))
  1597. contents)
  1598. (if headline-has-table-p
  1599. "</text:list-header>"
  1600. "</text:list-item>")))
  1601. (and (org-export-last-sibling-p headline info)
  1602. "</text:list>")))
  1603. ;; Case 3. Standard headline. Export it as a section.
  1604. (t
  1605. (concat
  1606. (format
  1607. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\" text:is-list-header=\"%s\">%s</text:h>"
  1608. (format "Heading_20_%s%s"
  1609. level (if numbered "" "_unnumbered"))
  1610. level
  1611. (if numbered "false" "true")
  1612. (concat extra-targets anchored-title))
  1613. contents))))))
  1614. (defun org-odt-format-headline-default-function
  1615. (todo todo-type priority text tags)
  1616. "Default format function for a headline.
  1617. See `org-odt-format-headline-function' for details."
  1618. (concat
  1619. ;; Todo.
  1620. (when todo
  1621. (let ((style (if (eq todo-type 'done) "OrgDone" "OrgTodo")))
  1622. (format "<text:span text:style-name=\"%s\">%s</text:span> " style todo)))
  1623. (when priority
  1624. (let* ((style (format "OrgPriority-%c" priority))
  1625. (priority (format "[#%c]" priority)))
  1626. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  1627. style priority)))
  1628. ;; Title.
  1629. text
  1630. ;; Tags.
  1631. (when tags
  1632. (concat
  1633. "<text:tab/>"
  1634. (format "<text:span text:style-name=\"%s\">[%s]</text:span>"
  1635. "OrgTags" (mapconcat
  1636. (lambda (tag)
  1637. (format
  1638. "<text:span text:style-name=\"%s\">%s</text:span>"
  1639. "OrgTag" tag)) tags " : "))))))
  1640. ;;;; Horizontal Rule
  1641. (defun org-odt-horizontal-rule (_horizontal-rule _contents _info)
  1642. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1643. CONTENTS is nil. INFO is a plist holding contextual information."
  1644. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1645. "Horizontal_20_Line" ""))
  1646. ;;;; Inline Babel Call
  1647. ;; Inline Babel Calls are ignored.
  1648. ;;;; Inline Src Block
  1649. (defun org-odt--find-verb-separator (s)
  1650. "Return a character not used in string S.
  1651. This is used to choose a separator for constructs like \\verb."
  1652. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1653. (cl-loop for c across ll
  1654. when (not (string-match (regexp-quote (char-to-string c)) s))
  1655. return (char-to-string c))))
  1656. (defun org-odt-inline-src-block (_inline-src-block _contents _info)
  1657. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1658. CONTENTS holds the contents of the item. INFO is a plist holding
  1659. contextual information."
  1660. (error "FIXME"))
  1661. ;;;; Inlinetask
  1662. (defun org-odt-inlinetask (inlinetask contents info)
  1663. "Transcode an INLINETASK element from Org to ODT.
  1664. CONTENTS holds the contents of the block. INFO is a plist
  1665. holding contextual information."
  1666. (let* ((todo
  1667. (and (plist-get info :with-todo-keywords)
  1668. (let ((todo (org-element-property :todo-keyword inlinetask)))
  1669. (and todo (org-export-data todo info)))))
  1670. (todo-type (and todo (org-element-property :todo-type inlinetask)))
  1671. (priority (and (plist-get info :with-priority)
  1672. (org-element-property :priority inlinetask)))
  1673. (text (org-export-data (org-element-property :title inlinetask) info))
  1674. (tags (and (plist-get info :with-tags)
  1675. (org-export-get-tags inlinetask info))))
  1676. (funcall (plist-get info :odt-format-inlinetask-function)
  1677. todo todo-type priority text tags contents)))
  1678. (defun org-odt-format-inlinetask-default-function
  1679. (todo todo-type priority name tags contents)
  1680. "Default format function for inlinetasks.
  1681. See `org-odt-format-inlinetask-function' for details."
  1682. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1683. "Text_20_body"
  1684. (org-odt--textbox
  1685. (concat
  1686. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1687. "OrgInlineTaskHeading"
  1688. (org-odt-format-headline-default-function
  1689. todo todo-type priority name tags))
  1690. contents)
  1691. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))
  1692. ;;;; Italic
  1693. (defun org-odt-italic (_italic contents _info)
  1694. "Transcode ITALIC from Org to ODT.
  1695. CONTENTS is the text with italic markup. INFO is a plist holding
  1696. contextual information."
  1697. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1698. "Emphasis" contents))
  1699. ;;;; Item
  1700. (defun org-odt-item (item contents info)
  1701. "Transcode an ITEM element from Org to ODT.
  1702. CONTENTS holds the contents of the item. INFO is a plist holding
  1703. contextual information."
  1704. (let* ((plain-list (org-export-get-parent item))
  1705. (count (org-element-property :counter item))
  1706. (type (org-element-property :type plain-list)))
  1707. (unless (memq type '(ordered unordered descriptive-1 descriptive-2))
  1708. (error "Unknown list type: %S" type))
  1709. (format "\n<text:list-item%s>\n%s\n%s"
  1710. (if count (format " text:start-value=\"%s\"" count) "")
  1711. contents
  1712. (if (org-element-map item 'table #'identity info 'first-match)
  1713. "</text:list-header>"
  1714. "</text:list-item>"))))
  1715. ;;;; Keyword
  1716. (defun org-odt-keyword (keyword _contents info)
  1717. "Transcode a KEYWORD element from Org to ODT.
  1718. CONTENTS is nil. INFO is a plist holding contextual
  1719. information."
  1720. (let ((key (org-element-property :key keyword))
  1721. (value (org-element-property :value keyword)))
  1722. (cond
  1723. ((string= key "ODT") value)
  1724. ((string= key "INDEX")
  1725. ;; FIXME
  1726. (ignore))
  1727. ((string= key "TOC")
  1728. (let ((case-fold-search t))
  1729. (cond
  1730. ((string-match-p "\\<headlines\\>" value)
  1731. (let ((depth (or (and (string-match "\\<[0-9]+\\>" value)
  1732. (string-to-number (match-string 0 value)))
  1733. (plist-get info :headline-levels)))
  1734. (scope
  1735. (cond
  1736. ((string-match ":target +\\(\".+?\"\\|\\S-+\\)" value) ;link
  1737. (org-export-resolve-link
  1738. (org-strip-quotes (match-string 1 value)) info))
  1739. ((string-match-p "\\<local\\>" value) keyword)))) ;local
  1740. (org-odt-toc depth info scope)))
  1741. ((string-match-p "tables\\|figures\\|listings" value)
  1742. ;; FIXME
  1743. (ignore))))))))
  1744. ;;;; Latex Environment
  1745. ;; (eval-after-load 'ox-odt '(ad-deactivate 'org-format-latex-as-mathml))
  1746. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1747. ;; (after org-odt-protect-latex-fragment activate)
  1748. ;; "Encode LaTeX fragment as XML.
  1749. ;; Do this when translation to MathML fails."
  1750. ;; (unless (> (length ad-return-value) 0)
  1751. ;; (setq ad-return-value (org-odt--encode-plain-text (ad-get-arg 0)))))
  1752. (defun org-odt-latex-environment (latex-environment _contents info)
  1753. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1754. CONTENTS is nil. INFO is a plist holding contextual information."
  1755. (let* ((latex-frag (org-remove-indentation
  1756. (org-element-property :value latex-environment))))
  1757. (org-odt-do-format-code latex-frag info)))
  1758. ;;;; Latex Fragment
  1759. ;; (when latex-frag ; FIXME
  1760. ;; (setq href (propertize href :title "LaTeX Fragment"
  1761. ;; :description latex-frag)))
  1762. ;; handle verbatim
  1763. ;; provide descriptions
  1764. (defun org-odt-latex-fragment (latex-fragment _contents _info)
  1765. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1766. CONTENTS is nil. INFO is a plist holding contextual information."
  1767. (let ((latex-frag (org-element-property :value latex-fragment)))
  1768. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1769. "OrgCode" (org-odt--encode-plain-text latex-frag t))))
  1770. ;;;; Line Break
  1771. (defun org-odt-line-break (_line-break _contents _info)
  1772. "Transcode a LINE-BREAK object from Org to ODT.
  1773. CONTENTS is nil. INFO is a plist holding contextual information."
  1774. "<text:line-break/>")
  1775. ;;;; Link
  1776. ;;;; Links :: Label references
  1777. (defun org-odt--enumerate (element info &optional predicate n)
  1778. (when predicate (cl-assert (funcall predicate element info)))
  1779. (let* ((--numbered-parent-headline-at-<=-n
  1780. (lambda (element n info)
  1781. (cl-loop for x in (org-element-lineage element)
  1782. thereis (and (eq (org-element-type x) 'headline)
  1783. (<= (org-export-get-relative-level x info) n)
  1784. (org-export-numbered-headline-p x info)
  1785. x))))
  1786. (--enumerate
  1787. (lambda (element scope info &optional predicate)
  1788. (let ((counter 0))
  1789. (org-element-map (or scope (plist-get info :parse-tree))
  1790. (org-element-type element)
  1791. (lambda (el)
  1792. (and (or (not predicate) (funcall predicate el info))
  1793. (cl-incf counter)
  1794. (eq element el)
  1795. counter))
  1796. info 'first-match))))
  1797. (scope (funcall --numbered-parent-headline-at-<=-n
  1798. element
  1799. (or n (plist-get info :odt-display-outline-level))
  1800. info))
  1801. (ordinal (funcall --enumerate element scope info predicate))
  1802. (tag
  1803. (concat
  1804. ;; Section number.
  1805. (and scope
  1806. (mapconcat 'number-to-string
  1807. (org-export-get-headline-number scope info) "."))
  1808. ;; Separator.
  1809. (and scope ".")
  1810. ;; Ordinal.
  1811. (number-to-string ordinal))))
  1812. tag))
  1813. (defun org-odt-format-label (element info op)
  1814. "Return a label for ELEMENT.
  1815. ELEMENT is a `link', `table', `src-block' or `paragraph' type
  1816. element. INFO is a plist used as a communication channel. OP is
  1817. either `definition' or `reference', depending on the purpose of
  1818. the generated string.
  1819. Return value is a string if OP is set to `reference' or a cons
  1820. cell like CAPTION . SHORT-CAPTION) where CAPTION and
  1821. SHORT-CAPTION are strings."
  1822. (cl-assert (memq (org-element-type element) '(link table src-block paragraph)))
  1823. (let* ((element-or-parent
  1824. (cl-case (org-element-type element)
  1825. (link (org-export-get-parent-element element))
  1826. (t element)))
  1827. ;; Get label and caption.
  1828. (label (and (or (org-element-property :name element)
  1829. (org-element-property :name element-or-parent))
  1830. (org-export-get-reference element-or-parent info)))
  1831. (caption (let ((c (org-export-get-caption element-or-parent)))
  1832. (and c (org-export-data c info))))
  1833. ;; FIXME: We don't use short-caption for now
  1834. ;; (short-caption nil)
  1835. )
  1836. (when (or label caption)
  1837. (let* ((default-category
  1838. (cl-case (org-element-type element)
  1839. (table "__Table__")
  1840. (src-block "__Listing__")
  1841. ((link paragraph)
  1842. (cond
  1843. ((org-odt--enumerable-latex-image-p element info)
  1844. "__DvipngImage__")
  1845. ((org-odt--enumerable-image-p element info)
  1846. "__Figure__")
  1847. ((org-odt--enumerable-formula-p element info)
  1848. "__MathFormula__")
  1849. (t (error "Don't know how to format label for link: %S"
  1850. element))))
  1851. (t (error "Don't know how to format label for element type: %s"
  1852. (org-element-type element)))))
  1853. seqno)
  1854. (cl-assert default-category)
  1855. (pcase-let
  1856. ((`(,counter ,label-style ,category ,predicate)
  1857. (assoc-default default-category org-odt-category-map-alist)))
  1858. ;; Compute sequence number of the element.
  1859. (setq seqno (org-odt--enumerate element info predicate))
  1860. ;; Localize category string.
  1861. (setq category (org-export-translate category :utf-8 info))
  1862. (cl-case op
  1863. ;; Case 1: Handle Label definition.
  1864. (definition
  1865. (cons
  1866. (concat
  1867. ;; Sneak in a bookmark. The bookmark is used when the
  1868. ;; labeled element is referenced with a link that
  1869. ;; provides its own description.
  1870. (format "\n<text:bookmark text:name=\"%s\"/>" label)
  1871. ;; Label definition: Typically formatted as below:
  1872. ;; CATEGORY SEQ-NO: LONG CAPTION
  1873. ;; with translation for correct punctuation.
  1874. (format-spec
  1875. (org-export-translate
  1876. (cadr (assoc-string label-style org-odt-label-styles t))
  1877. :utf-8 info)
  1878. `((?e . ,category)
  1879. (?n . ,(format
  1880. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1881. label counter counter seqno))
  1882. (?c . ,(or caption "")))))
  1883. nil)) ;; short-caption
  1884. ;; Case 2: Handle Label reference.
  1885. (reference
  1886. (let* ((fmt (cddr (assoc-string label-style org-odt-label-styles t)))
  1887. (fmt1 (car fmt))
  1888. (fmt2 (cadr fmt)))
  1889. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1890. fmt1
  1891. label
  1892. (format-spec fmt2 `((?e . ,category) (?n . ,seqno))))))
  1893. (t (error "Unknown %S on label" op))))))))
  1894. ;;;; Links :: Inline Images
  1895. (defun org-odt--copy-image-file (path)
  1896. "Return the internal name of the file"
  1897. (let* ((image-type (file-name-extension path))
  1898. (media-type (format "image/%s" image-type))
  1899. (target-dir "Images/")
  1900. (target-file
  1901. (format "%s%04d.%s" target-dir
  1902. (cl-incf org-odt-embedded-images-count) image-type)))
  1903. (message "Embedding %s as %s..."
  1904. (substring-no-properties path) target-file)
  1905. (when (= 1 org-odt-embedded-images-count)
  1906. (make-directory (concat org-odt-zip-dir target-dir))
  1907. (org-odt-create-manifest-file-entry "" target-dir))
  1908. (copy-file path (concat org-odt-zip-dir target-file) 'overwrite)
  1909. (org-odt-create-manifest-file-entry media-type target-file)
  1910. target-file))
  1911. ;; For --without-x builds.
  1912. (declare-function clear-image-cache "image.c" (&optional filter))
  1913. (declare-function image-size "image.c" (spec &optional pixels frame))
  1914. (defun org-odt--image-size
  1915. (file info &optional user-width user-height scale dpi embed-as)
  1916. (let* ((--pixels-to-cms
  1917. (lambda (pixels dpi)
  1918. (let ((cms-per-inch 2.54)
  1919. (inches (/ pixels dpi)))
  1920. (* cms-per-inch inches))))
  1921. (--size-in-cms
  1922. (lambda (size-in-pixels dpi)
  1923. (and size-in-pixels
  1924. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  1925. (funcall --pixels-to-cms (cdr size-in-pixels) dpi)))))
  1926. (dpi (or dpi (plist-get info :odt-pixels-per-inch)))
  1927. (anchor-type (or embed-as "paragraph"))
  1928. (user-width (and (not scale) user-width))
  1929. (user-height (and (not scale) user-height))
  1930. (size
  1931. (and
  1932. (not (and user-height user-width))
  1933. (or
  1934. ;; Use Imagemagick.
  1935. (and (executable-find "identify")
  1936. (let ((size-in-pixels
  1937. (let ((dim (shell-command-to-string
  1938. (format "identify -format \"%%w:%%h\" \"%s\""
  1939. file))))
  1940. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1941. (cons (string-to-number (match-string 1 dim))
  1942. (string-to-number (match-string 2 dim)))))))
  1943. (funcall --size-in-cms size-in-pixels dpi)))
  1944. ;; Use Emacs.
  1945. (let ((size-in-pixels
  1946. (ignore-errors ; Emacs could be in batch mode
  1947. (clear-image-cache)
  1948. (image-size (create-image file) 'pixels))))
  1949. (funcall --size-in-cms size-in-pixels dpi))
  1950. ;; Use hard-coded values.
  1951. (cdr (assoc-string anchor-type
  1952. org-odt-default-image-sizes-alist))
  1953. ;; Error out.
  1954. (error "Cannot determine image size, aborting"))))
  1955. (width (car size)) (height (cdr size)))
  1956. (cond
  1957. (scale
  1958. (setq width (* width scale) height (* height scale)))
  1959. ((and user-height user-width)
  1960. (setq width user-width height user-height))
  1961. (user-height
  1962. (setq width (* user-height (/ width height)) height user-height))
  1963. (user-width
  1964. (setq height (* user-width (/ height width)) width user-width))
  1965. (t (ignore)))
  1966. ;; ensure that an embedded image fits comfortably within a page
  1967. (let ((max-width (car org-odt-max-image-size))
  1968. (max-height (cdr org-odt-max-image-size)))
  1969. (when (or (> width max-width) (> height max-height))
  1970. (let* ((scale1 (/ max-width width))
  1971. (scale2 (/ max-height height))
  1972. (scale (min scale1 scale2)))
  1973. (setq width (* scale width) height (* scale height)))))
  1974. (cons width height)))
  1975. (defun org-odt-link--inline-image (element info)
  1976. "Return ODT code for an inline image.
  1977. LINK is the link pointing to the inline image. INFO is a plist
  1978. used as a communication channel."
  1979. (cl-assert (eq (org-element-type element) 'link))
  1980. (let* ((src (let* ((type (org-element-property :type element))
  1981. (raw-path (org-element-property :path element)))
  1982. (cond ((member type '("http" "https"))
  1983. (concat type ":" raw-path))
  1984. ((file-name-absolute-p raw-path)
  1985. (expand-file-name raw-path))
  1986. (t raw-path))))
  1987. (src-expanded (if (file-name-absolute-p src) src
  1988. (expand-file-name src (file-name-directory
  1989. (plist-get info :input-file)))))
  1990. (href (format
  1991. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  1992. (org-odt--copy-image-file src-expanded)))
  1993. ;; Extract attributes from #+ATTR_ODT line.
  1994. (attr-from (cl-case (org-element-type element)
  1995. (link (org-export-get-parent-element element))
  1996. (t element)))
  1997. ;; Convert attributes to a plist.
  1998. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  1999. ;; Handle `:anchor', `:style' and `:attributes' properties.
  2000. (user-frame-anchor
  2001. (car (assoc-string (plist-get attr-plist :anchor)
  2002. '(("as-char") ("paragraph") ("page")) t)))
  2003. (user-frame-style
  2004. (and user-frame-anchor (plist-get attr-plist :style)))
  2005. (user-frame-attrs
  2006. (and user-frame-anchor (plist-get attr-plist :attributes)))
  2007. (user-frame-params
  2008. (list user-frame-style user-frame-attrs user-frame-anchor))
  2009. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  2010. ;;
  2011. ;; Handle `:width', `:height' and `:scale' properties. Read
  2012. ;; them as numbers since we need them for computations.
  2013. (size (org-odt--image-size
  2014. src-expanded info
  2015. (let ((width (plist-get attr-plist :width)))
  2016. (and width (read width)))
  2017. (let ((length (plist-get attr-plist :length)))
  2018. (and length (read length)))
  2019. (let ((scale (plist-get attr-plist :scale)))
  2020. (and scale (read scale)))
  2021. nil ; embed-as
  2022. "paragraph" ; FIXME
  2023. ))
  2024. (width (car size)) (height (cdr size))
  2025. (standalone-link-p (org-odt--standalone-link-p element info))
  2026. (embed-as (if standalone-link-p "paragraph" "as-char"))
  2027. (captions (org-odt-format-label element info 'definition))
  2028. (caption (car captions))
  2029. (entity (concat (and caption "Captioned") embed-as "Image"))
  2030. ;; Check if this link was created by LaTeX-to-PNG converter.
  2031. (replaces (org-element-property
  2032. :replaces (if (not standalone-link-p) element
  2033. (org-export-get-parent-element element))))
  2034. ;; If yes, note down the type of the element - LaTeX Fragment
  2035. ;; or LaTeX environment. It will go in to frame title.
  2036. (title (and replaces (capitalize
  2037. (symbol-name (org-element-type replaces)))))
  2038. ;; If yes, note down its contents. It will go in to frame
  2039. ;; description. This quite useful for debugging.
  2040. (desc (and replaces (org-element-property :value replaces))))
  2041. (org-odt--render-image/formula entity href width height
  2042. captions user-frame-params title desc)))
  2043. ;;;; Links :: Math formula
  2044. (defun org-odt-link--inline-formula (element info)
  2045. (let* ((src (let ((raw-path (org-element-property :path element)))
  2046. (cond
  2047. ((file-name-absolute-p raw-path)
  2048. (expand-file-name raw-path))
  2049. (t raw-path))))
  2050. (src-expanded (if (file-name-absolute-p src) src
  2051. (expand-file-name src (file-name-directory
  2052. (plist-get info :input-file)))))
  2053. (href
  2054. (format
  2055. "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  2056. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  2057. (file-name-directory (org-odt--copy-formula-file src-expanded))))
  2058. (standalone-link-p (org-odt--standalone-link-p element info))
  2059. (embed-as (if standalone-link-p 'paragraph 'character))
  2060. (captions (org-odt-format-label element info 'definition))
  2061. ;; Check if this link was created by LaTeX-to-MathML
  2062. ;; converter.
  2063. (replaces (org-element-property
  2064. :replaces (if (not standalone-link-p) element
  2065. (org-export-get-parent-element element))))
  2066. ;; If yes, note down the type of the element - LaTeX Fragment
  2067. ;; or LaTeX environment. It will go in to frame title.
  2068. (title (and replaces (capitalize
  2069. (symbol-name (org-element-type replaces)))))
  2070. ;; If yes, note down its contents. It will go in to frame
  2071. ;; description. This quite useful for debugging.
  2072. (desc (and replaces (org-element-property :value replaces)))
  2073. ) ;; width height
  2074. (cond
  2075. ((eq embed-as 'character)
  2076. (org-odt--render-image/formula "InlineFormula" href nil nil ;; width height
  2077. nil nil title desc))
  2078. (t
  2079. (let* ((equation (org-odt--render-image/formula
  2080. "CaptionedDisplayFormula" href nil nil ;; width height
  2081. captions nil title desc))
  2082. (label
  2083. (let* ((org-odt-category-map-alist
  2084. '(("__MathFormula__" "Text" "math-label" "Equation"
  2085. org-odt--enumerable-formula-p))))
  2086. (car (org-odt-format-label element info 'definition)))))
  2087. (concat equation "<text:tab/>" label))))))
  2088. (defun org-odt--copy-formula-file (src-file)
  2089. "Return the internal name of the file"
  2090. (let* ((target-dir (format "Formula-%04d/"
  2091. (cl-incf org-odt-embedded-formulas-count)))
  2092. (target-file (concat target-dir "content.xml")))
  2093. ;; Create a directory for holding formula file. Also enter it in
  2094. ;; to manifest.
  2095. (make-directory (concat org-odt-zip-dir target-dir))
  2096. (org-odt-create-manifest-file-entry
  2097. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  2098. ;; Copy over the formula file from user directory to zip
  2099. ;; directory.
  2100. (message "Embedding %s as %s..." src-file target-file)
  2101. (let ((ext (file-name-extension src-file)))
  2102. (cond
  2103. ;; Case 1: Mathml.
  2104. ((member ext '("mathml" "mml"))
  2105. (copy-file src-file (concat org-odt-zip-dir target-file) 'overwrite))
  2106. ;; Case 2: OpenDocument formula.
  2107. ((string= ext "odf")
  2108. (org-odt--zip-extract src-file "content.xml"
  2109. (concat org-odt-zip-dir target-dir)))
  2110. (t (error "%s is not a formula file" src-file))))
  2111. ;; Enter the formula file in to manifest.
  2112. (org-odt-create-manifest-file-entry "text/xml" target-file)
  2113. target-file))
  2114. ;;;; Targets
  2115. (defun org-odt--render-image/formula (cfg-key href width height &optional
  2116. captions user-frame-params
  2117. &rest title-and-desc)
  2118. (let* ((frame-cfg-alist
  2119. ;; Each element of this alist is of the form (CFG-HANDLE
  2120. ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
  2121. ;; CFG-HANDLE is the key to the alist.
  2122. ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
  2123. ;; frame params for INNER-FRAME and OUTER-FRAME
  2124. ;; respectively. See below.
  2125. ;; Configurations that are meant to be applied to
  2126. ;; non-captioned image/formula specifies no
  2127. ;; OUTER-FRAME-PARAMS.
  2128. ;; TERMINOLOGY
  2129. ;; ===========
  2130. ;; INNER-FRAME :: Frame that directly surrounds an
  2131. ;; image/formula.
  2132. ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
  2133. ;; frame also contains the caption, if any.
  2134. ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
  2135. ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
  2136. ;; that these are the last three arguments
  2137. ;; to `org-odt--frame'.
  2138. ;; Note that an un-captioned image/formula requires just an
  2139. ;; INNER-FRAME, while a captioned image/formula requires
  2140. ;; both an INNER and an OUTER-FRAME.
  2141. '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
  2142. ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
  2143. ("PageImage" ("OrgPageImage" nil "page"))
  2144. ("CaptionedAs-CharImage"
  2145. ("OrgCaptionedImage"
  2146. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2147. ("OrgInlineImage" nil "as-char"))
  2148. ("CaptionedParagraphImage"
  2149. ("OrgCaptionedImage"
  2150. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2151. ("OrgImageCaptionFrame" nil "paragraph"))
  2152. ("CaptionedPageImage"
  2153. ("OrgCaptionedImage"
  2154. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2155. ("OrgPageImageCaptionFrame" nil "page"))
  2156. ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
  2157. ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
  2158. ("CaptionedDisplayFormula"
  2159. ("OrgCaptionedFormula" nil "paragraph")
  2160. ("OrgFormulaCaptionFrame" nil "paragraph"))))
  2161. (caption (car captions)) (short-caption (cdr captions))
  2162. ;; Retrieve inner and outer frame params, from configuration.
  2163. (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
  2164. (inner (nth 1 frame-cfg))
  2165. (outer (nth 2 frame-cfg))
  2166. ;; User-specified frame params (from #+ATTR_ODT spec)
  2167. (user user-frame-params)
  2168. (--merge-frame-params (function
  2169. (lambda (default user)
  2170. "Merge default and user frame params."
  2171. (if (not user) default
  2172. (cl-assert (= (length default) 3))
  2173. (cl-assert (= (length user) 3))
  2174. (cl-loop for u in user
  2175. for d in default
  2176. collect (or u d)))))))
  2177. (cond
  2178. ;; Case 1: Image/Formula has no caption.
  2179. ;; There is only one frame, one that surrounds the image
  2180. ;; or formula.
  2181. ((not caption)
  2182. ;; Merge user frame params with that from configuration.
  2183. (setq inner (funcall --merge-frame-params inner user))
  2184. (apply 'org-odt--frame href width height
  2185. (append inner title-and-desc)))
  2186. ;; Case 2: Image/Formula is captioned or labeled.
  2187. ;; There are two frames: The inner one surrounds the
  2188. ;; image or formula. The outer one contains the
  2189. ;; caption/sequence number.
  2190. (t
  2191. ;; Merge user frame params with outer frame params.
  2192. (setq outer (funcall --merge-frame-params outer user))
  2193. ;; Short caption, if specified, goes as part of inner frame.
  2194. (setq inner (let ((frame-params (copy-sequence inner)))
  2195. (setcar (cdr frame-params)
  2196. (concat
  2197. (cadr frame-params)
  2198. (when short-caption
  2199. (format " draw:name=\"%s\" " short-caption))))
  2200. frame-params))
  2201. (apply 'org-odt--textbox
  2202. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2203. "Illustration"
  2204. (concat
  2205. (apply 'org-odt--frame href width height
  2206. (append inner title-and-desc))
  2207. caption))
  2208. width height outer)))))
  2209. (defun org-odt--enumerable-p (element _info)
  2210. ;; Element should have a caption or label.
  2211. (or (org-element-property :caption element)
  2212. (org-element-property :name element)))
  2213. (defun org-odt--enumerable-image-p (element info)
  2214. (org-odt--standalone-link-p
  2215. element info
  2216. ;; Paragraph should have a caption or label. It SHOULD NOT be a
  2217. ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
  2218. ;; processing.)
  2219. (lambda (p)
  2220. (and (not (org-element-property :replaces p))
  2221. (or (org-element-property :caption p)
  2222. (org-element-property :name p))))
  2223. ;; Link should point to an image file.
  2224. (lambda (l)
  2225. (cl-assert (eq (org-element-type l) 'link))
  2226. (org-export-inline-image-p l (plist-get info :odt-inline-image-rules)))))
  2227. (defun org-odt--enumerable-latex-image-p (element info)
  2228. (org-odt--standalone-link-p
  2229. element info
  2230. ;; Paragraph should have a caption or label. It SHOULD also be a
  2231. ;; replacement element. (i.e., It SHOULD be a result of LaTeX
  2232. ;; processing.)
  2233. (lambda (p)
  2234. (and (org-element-property :replaces p)
  2235. (or (org-element-property :caption p)
  2236. (org-element-property :name p))))
  2237. ;; Link should point to an image file.
  2238. (lambda (l)
  2239. (cl-assert (eq (org-element-type l) 'link))
  2240. (org-export-inline-image-p l (plist-get info :odt-inline-image-rules)))))
  2241. (defun org-odt--enumerable-formula-p (element info)
  2242. (org-odt--standalone-link-p
  2243. element info
  2244. ;; Paragraph should have a caption or label.
  2245. (lambda (p)
  2246. (or (org-element-property :caption p)
  2247. (org-element-property :name p)))
  2248. ;; Link should point to a MathML or ODF file.
  2249. (lambda (l)
  2250. (cl-assert (eq (org-element-type l) 'link))
  2251. (org-export-inline-image-p l (plist-get info :odt-inline-formula-rules)))))
  2252. (defun org-odt--standalone-link-p (element _info &optional
  2253. paragraph-predicate
  2254. link-predicate)
  2255. "Test if ELEMENT is a standalone link for the purpose ODT export.
  2256. INFO is a plist holding contextual information.
  2257. Return non-nil, if ELEMENT is of type paragraph satisfying
  2258. PARAGRAPH-PREDICATE and its sole content, save for whitespaces,
  2259. is a link that satisfies LINK-PREDICATE.
  2260. Return non-nil, if ELEMENT is of type link satisfying
  2261. LINK-PREDICATE and its containing paragraph satisfies
  2262. PARAGRAPH-PREDICATE in addition to having no other content save for
  2263. leading and trailing whitespaces.
  2264. Return nil, otherwise."
  2265. (let ((p (cl-case (org-element-type element)
  2266. (paragraph element)
  2267. (link (and (or (not link-predicate)
  2268. (funcall link-predicate element))
  2269. (org-export-get-parent element)))
  2270. (t nil))))
  2271. (when (and p (eq (org-element-type p) 'paragraph))
  2272. (when (or (not paragraph-predicate)
  2273. (funcall paragraph-predicate p))
  2274. (let ((contents (org-element-contents p)))
  2275. (cl-loop for x in contents
  2276. with inline-image-count = 0
  2277. always (cl-case (org-element-type x)
  2278. (plain-text
  2279. (not (org-string-nw-p x)))
  2280. (link
  2281. (and (or (not link-predicate)
  2282. (funcall link-predicate x))
  2283. (= (cl-incf inline-image-count) 1)))
  2284. (t nil))))))))
  2285. (defun org-odt-link--infer-description (destination info)
  2286. ;; DESTINATION is a headline or an element (like paragraph,
  2287. ;; verse-block etc) to which a "#+NAME: label" can be attached.
  2288. ;; Note that labels that are attached to captioned entities - inline
  2289. ;; images, math formulae and tables - get resolved as part of
  2290. ;; `org-odt-format-label' and `org-odt--enumerate'.
  2291. ;; Create a cross-reference to DESTINATION but make best-efforts to
  2292. ;; create a *meaningful* description. Check item numbers, section
  2293. ;; number and section title in that order.
  2294. ;; NOTE: Counterpart of `org-export-get-ordinal'.
  2295. ;; FIXME: Handle footnote-definition footnote-reference?
  2296. (let* ((genealogy (org-element-lineage destination))
  2297. (data (reverse genealogy))
  2298. (label (let ((type (org-element-type destination)))
  2299. (if (memq type '(headline target))
  2300. (org-export-get-reference destination info)
  2301. (error "FIXME: Unable to resolve %S" destination)))))
  2302. (or
  2303. (let* ( ;; Locate top-level list.
  2304. (top-level-list
  2305. (cl-loop for x on data
  2306. when (eq (org-element-type (car x)) 'plain-list)
  2307. return x))
  2308. ;; Get list item nos.
  2309. (item-numbers
  2310. (cl-loop for (plain-list item . rest) on top-level-list by #'cddr
  2311. until (not (eq (org-element-type plain-list) 'plain-list))
  2312. collect (when (eq (org-element-property :type
  2313. plain-list)
  2314. 'ordered)
  2315. (1+ (length (org-export-get-previous-element
  2316. item info t))))))
  2317. ;; Locate top-most listified headline.
  2318. (listified-headlines
  2319. (cl-loop for x on data
  2320. when (and (eq (org-element-type (car x)) 'headline)
  2321. (org-export-low-level-p (car x) info))
  2322. return x))
  2323. ;; Get listified headline numbers.
  2324. (listified-headline-nos
  2325. (cl-loop for el in listified-headlines
  2326. when (eq (org-element-type el) 'headline)
  2327. collect (when (org-export-numbered-headline-p el info)
  2328. (1+ (length (org-export-get-previous-element
  2329. el info t)))))))
  2330. ;; Combine item numbers from both the listified headlines and
  2331. ;; regular list items.
  2332. ;; Case 1: Check if all the parents of list item are numbered.
  2333. ;; If yes, link to the item proper.
  2334. (let ((item-numbers (append listified-headline-nos item-numbers)))
  2335. (when (and item-numbers (not (memq nil item-numbers)))
  2336. (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
  2337. label
  2338. (mapconcat (lambda (n) (if (not n) " "
  2339. (concat (number-to-string n) ".")))
  2340. item-numbers "")))))
  2341. ;; Case 2: Locate a regular and numbered headline in the
  2342. ;; hierarchy. Display its section number.
  2343. (let ((headline
  2344. (and
  2345. ;; Test if destination is a numbered headline.
  2346. (org-export-numbered-headline-p destination info)
  2347. (cl-loop for el in (cons destination genealogy)
  2348. when (and (eq (org-element-type el) 'headline)
  2349. (not (org-export-low-level-p el info))
  2350. (org-export-numbered-headline-p el info))
  2351. return el))))
  2352. ;; We found one.
  2353. (when headline
  2354. (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2355. label
  2356. (mapconcat 'number-to-string (org-export-get-headline-number
  2357. headline info) "."))))
  2358. ;; Case 4: Locate a regular headline in the hierarchy. Display
  2359. ;; its title.
  2360. (let ((headline (cl-loop for el in (cons destination genealogy)
  2361. when (and (eq (org-element-type el) 'headline)
  2362. (not (org-export-low-level-p el info)))
  2363. return el)))
  2364. ;; We found one.
  2365. (when headline
  2366. (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2367. label
  2368. (let ((title (org-element-property :title headline)))
  2369. (org-export-data title info)))))
  2370. (error "FIXME?"))))
  2371. (defun org-odt-link (link desc info)
  2372. "Transcode a LINK object from Org to ODT.
  2373. DESC is the description part of the link, or the empty string.
  2374. INFO is a plist holding contextual information. See
  2375. `org-export-data'."
  2376. (let* ((type (org-element-property :type link))
  2377. (raw-path (org-element-property :path link))
  2378. ;; Ensure DESC really exists, or set it to nil.
  2379. (desc (and (not (string= desc "")) desc))
  2380. (imagep (org-export-inline-image-p
  2381. link (plist-get info :odt-inline-image-rules)))
  2382. (path (cond
  2383. ((member type '("http" "https" "ftp" "mailto"))
  2384. (concat type ":" raw-path))
  2385. ((string= type "file")
  2386. (org-export-file-uri raw-path))
  2387. (t raw-path)))
  2388. ;; Convert & to &amp; for correct XML representation
  2389. (path (replace-regexp-in-string "&" "&amp;" path)))
  2390. (cond
  2391. ;; Link type is handled by a special function.
  2392. ((org-export-custom-protocol-maybe link desc 'odt info))
  2393. ;; Image file.
  2394. ((and (not desc) imagep) (org-odt-link--inline-image link info))
  2395. ;; Formula file.
  2396. ((and (not desc)
  2397. (org-export-inline-image-p
  2398. link (plist-get info :odt-inline-formula-rules)))
  2399. (org-odt-link--inline-formula link info))
  2400. ;; Radio target: Transcode target's contents and use them as
  2401. ;; link's description.
  2402. ((string= type "radio")
  2403. (let ((destination (org-export-resolve-radio-link link info)))
  2404. (if (not destination) desc
  2405. (format
  2406. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2407. (org-export-get-reference destination info)
  2408. desc))))
  2409. ;; Links pointing to a headline: Find destination and build
  2410. ;; appropriate referencing command.
  2411. ((member type '("custom-id" "fuzzy" "id"))
  2412. (let ((destination (if (string= type "fuzzy")
  2413. (org-export-resolve-fuzzy-link link info)
  2414. (org-export-resolve-id-link link info))))
  2415. (cl-case (org-element-type destination)
  2416. ;; Fuzzy link points to a headline. If there's
  2417. ;; a description, create a hyperlink. Otherwise, try to
  2418. ;; provide a meaningful description.
  2419. (headline
  2420. (if (not desc) (org-odt-link--infer-description destination info)
  2421. (let ((label
  2422. (or (and (string= type "custom-id")
  2423. (org-element-property :CUSTOM_ID destination))
  2424. (org-export-get-reference destination info))))
  2425. (format
  2426. "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2427. label desc))))
  2428. ;; Fuzzy link points to a target. If there's a description,
  2429. ;; create a hyperlink. Otherwise, try to provide
  2430. ;; a meaningful description.
  2431. (target
  2432. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2433. (org-export-get-reference destination info)
  2434. (or desc (org-export-get-ordinal destination info))))
  2435. ;; Fuzzy link points to some element (e.g., an inline image,
  2436. ;; a math formula or a table).
  2437. (otherwise
  2438. (let ((label-reference
  2439. (ignore-errors
  2440. (org-odt-format-label destination info 'reference))))
  2441. (cond
  2442. ((not label-reference)
  2443. (org-odt-link--infer-description destination info))
  2444. ;; LINK has no description. Create
  2445. ;; a cross-reference showing entity's sequence
  2446. ;; number.
  2447. ((not desc) label-reference)
  2448. ;; LINK has description. Insert a hyperlink with
  2449. ;; user-provided description.
  2450. (t
  2451. (format
  2452. "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2453. (org-export-get-reference destination info)
  2454. desc))))))))
  2455. ;; Coderef: replace link with the reference name or the
  2456. ;; equivalent line number.
  2457. ((string= type "coderef")
  2458. (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
  2459. (href (concat "coderef-" path)))
  2460. (format
  2461. (org-export-get-coderef-format path desc)
  2462. (format
  2463. "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2464. href line-no))))
  2465. ;; External link with a description part.
  2466. ((and path desc)
  2467. (let ((link-contents (org-element-contents link)))
  2468. ;; Check if description is a link to an inline image.
  2469. (if (and (not (cdr link-contents))
  2470. (let ((desc-element (car link-contents)))
  2471. (and (eq (org-element-type desc-element) 'link)
  2472. (org-export-inline-image-p
  2473. desc-element
  2474. (plist-get info :odt-inline-image-rules)))))
  2475. ;; Format link as a clickable image.
  2476. (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  2477. path desc)
  2478. ;; Otherwise, format it as a regular link.
  2479. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2480. path desc))))
  2481. ;; External link without a description part.
  2482. (path
  2483. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2484. path path))
  2485. ;; No path, only description. Try to do something useful.
  2486. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2487. "Emphasis" desc)))))
  2488. ;;;; Node Property
  2489. (defun org-odt-node-property (node-property _contents _info)
  2490. "Transcode a NODE-PROPERTY element from Org to ODT.
  2491. CONTENTS is nil. INFO is a plist holding contextual
  2492. information."
  2493. (org-odt--encode-plain-text
  2494. (format "%s:%s"
  2495. (org-element-property :key node-property)
  2496. (let ((value (org-element-property :value node-property)))
  2497. (if value (concat " " value) "")))))
  2498. ;;;; Paragraph
  2499. (defun org-odt--paragraph-style (paragraph)
  2500. "Return style of PARAGRAPH.
  2501. Style is a symbol among `quoted', `centered' and nil."
  2502. (let ((up paragraph))
  2503. (while (and (setq up (org-element-property :parent up))
  2504. (not (memq (org-element-type up)
  2505. '(center-block quote-block section)))))
  2506. (cl-case (org-element-type up)
  2507. (center-block 'centered)
  2508. (quote-block 'quoted))))
  2509. (defun org-odt--format-paragraph (paragraph contents info default center quote)
  2510. "Format paragraph according to given styles.
  2511. PARAGRAPH is a paragraph type element. CONTENTS is the
  2512. transcoded contents of that paragraph, as a string. INFO is
  2513. a plist used as a communication channel. DEFAULT, CENTER and
  2514. QUOTE are, respectively, style to use when paragraph belongs to
  2515. no special environment, a center block, or a quote block."
  2516. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2517. (cl-case (org-odt--paragraph-style paragraph)
  2518. (quoted quote)
  2519. (centered center)
  2520. (otherwise default))
  2521. ;; If PARAGRAPH is a leading paragraph in an item that has
  2522. ;; a checkbox, splice checkbox and paragraph contents
  2523. ;; together.
  2524. (concat (let ((parent (org-element-property :parent paragraph)))
  2525. (and (eq (org-element-type parent) 'item)
  2526. (not (org-export-get-previous-element paragraph info))
  2527. (org-odt--checkbox parent)))
  2528. contents)))
  2529. (defun org-odt-paragraph (paragraph contents info)
  2530. "Transcode a PARAGRAPH element from Org to ODT.
  2531. CONTENTS is the contents of the paragraph, as a string. INFO is
  2532. the plist used as a communication channel."
  2533. (org-odt--format-paragraph
  2534. paragraph contents info
  2535. (or (org-element-property :style paragraph) "Text_20_body")
  2536. "OrgCenter"
  2537. "Quotations"))
  2538. ;;;; Plain List
  2539. (defun org-odt-plain-list (plain-list contents _info)
  2540. "Transcode a PLAIN-LIST element from Org to ODT.
  2541. CONTENTS is the contents of the list. INFO is a plist holding
  2542. contextual information."
  2543. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2544. ;; Choose style based on list type.
  2545. (cl-case (org-element-property :type plain-list)
  2546. (ordered "OrgNumberedList")
  2547. (unordered "OrgBulletedList")
  2548. (descriptive-1 "OrgDescriptionList")
  2549. (descriptive-2 "OrgDescriptionList"))
  2550. ;; If top-level list, re-start numbering. Otherwise,
  2551. ;; continue numbering.
  2552. (format "text:continue-numbering=\"%s\""
  2553. (let* ((parent (org-export-get-parent plain-list)))
  2554. (if (and parent (eq (org-element-type parent) 'item))
  2555. "true" "false")))
  2556. contents))
  2557. ;;;; Plain Text
  2558. (defun org-odt--encode-tabs-and-spaces (line)
  2559. (replace-regexp-in-string
  2560. "\\(\t\\| \\{2,\\}\\)"
  2561. (lambda (s)
  2562. (if (string= s "\t") "<text:tab/>"
  2563. (format " <text:s text:c=\"%d\"/>" (1- (length s)))))
  2564. line))
  2565. (defun org-odt--encode-plain-text (text &optional no-whitespace-filling)
  2566. (dolist (pair '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2567. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2568. (if no-whitespace-filling text
  2569. (org-odt--encode-tabs-and-spaces text)))
  2570. (defun org-odt-plain-text (text info)
  2571. "Transcode a TEXT string from Org to ODT.
  2572. TEXT is the string to transcode. INFO is a plist holding
  2573. contextual information."
  2574. (let ((output text))
  2575. ;; Protect &, < and >.
  2576. (setq output (org-odt--encode-plain-text output t))
  2577. ;; Handle smart quotes. Be sure to provide original string since
  2578. ;; OUTPUT may have been modified.
  2579. (when (plist-get info :with-smart-quotes)
  2580. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  2581. ;; Convert special strings.
  2582. (when (plist-get info :with-special-strings)
  2583. (dolist (pair org-odt-special-string-regexps)
  2584. (setq output
  2585. (replace-regexp-in-string (car pair) (cdr pair) output t nil))))
  2586. ;; Handle break preservation if required.
  2587. (when (plist-get info :preserve-breaks)
  2588. (setq output (replace-regexp-in-string
  2589. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>" output t)))
  2590. ;; Return value.
  2591. output))
  2592. ;;;; Planning
  2593. (defun org-odt-planning (planning contents info)
  2594. "Transcode a PLANNING element from Org to ODT.
  2595. CONTENTS is nil. INFO is a plist used as a communication
  2596. channel."
  2597. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2598. "OrgPlanning"
  2599. (concat
  2600. (let ((closed (org-element-property :closed planning)))
  2601. (when closed
  2602. (concat
  2603. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2604. "OrgClosedKeyword" org-closed-string)
  2605. (org-odt-timestamp closed contents info))))
  2606. (let ((deadline (org-element-property :deadline planning)))
  2607. (when deadline
  2608. (concat
  2609. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2610. "OrgDeadlineKeyword" org-deadline-string)
  2611. (org-odt-timestamp deadline contents info))))
  2612. (let ((scheduled (org-element-property :scheduled planning)))
  2613. (when scheduled
  2614. (concat
  2615. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2616. "OrgScheduledKeyword" org-scheduled-string)
  2617. (org-odt-timestamp scheduled contents info)))))))
  2618. ;;;; Property Drawer
  2619. (defun org-odt-property-drawer (_property-drawer contents _info)
  2620. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2621. CONTENTS holds the contents of the drawer. INFO is a plist
  2622. holding contextual information."
  2623. (and (org-string-nw-p contents)
  2624. (format "<text:p text:style-name=\"OrgFixedWidthBlock\">%s</text:p>"
  2625. contents)))
  2626. ;;;; Quote Block
  2627. (defun org-odt-quote-block (_quote-block contents _info)
  2628. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2629. CONTENTS holds the contents of the block. INFO is a plist
  2630. holding contextual information."
  2631. contents)
  2632. ;;;; Section
  2633. (defun org-odt-format-section (text style &optional name)
  2634. (let ((default-name (car (org-odt-add-automatic-style "Section"))))
  2635. (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
  2636. style
  2637. (format "text:name=\"%s\"" (or name default-name))
  2638. text)))
  2639. (defun org-odt-section (_section contents _info) ; FIXME
  2640. "Transcode a SECTION element from Org to ODT.
  2641. CONTENTS holds the contents of the section. INFO is a plist
  2642. holding contextual information."
  2643. contents)
  2644. ;;;; Radio Target
  2645. (defun org-odt-radio-target (radio-target text info)
  2646. "Transcode a RADIO-TARGET object from Org to ODT.
  2647. TEXT is the text of the target. INFO is a plist holding
  2648. contextual information."
  2649. (org-odt--target text (org-export-get-reference radio-target info)))
  2650. ;;;; Special Block
  2651. (defun org-odt-special-block (special-block contents info)
  2652. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2653. CONTENTS holds the contents of the block. INFO is a plist
  2654. holding contextual information."
  2655. (let ((type (org-element-property :type special-block))
  2656. (attributes (org-export-read-attribute :attr_odt special-block)))
  2657. (cond
  2658. ;; Annotation.
  2659. ((string= type "annotation")
  2660. (let* ((author (or (plist-get attributes :author)
  2661. (let ((author (plist-get info :author)))
  2662. (and author (org-export-data author info)))))
  2663. (date (or (plist-get attributes :date)
  2664. ;; FIXME: Is `car' right thing to do below?
  2665. (car (plist-get info :date)))))
  2666. (format "\n<text:p>%s</text:p>"
  2667. (format "<office:annotation>\n%s\n</office:annotation>"
  2668. (concat
  2669. (and author
  2670. (format "<dc:creator>%s</dc:creator>" author))
  2671. (and date
  2672. (format "<dc:date>%s</dc:date>"
  2673. (org-odt--format-timestamp date nil 'iso-date)))
  2674. contents)))))
  2675. ;; Textbox.
  2676. ((string= type "textbox")
  2677. (let ((width (plist-get attributes :width))
  2678. (height (plist-get attributes :height))
  2679. (style (plist-get attributes :style))
  2680. (extra (plist-get attributes :extra))
  2681. (anchor (plist-get attributes :anchor)))
  2682. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2683. "Text_20_body" (org-odt--textbox contents width height
  2684. style extra anchor))))
  2685. (t contents))))
  2686. ;;;; Src Block
  2687. (defun org-odt-hfy-face-to-css (fn)
  2688. "Create custom style for face FN.
  2689. When FN is the default face, use its foreground and background
  2690. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2691. use its color attribute to create a character style whose name
  2692. is obtained from FN. Currently all attributes of FN other than
  2693. color are ignored.
  2694. The style name for a face FN is derived using the following
  2695. operations on the face name in that order - de-dash, CamelCase
  2696. and prefix with \"OrgSrc\". For example,
  2697. `font-lock-function-name-face' is associated with
  2698. \"OrgSrcFontLockFunctionNameFace\"."
  2699. (let* ((css-list (hfy-face-to-style fn))
  2700. (style-name (concat "OrgSrc"
  2701. (mapconcat
  2702. 'capitalize (split-string
  2703. (hfy-face-or-def-to-name fn) "-")
  2704. "")))
  2705. (color-val (cdr (assoc "color" css-list)))
  2706. (background-color-val (cdr (assoc "background" css-list)))
  2707. (style (and org-odt-create-custom-styles-for-srcblocks
  2708. (cond
  2709. ((eq fn 'default)
  2710. (format org-odt-src-block-paragraph-format
  2711. background-color-val color-val))
  2712. (t
  2713. (format
  2714. "
  2715. <style:style style:name=\"%s\" style:family=\"text\">
  2716. <style:text-properties fo:color=\"%s\"/>
  2717. </style:style>" style-name color-val))))))
  2718. (cons style-name style)))
  2719. (defun org-odt-htmlfontify-string (line)
  2720. (let* ((hfy-html-quote-regex "\\([<\"&> \t]\\)")
  2721. (hfy-html-quote-map '(("\"" "&quot;")
  2722. ("<" "&lt;")
  2723. ("&" "&amp;")
  2724. (">" "&gt;")
  2725. (" " "<text:s/>")
  2726. ("\t" "<text:tab/>")))
  2727. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  2728. (hfy-optimizations-1 (copy-sequence hfy-optimizations))
  2729. (hfy-optimizations (cl-pushnew 'body-text-only hfy-optimizations-1))
  2730. (hfy-begin-span-handler
  2731. (lambda (style _text-block _text-id _text-begins-block-p)
  2732. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2733. (hfy-end-span-handler (lambda () (insert "</text:span>"))))
  2734. (with-no-warnings (htmlfontify-string line))))
  2735. (defun org-odt-do-format-code
  2736. (code info &optional lang refs retain-labels num-start)
  2737. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2738. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2739. (code-lines (org-split-string code "\n"))
  2740. (code-length (length code-lines))
  2741. (use-htmlfontify-p (and (functionp lang-mode)
  2742. (plist-get info :odt-fontify-srcblocks)
  2743. (require 'htmlfontify nil t)
  2744. (fboundp 'htmlfontify-string)))
  2745. (code (if (not use-htmlfontify-p) code
  2746. (with-temp-buffer
  2747. (insert code)
  2748. (funcall lang-mode)
  2749. (org-font-lock-ensure)
  2750. (buffer-string))))
  2751. (fontifier (if use-htmlfontify-p 'org-odt-htmlfontify-string
  2752. 'org-odt--encode-plain-text))
  2753. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2754. "OrgFixedWidthBlock"))
  2755. (i 0))
  2756. (cl-assert (= code-length (length (org-split-string code "\n"))))
  2757. (setq code
  2758. (org-export-format-code
  2759. code
  2760. (lambda (loc line-num ref)
  2761. (setq par-style
  2762. (concat par-style (and (= (cl-incf i) code-length)
  2763. "LastLine")))
  2764. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2765. (setq loc (funcall fontifier loc))
  2766. (when ref
  2767. (setq loc (org-odt--target loc (concat "coderef-" ref))))
  2768. (cl-assert par-style)
  2769. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2770. par-style loc))
  2771. (if (not line-num) loc
  2772. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2773. num-start refs))
  2774. (cond
  2775. ((not num-start) code)
  2776. ((= num-start 0)
  2777. (format
  2778. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2779. " text:continue-numbering=\"false\"" code))
  2780. (t
  2781. (format
  2782. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2783. " text:continue-numbering=\"true\"" code)))))
  2784. (defun org-odt-format-code (element info)
  2785. (let* ((lang (org-element-property :language element))
  2786. ;; Extract code and references.
  2787. (code-info (org-export-unravel-code element))
  2788. (code (car code-info))
  2789. (refs (cdr code-info))
  2790. ;; Does the source block contain labels?
  2791. (retain-labels (org-element-property :retain-labels element))
  2792. ;; Does it have line numbers?
  2793. (num-start (org-export-get-loc element info)))
  2794. (org-odt-do-format-code code info lang refs retain-labels num-start)))
  2795. (defun org-odt-src-block (src-block _contents info)
  2796. "Transcode a SRC-BLOCK element from Org to ODT.
  2797. CONTENTS holds the contents of the item. INFO is a plist holding
  2798. contextual information."
  2799. (let* ((attributes (org-export-read-attribute :attr_odt src-block))
  2800. (caption (car (org-odt-format-label src-block info 'definition))))
  2801. (concat
  2802. (and caption
  2803. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2804. "Listing" caption))
  2805. (let ((--src-block (org-odt-format-code src-block info)))
  2806. (if (not (plist-get attributes :textbox)) --src-block
  2807. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2808. "Text_20_body"
  2809. (org-odt--textbox --src-block nil nil nil)))))))
  2810. ;;;; Statistics Cookie
  2811. (defun org-odt-statistics-cookie (statistics-cookie _contents _info)
  2812. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2813. CONTENTS is nil. INFO is a plist holding contextual information."
  2814. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2815. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2816. "OrgCode" cookie-value)))
  2817. ;;;; Strike-Through
  2818. (defun org-odt-strike-through (_strike-through contents _info)
  2819. "Transcode STRIKE-THROUGH from Org to ODT.
  2820. CONTENTS is the text with strike-through markup. INFO is a plist
  2821. holding contextual information."
  2822. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2823. "Strikethrough" contents))
  2824. ;;;; Subscript
  2825. (defun org-odt-subscript (_subscript contents _info)
  2826. "Transcode a SUBSCRIPT object from Org to ODT.
  2827. CONTENTS is the contents of the object. INFO is a plist holding
  2828. contextual information."
  2829. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2830. "OrgSubscript" contents))
  2831. ;;;; Superscript
  2832. (defun org-odt-superscript (_superscript contents _info)
  2833. "Transcode a SUPERSCRIPT object from Org to ODT.
  2834. CONTENTS is the contents of the object. INFO is a plist holding
  2835. contextual information."
  2836. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2837. "OrgSuperscript" contents))
  2838. ;;;; Table Cell
  2839. (defun org-odt-table-style-spec (element info)
  2840. (let* ((table (org-export-get-parent-table element))
  2841. (table-attributes (org-export-read-attribute :attr_odt table))
  2842. (table-style (plist-get table-attributes :style)))
  2843. (assoc table-style (plist-get info :odt-table-styles))))
  2844. (defun org-odt-get-table-cell-styles (table-cell info)
  2845. "Retrieve styles applicable to a table cell.
  2846. R and C are (zero-based) row and column numbers of the table
  2847. cell. STYLE-SPEC is an entry in `org-odt-table-styles'
  2848. applicable to the current table. It is nil if the table is not
  2849. associated with any style attributes.
  2850. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2851. When STYLE-SPEC is nil, style the table cell the conventional way
  2852. - choose cell borders based on row and column groupings and
  2853. choose paragraph alignment based on `org-col-cookies' text
  2854. property. See also
  2855. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  2856. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2857. styles congruent with the ODF-1.2 specification."
  2858. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2859. (r (car table-cell-address)) (c (cdr table-cell-address))
  2860. (style-spec (org-odt-table-style-spec table-cell info))
  2861. (table-dimensions (org-export-table-dimensions
  2862. (org-export-get-parent-table table-cell)
  2863. info)))
  2864. (when style-spec
  2865. ;; LibreOffice - particularly the Writer - honors neither table
  2866. ;; templates nor custom table-cell styles. Inorder to retain
  2867. ;; interoperability with LibreOffice, only automatic styles are
  2868. ;; used for styling of table-cells. The current implementation is
  2869. ;; congruent with ODF-1.2 specification and hence is
  2870. ;; future-compatible.
  2871. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2872. ;; which recognizes as many as 16 different cell types - is much
  2873. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2874. ;; by hand.
  2875. (let* ((template-name (nth 1 style-spec))
  2876. (cell-style-selectors (nth 2 style-spec))
  2877. (cell-type
  2878. (cond
  2879. ((and (cdr (assq 'use-first-column-styles cell-style-selectors))
  2880. (= c 0)) "FirstColumn")
  2881. ((and (cdr (assq 'use-last-column-styles cell-style-selectors))
  2882. (= (1+ c) (cdr table-dimensions)))
  2883. "LastColumn")
  2884. ((and (cdr (assq 'use-first-row-styles cell-style-selectors))
  2885. (= r 0)) "FirstRow")
  2886. ((and (cdr (assq 'use-last-row-styles cell-style-selectors))
  2887. (= (1+ r) (car table-dimensions)))
  2888. "LastRow")
  2889. ((and (cdr (assq 'use-banding-rows-styles cell-style-selectors))
  2890. (= (% r 2) 1)) "EvenRow")
  2891. ((and (cdr (assq 'use-banding-rows-styles cell-style-selectors))
  2892. (= (% r 2) 0)) "OddRow")
  2893. ((and (cdr (assq 'use-banding-columns-styles cell-style-selectors))
  2894. (= (% c 2) 1)) "EvenColumn")
  2895. ((and (cdr (assq 'use-banding-columns-styles cell-style-selectors))
  2896. (= (% c 2) 0)) "OddColumn")
  2897. (t ""))))
  2898. (concat template-name cell-type)))))
  2899. (defun org-odt-table-cell (table-cell contents info)
  2900. "Transcode a TABLE-CELL element from Org to ODT.
  2901. CONTENTS is nil. INFO is a plist used as a communication
  2902. channel."
  2903. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2904. (r (car table-cell-address))
  2905. (c (cdr table-cell-address))
  2906. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2907. (table-row (org-export-get-parent table-cell))
  2908. (custom-style-prefix (org-odt-get-table-cell-styles
  2909. table-cell info))
  2910. (paragraph-style
  2911. (or
  2912. (and custom-style-prefix
  2913. (format "%sTableParagraph" custom-style-prefix))
  2914. (concat
  2915. (cond
  2916. ((and (= 1 (org-export-table-row-group table-row info))
  2917. (org-export-table-has-header-p
  2918. (org-export-get-parent-table table-row) info))
  2919. "OrgTableHeading")
  2920. ((let* ((table (org-export-get-parent-table table-cell))
  2921. (table-attrs (org-export-read-attribute :attr_odt table))
  2922. (table-header-columns
  2923. (let ((cols (plist-get table-attrs :header-columns)))
  2924. (and cols (read cols)))))
  2925. (<= c (cond ((wholenump table-header-columns)
  2926. (- table-header-columns 1))
  2927. (table-header-columns 0)
  2928. (t -1))))
  2929. "OrgTableHeading")
  2930. (t "OrgTableContents"))
  2931. (capitalize (symbol-name (org-export-table-cell-alignment
  2932. table-cell info))))))
  2933. (cell-style-name
  2934. (or
  2935. (and custom-style-prefix (format "%sTableCell"
  2936. custom-style-prefix))
  2937. (concat
  2938. "OrgTblCell"
  2939. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2940. (zerop r)) "T")
  2941. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2942. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2943. (not (zerop c)) ) "L"))))
  2944. (cell-attributes
  2945. (concat
  2946. (format " table:style-name=\"%s\"" cell-style-name)
  2947. (and (> horiz-span 0)
  2948. (format " table:number-columns-spanned=\"%d\""
  2949. (1+ horiz-span))))))
  2950. (unless contents (setq contents ""))
  2951. (concat
  2952. (cl-assert paragraph-style)
  2953. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2954. cell-attributes
  2955. (let ((table-cell-contents (org-element-contents table-cell)))
  2956. (if (eq (org-element-class (car table-cell-contents)) 'element)
  2957. contents
  2958. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2959. paragraph-style contents))))
  2960. (let (s)
  2961. (dotimes (_ horiz-span s)
  2962. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2963. "\n")))
  2964. ;;;; Table Row
  2965. (defun org-odt-table-row (table-row contents info)
  2966. "Transcode a TABLE-ROW element from Org to ODT.
  2967. CONTENTS is the contents of the row. INFO is a plist used as a
  2968. communication channel."
  2969. ;; Rules are ignored since table separators are deduced from
  2970. ;; borders of the current row.
  2971. (when (eq (org-element-property :type table-row) 'standard)
  2972. (let* ((rowgroup-tags
  2973. (if (and (= 1 (org-export-table-row-group table-row info))
  2974. (org-export-table-has-header-p
  2975. (org-export-get-parent-table table-row) info))
  2976. ;; If the row belongs to the first rowgroup and the
  2977. ;; table has more than one row groups, then this row
  2978. ;; belongs to the header row group.
  2979. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2980. ;; Otherwise, it belongs to non-header row group.
  2981. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2982. (concat
  2983. ;; Does this row begin a rowgroup?
  2984. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2985. (car rowgroup-tags))
  2986. ;; Actual table row
  2987. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2988. ;; Does this row end a rowgroup?
  2989. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2990. (cdr rowgroup-tags))))))
  2991. ;;;; Table
  2992. (defun org-odt-table-first-row-data-cells (table info)
  2993. (let ((table-row
  2994. (org-element-map table 'table-row
  2995. (lambda (row)
  2996. (unless (eq (org-element-property :type row) 'rule) row))
  2997. info 'first-match))
  2998. (special-column-p (org-export-table-has-special-column-p table)))
  2999. (if (not special-column-p) (org-element-contents table-row)
  3000. (cdr (org-element-contents table-row)))))
  3001. (defun org-odt--table (table contents info)
  3002. "Transcode a TABLE element from Org to ODT.
  3003. CONTENTS is the contents of the table. INFO is a plist holding
  3004. contextual information."
  3005. (cl-case (org-element-property :type table)
  3006. ;; Case 1: table.el doesn't support export to OD format. Strip
  3007. ;; such tables from export.
  3008. (table.el
  3009. (prog1 nil
  3010. (message
  3011. (concat
  3012. "(ox-odt): Found table.el-type table in the source Org file."
  3013. " table.el doesn't support export to ODT format."
  3014. " Stripping the table from export."))))
  3015. ;; Case 2: Native Org tables.
  3016. (otherwise
  3017. (let* ((captions (org-odt-format-label table info 'definition))
  3018. (caption (car captions)) (short-caption (cdr captions))
  3019. (attributes (org-export-read-attribute :attr_odt table))
  3020. (custom-table-style (nth 1 (org-odt-table-style-spec table info)))
  3021. (table-column-specs
  3022. (lambda (table info)
  3023. (let* ((table-style (or custom-table-style "OrgTable"))
  3024. (column-style (format "%sColumn" table-style)))
  3025. (mapconcat
  3026. (lambda (table-cell)
  3027. (let ((width (1+ (or (org-export-table-cell-width
  3028. table-cell info) 0)))
  3029. (s (format
  3030. "\n<table:table-column table:style-name=\"%s\"/>"
  3031. column-style))
  3032. out)
  3033. (dotimes (_ width out) (setq out (concat s out)))))
  3034. (org-odt-table-first-row-data-cells table info) "\n")))))
  3035. (concat
  3036. ;; caption.
  3037. (when caption
  3038. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3039. "Table" caption))
  3040. ;; begin table.
  3041. (let* ((automatic-name
  3042. (org-odt-add-automatic-style "Table" attributes)))
  3043. (format
  3044. "\n<table:table table:style-name=\"%s\"%s>"
  3045. (or custom-table-style (cdr automatic-name) "OrgTable")
  3046. (concat (when short-caption
  3047. (format " table:name=\"%s\"" short-caption)))))
  3048. ;; column specification.
  3049. (funcall table-column-specs table info)
  3050. ;; actual contents.
  3051. "\n" contents
  3052. ;; end table.
  3053. "</table:table>")))))
  3054. (defun org-odt-table (table contents info)
  3055. "Transcode a TABLE element from Org to ODT.
  3056. CONTENTS is the contents of the table. INFO is a plist holding
  3057. contextual information.
  3058. Use `org-odt--table' to typeset the table. Handle details
  3059. pertaining to indentation here."
  3060. (let* ((--element-preceded-by-table-p
  3061. (lambda (element info)
  3062. (cl-loop for el in (org-export-get-previous-element element info t)
  3063. thereis (eq (org-element-type el) 'table))))
  3064. (--walk-list-genealogy-and-collect-tags
  3065. (lambda (table info)
  3066. (let* ((genealogy (org-element-lineage table))
  3067. (list-genealogy
  3068. (when (eq (org-element-type (car genealogy)) 'item)
  3069. (cl-loop for el in genealogy
  3070. when (memq (org-element-type el)
  3071. '(item plain-list))
  3072. collect el)))
  3073. (llh-genealogy
  3074. (apply #'nconc
  3075. (cl-loop
  3076. for el in genealogy
  3077. when (and (eq (org-element-type el) 'headline)
  3078. (org-export-low-level-p el info))
  3079. collect
  3080. (list el
  3081. (assq 'headline
  3082. (org-element-contents
  3083. (org-export-get-parent el)))))))
  3084. parent-list)
  3085. (nconc
  3086. ;; Handle list genealogy.
  3087. (cl-loop
  3088. for el in list-genealogy collect
  3089. (cl-case (org-element-type el)
  3090. (plain-list
  3091. (setq parent-list el)
  3092. (cons "</text:list>"
  3093. (format "\n<text:list text:style-name=\"%s\" %s>"
  3094. (cl-case (org-element-property :type el)
  3095. (ordered "OrgNumberedList")
  3096. (unordered "OrgBulletedList")
  3097. (descriptive-1 "OrgDescriptionList")
  3098. (descriptive-2 "OrgDescriptionList"))
  3099. "text:continue-numbering=\"true\"")))
  3100. (item
  3101. (cond
  3102. ((not parent-list)
  3103. (if (funcall --element-preceded-by-table-p table info)
  3104. '("</text:list-header>" . "<text:list-header>")
  3105. '("</text:list-item>" . "<text:list-header>")))
  3106. ((funcall --element-preceded-by-table-p
  3107. parent-list info)
  3108. '("</text:list-header>" . "<text:list-header>"))
  3109. (t '("</text:list-item>" . "<text:list-item>"))))))
  3110. ;; Handle low-level headlines.
  3111. (cl-loop for el in llh-genealogy
  3112. with step = 'item collect
  3113. (cl-case step
  3114. (plain-list
  3115. (setq step 'item) ; Flip-flop
  3116. (setq parent-list el)
  3117. (cons "</text:list>"
  3118. (format "\n<text:list text:style-name=\"%s\" %s>"
  3119. (if (org-export-numbered-headline-p
  3120. el info)
  3121. "OrgNumberedList"
  3122. "OrgBulletedList")
  3123. "text:continue-numbering=\"true\"")))
  3124. (item
  3125. (setq step 'plain-list) ; Flip-flop
  3126. (cond
  3127. ((not parent-list)
  3128. (if (funcall --element-preceded-by-table-p table info)
  3129. '("</text:list-header>" . "<text:list-header>")
  3130. '("</text:list-item>" . "<text:list-header>")))
  3131. ((let ((section? (org-export-get-previous-element
  3132. parent-list info)))
  3133. (and section?
  3134. (eq (org-element-type section?) 'section)
  3135. (assq 'table (org-element-contents section?))))
  3136. '("</text:list-header>" . "<text:list-header>"))
  3137. (t
  3138. '("</text:list-item>" . "<text:list-item>"))))))))))
  3139. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  3140. table info)))
  3141. ;; OpenDocument schema does not permit table to occur within a
  3142. ;; list item.
  3143. ;; One solution - the easiest and lightweight, in terms of
  3144. ;; implementation - is to put the table in an indented text box
  3145. ;; and make the text box part of the list-item. Unfortunately if
  3146. ;; the table is big and spans multiple pages, the text box could
  3147. ;; overflow. In this case, the following attribute will come
  3148. ;; handy.
  3149. ;; ,---- From OpenDocument-v1.1.pdf
  3150. ;; | 15.27.28 Overflow behavior
  3151. ;; |
  3152. ;; | For text boxes contained within text document, the
  3153. ;; | style:overflow-behavior property specifies the behavior of text
  3154. ;; | boxes where the containing text does not fit into the text
  3155. ;; | box.
  3156. ;; |
  3157. ;; | If the attribute's value is clip, the text that does not fit
  3158. ;; | into the text box is not displayed.
  3159. ;; |
  3160. ;; | If the attribute value is auto-create-new-frame, a new frame
  3161. ;; | will be created on the next page, with the same position and
  3162. ;; | dimensions of the original frame.
  3163. ;; |
  3164. ;; | If the style:overflow-behavior property's value is
  3165. ;; | auto-create-new-frame and the text box has a minimum width or
  3166. ;; | height specified, then the text box will grow until the page
  3167. ;; | bounds are reached before a new frame is created.
  3168. ;; `----
  3169. ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
  3170. ;; auto-create-new-frame property and always resorts to clipping
  3171. ;; the text box. This results in table being truncated.
  3172. ;; So we solve the problem the hard (and fun) way using list
  3173. ;; continuations.
  3174. ;; The problem only becomes more interesting if you take in to
  3175. ;; account the following facts:
  3176. ;;
  3177. ;; - Description lists are simulated as plain lists.
  3178. ;; - Low-level headlines can be listified.
  3179. ;; - In Org mode, a table can occur not only as a regular list
  3180. ;; item, but also within description lists and low-level
  3181. ;; headlines.
  3182. ;; See `org-odt-translate-description-lists' and
  3183. ;; `org-odt-translate-low-level-headlines' for how this is
  3184. ;; tackled.
  3185. (concat "\n"
  3186. ;; Discontinue the list.
  3187. (mapconcat 'car close-open-tags "\n")
  3188. ;; Put the table in an indented section.
  3189. (let* ((table (org-odt--table table contents info))
  3190. (level (/ (length (mapcar 'car close-open-tags)) 2))
  3191. (style (format "OrgIndentedSection-Level-%d" level)))
  3192. (when table (org-odt-format-section table style)))
  3193. ;; Continue the list.
  3194. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  3195. ;;;; Target
  3196. (defun org-odt-target (target _contents info)
  3197. "Transcode a TARGET object from Org to ODT.
  3198. CONTENTS is nil. INFO is a plist holding contextual
  3199. information."
  3200. (org-odt--target "" (org-export-get-reference target info)))
  3201. ;;;; Timestamp
  3202. (defun org-odt-timestamp (timestamp _contents info)
  3203. "Transcode a TIMESTAMP object from Org to ODT.
  3204. CONTENTS is nil. INFO is a plist used as a communication
  3205. channel."
  3206. (let ((type (org-element-property :type timestamp)))
  3207. (if (not (plist-get info :odt-use-date-fields))
  3208. (let ((value (org-odt-plain-text
  3209. (org-timestamp-translate timestamp) info)))
  3210. (cl-case (org-element-property :type timestamp)
  3211. ((active active-range)
  3212. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3213. "OrgActiveTimestamp" value))
  3214. ((inactive inactive-range)
  3215. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3216. "OrgInactiveTimestamp" value))
  3217. (otherwise value)))
  3218. (cl-case type
  3219. (active
  3220. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3221. "OrgActiveTimestamp"
  3222. (format "&lt;%s&gt;" (org-odt--format-timestamp timestamp))))
  3223. (inactive
  3224. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3225. "OrgInactiveTimestamp"
  3226. (format "[%s]" (org-odt--format-timestamp timestamp))))
  3227. (active-range
  3228. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3229. "OrgActiveTimestamp"
  3230. (format "&lt;%s&gt;&#x2013;&lt;%s&gt;"
  3231. (org-odt--format-timestamp timestamp)
  3232. (org-odt--format-timestamp timestamp 'end))))
  3233. (inactive-range
  3234. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3235. "OrgInactiveTimestamp"
  3236. (format "[%s]&#x2013;[%s]"
  3237. (org-odt--format-timestamp timestamp)
  3238. (org-odt--format-timestamp timestamp 'end))))
  3239. (otherwise
  3240. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3241. "OrgDiaryTimestamp"
  3242. (org-odt-plain-text (org-timestamp-translate timestamp)
  3243. info)))))))
  3244. ;;;; Underline
  3245. (defun org-odt-underline (_underline contents _info)
  3246. "Transcode UNDERLINE from Org to ODT.
  3247. CONTENTS is the text with underline markup. INFO is a plist
  3248. holding contextual information."
  3249. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3250. "Underline" contents))
  3251. ;;;; Verbatim
  3252. (defun org-odt-verbatim (verbatim _contents _info)
  3253. "Transcode a VERBATIM object from Org to ODT.
  3254. CONTENTS is nil. INFO is a plist used as a communication
  3255. channel."
  3256. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3257. "OrgCode" (org-odt--encode-plain-text
  3258. (org-element-property :value verbatim))))
  3259. ;;;; Verse Block
  3260. (defun org-odt-verse-block (_verse-block contents _info)
  3261. "Transcode a VERSE-BLOCK element from Org to ODT.
  3262. CONTENTS is verse block contents. INFO is a plist holding
  3263. contextual information."
  3264. (format "\n<text:p text:style-name=\"OrgVerse\">%s</text:p>"
  3265. (replace-regexp-in-string
  3266. ;; Replace leading tabs and spaces.
  3267. "^[ \t]+" #'org-odt--encode-tabs-and-spaces
  3268. ;; Add line breaks to each line of verse.
  3269. (replace-regexp-in-string
  3270. "\\(<text:line-break/>\\)?[ \t]*$" "<text:line-break/>" contents))))
  3271. ;;; Filters
  3272. ;;; Images
  3273. (defun org-odt--translate-image-links (data _backend info)
  3274. (org-export-insert-image-links data info org-odt-inline-image-rules))
  3275. ;;;; LaTeX fragments
  3276. (defun org-odt--translate-latex-fragments (tree _backend info)
  3277. (let ((processing-type (plist-get info :with-latex))
  3278. (count 0))
  3279. ;; Normalize processing-type to one of dvipng, mathml or verbatim.
  3280. ;; If the desired converter is not available, force verbatim
  3281. ;; processing.
  3282. (cl-case processing-type
  3283. ((t mathml)
  3284. (if (and (fboundp 'org-format-latex-mathml-available-p)
  3285. (org-format-latex-mathml-available-p))
  3286. (setq processing-type 'mathml)
  3287. (message "LaTeX to MathML converter not available.")
  3288. (setq processing-type 'verbatim)))
  3289. ((dvipng imagemagick)
  3290. (unless (and (org-check-external-command "latex" "" t)
  3291. (org-check-external-command
  3292. (if (eq processing-type 'dvipng) "dvipng" "convert") "" t))
  3293. (message "LaTeX to PNG converter not available.")
  3294. (setq processing-type 'verbatim)))
  3295. (otherwise
  3296. (message "Unknown LaTeX option. Forcing verbatim.")
  3297. (setq processing-type 'verbatim)))
  3298. ;; Store normalized value for later use.
  3299. (when (plist-get info :with-latex)
  3300. (plist-put info :with-latex processing-type))
  3301. (message "Formatting LaTeX using %s" processing-type)
  3302. ;; Convert `latex-fragment's and `latex-environment's.
  3303. (when (memq processing-type '(mathml dvipng imagemagick))
  3304. (org-element-map tree '(latex-fragment latex-environment)
  3305. (lambda (latex-*)
  3306. (cl-incf count)
  3307. (let* ((latex-frag (org-element-property :value latex-*))
  3308. (input-file (plist-get info :input-file))
  3309. (cache-dir (file-name-directory input-file))
  3310. (cache-subdir (concat
  3311. (cl-case processing-type
  3312. ((dvipng imagemagick)
  3313. org-preview-latex-image-directory)
  3314. (mathml "ltxmathml/"))
  3315. (file-name-sans-extension
  3316. (file-name-nondirectory input-file))))
  3317. (display-msg
  3318. (cl-case processing-type
  3319. ((dvipng imagemagick)
  3320. (format "Creating LaTeX Image %d..." count))
  3321. (mathml (format "Creating MathML snippet %d..." count))))
  3322. ;; Get an Org-style link to PNG image or the MathML
  3323. ;; file.
  3324. (link
  3325. (with-temp-buffer
  3326. (insert latex-frag)
  3327. ;; When converting to a PNG image, make sure to
  3328. ;; copy all LaTeX header specifications from the
  3329. ;; Org source.
  3330. (unless (eq processing-type 'mathml)
  3331. (let ((h (plist-get info :latex-header)))
  3332. (when h
  3333. (insert "\n"
  3334. (replace-regexp-in-string
  3335. "^" "#+LATEX_HEADER: " h)))))
  3336. (org-format-latex cache-subdir nil nil cache-dir
  3337. nil display-msg nil
  3338. processing-type)
  3339. (goto-char (point-min))
  3340. (skip-chars-forward " \t\n")
  3341. (org-element-link-parser))))
  3342. (if (not (eq 'link (org-element-type link)))
  3343. (message "LaTeX Conversion failed.")
  3344. ;; Conversion succeeded. Parse above Org-style link to
  3345. ;; a `link' object.
  3346. (let ((replacement
  3347. (cl-case (org-element-type latex-*)
  3348. ;;LaTeX environment. Mimic a "standalone image
  3349. ;; or formula" by enclosing the `link' in
  3350. ;; a `paragraph'. Copy over original
  3351. ;; attributes, captions to the enclosing
  3352. ;; paragraph.
  3353. (latex-environment
  3354. (org-element-adopt-elements
  3355. (list 'paragraph
  3356. (list :style "OrgFormula"
  3357. :name
  3358. (org-element-property :name latex-*)
  3359. :caption
  3360. (org-element-property :caption latex-*)))
  3361. link))
  3362. ;; LaTeX fragment. No special action.
  3363. (latex-fragment link))))
  3364. ;; Note down the object that link replaces.
  3365. (org-element-put-property replacement :replaces
  3366. (list (org-element-type latex-*)
  3367. (list :value latex-frag)))
  3368. ;; Restore blank after initial element or object.
  3369. (org-element-put-property
  3370. replacement :post-blank
  3371. (org-element-property :post-blank latex-*))
  3372. ;; Replace now.
  3373. (org-element-set-element latex-* replacement)))))
  3374. info nil nil t)))
  3375. tree)
  3376. ;;;; Description lists
  3377. ;; This translator is necessary to handle indented tables in a uniform
  3378. ;; manner. See comment in `org-odt--table'.
  3379. (defun org-odt--translate-description-lists (tree _backend info)
  3380. ;; OpenDocument has no notion of a description list. So simulate it
  3381. ;; using plain lists. Description lists in the exported document
  3382. ;; are typeset in the same manner as they are in a typical HTML
  3383. ;; document.
  3384. ;;
  3385. ;; Specifically, a description list like this:
  3386. ;;
  3387. ;; ,----
  3388. ;; | - term-1 :: definition-1
  3389. ;; | - term-2 :: definition-2
  3390. ;; `----
  3391. ;;
  3392. ;; gets translated in to the following form:
  3393. ;;
  3394. ;; ,----
  3395. ;; | - term-1
  3396. ;; | - definition-1
  3397. ;; | - term-2
  3398. ;; | - definition-2
  3399. ;; `----
  3400. ;;
  3401. ;; Further effect is achieved by fixing the OD styles as below:
  3402. ;;
  3403. ;; 1. Set the :type property of the simulated lists to
  3404. ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
  3405. ;; that has *no* bullets whatsoever.
  3406. ;;
  3407. ;; 2. The paragraph containing the definition term is styled to be
  3408. ;; in bold.
  3409. ;;
  3410. (org-element-map tree 'plain-list
  3411. (lambda (el)
  3412. (when (eq (org-element-property :type el) 'descriptive)
  3413. (org-element-set-element
  3414. el
  3415. (apply 'org-element-adopt-elements
  3416. (list 'plain-list (list :type 'descriptive-1))
  3417. (mapcar
  3418. (lambda (item)
  3419. (org-element-adopt-elements
  3420. (list 'item (list :checkbox (org-element-property
  3421. :checkbox item)))
  3422. (list 'paragraph (list :style "Text_20_body_20_bold")
  3423. (or (org-element-property :tag item) "(no term)"))
  3424. (org-element-adopt-elements
  3425. (list 'plain-list (list :type 'descriptive-2))
  3426. (apply 'org-element-adopt-elements
  3427. (list 'item nil)
  3428. (org-element-contents item)))))
  3429. (org-element-contents el)))))
  3430. nil)
  3431. info)
  3432. tree)
  3433. ;;;; List tables
  3434. ;; Lists that are marked with attribute `:list-table' are called as
  3435. ;; list tables. They will be rendered as a table within the exported
  3436. ;; document.
  3437. ;; Consider an example. The following list table
  3438. ;;
  3439. ;; #+attr_odt :list-table t
  3440. ;; - Row 1
  3441. ;; - 1.1
  3442. ;; - 1.2
  3443. ;; - 1.3
  3444. ;; - Row 2
  3445. ;; - 2.1
  3446. ;; - 2.2
  3447. ;; - 2.3
  3448. ;;
  3449. ;; will be exported as though it were an Org table like the one show
  3450. ;; below.
  3451. ;;
  3452. ;; | Row 1 | 1.1 | 1.2 | 1.3 |
  3453. ;; | Row 2 | 2.1 | 2.2 | 2.3 |
  3454. ;;
  3455. ;; Note that org-tables are NOT multi-line and each line is mapped to
  3456. ;; a unique row in the exported document. So if an exported table
  3457. ;; needs to contain a single paragraph (with copious text) it needs to
  3458. ;; be typed up in a single line. Editing such long lines using the
  3459. ;; table editor will be a cumbersome task. Furthermore inclusion of
  3460. ;; multi-paragraph text in a table cell is well-nigh impossible.
  3461. ;;
  3462. ;; A LIST-TABLE circumvents above problems.
  3463. ;;
  3464. ;; Note that in the example above the list items could be paragraphs
  3465. ;; themselves and the list can be arbitrarily deep.
  3466. ;;
  3467. ;; Inspired by following thread:
  3468. ;; https://lists.gnu.org/r/emacs-orgmode/2011-03/msg01101.html
  3469. ;; Translate lists to tables
  3470. (defun org-odt--translate-list-tables (tree _backend info)
  3471. (org-element-map tree 'plain-list
  3472. (lambda (l1-list)
  3473. (when (org-export-read-attribute :attr_odt l1-list :list-table)
  3474. ;; Replace list with table.
  3475. (org-element-set-element
  3476. l1-list
  3477. ;; Build replacement table.
  3478. (apply 'org-element-adopt-elements
  3479. (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
  3480. (org-element-map l1-list 'item
  3481. (lambda (l1-item)
  3482. (let* ((l1-item-contents (org-element-contents l1-item))
  3483. l1-item-leading-text l2-list)
  3484. ;; Remove Level-2 list from the Level-item. It
  3485. ;; will be subsequently attached as table-cells.
  3486. (let ((cur l1-item-contents) prev)
  3487. (while (and cur (not (eq (org-element-type (car cur))
  3488. 'plain-list)))
  3489. (setq prev cur)
  3490. (setq cur (cdr cur)))
  3491. (when prev
  3492. (setcdr prev nil)
  3493. (setq l2-list (car cur)))
  3494. (setq l1-item-leading-text l1-item-contents))
  3495. ;; Level-1 items start a table row.
  3496. (apply 'org-element-adopt-elements
  3497. (list 'table-row (list :type 'standard))
  3498. ;; Leading text of level-1 item define
  3499. ;; the first table-cell.
  3500. (apply 'org-element-adopt-elements
  3501. (list 'table-cell nil)
  3502. l1-item-leading-text)
  3503. ;; Level-2 items define subsequent
  3504. ;; table-cells of the row.
  3505. (org-element-map l2-list 'item
  3506. (lambda (l2-item)
  3507. (apply 'org-element-adopt-elements
  3508. (list 'table-cell nil)
  3509. (org-element-contents l2-item)))
  3510. info nil 'item))))
  3511. info nil 'item))))
  3512. nil)
  3513. info)
  3514. tree)
  3515. ;;; Interactive functions
  3516. (defun org-odt-create-manifest-file-entry (&rest args)
  3517. (push args org-odt-manifest-file-entries))
  3518. (defun org-odt-write-manifest-file ()
  3519. (make-directory (concat org-odt-zip-dir "META-INF"))
  3520. (let ((manifest-file (concat org-odt-zip-dir "META-INF/manifest.xml")))
  3521. (with-current-buffer
  3522. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3523. (find-file-noselect manifest-file t))
  3524. (insert
  3525. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  3526. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  3527. (dolist (file-entry org-odt-manifest-file-entries)
  3528. (let* ((version (nth 2 file-entry))
  3529. (extra (if (not version) ""
  3530. (format " manifest:version=\"%s\"" version))))
  3531. (insert
  3532. (format org-odt-manifest-file-entry-tag
  3533. (nth 0 file-entry) (nth 1 file-entry) extra))))
  3534. (insert "\n</manifest:manifest>"))))
  3535. (defmacro org-odt--export-wrap (out-file &rest body)
  3536. `(let* ((--out-file ,out-file)
  3537. (out-file-type (file-name-extension --out-file))
  3538. (org-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  3539. "meta.xml" "styles.xml"))
  3540. ;; Initialize temporary workarea. All files that end up in
  3541. ;; the exported document get parked/created here.
  3542. (org-odt-zip-dir (file-name-as-directory
  3543. (make-temp-file (format "%s-" out-file-type) t)))
  3544. (org-odt-manifest-file-entries nil)
  3545. (--cleanup-xml-buffers
  3546. (lambda ()
  3547. ;; Kill all XML buffers.
  3548. (dolist (file org-odt-xml-files)
  3549. (let ((buf (find-buffer-visiting
  3550. (concat org-odt-zip-dir file))))
  3551. (when buf
  3552. (with-current-buffer buf
  3553. (set-buffer-modified-p nil)
  3554. (kill-buffer buf)))))
  3555. ;; Delete temporary directory and also other embedded
  3556. ;; files that get copied there.
  3557. (delete-directory org-odt-zip-dir t))))
  3558. (condition-case err
  3559. (progn
  3560. (unless (executable-find "zip")
  3561. ;; Not at all OSes ship with zip by default
  3562. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3563. ;; Do export. This creates a bunch of xml files ready to be
  3564. ;; saved and zipped.
  3565. (progn ,@body)
  3566. ;; Create a manifest entry for content.xml.
  3567. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  3568. ;; Write mimetype file
  3569. (let* ((mimetypes
  3570. '(("odt" . "application/vnd.oasis.opendocument.text")
  3571. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3572. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3573. (unless mimetype
  3574. (error "Unknown OpenDocument backend %S" out-file-type))
  3575. (write-region mimetype nil (concat org-odt-zip-dir "mimetype"))
  3576. (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3577. ;; Write out the manifest entries before zipping
  3578. (org-odt-write-manifest-file)
  3579. ;; Save all XML files.
  3580. (dolist (file org-odt-xml-files)
  3581. (let ((buf (find-buffer-visiting
  3582. (concat org-odt-zip-dir file))))
  3583. (when buf
  3584. (with-current-buffer buf
  3585. ;; Prettify output if needed.
  3586. (when org-odt-prettify-xml
  3587. (indent-region (point-min) (point-max)))
  3588. (save-buffer 0)))))
  3589. ;; Run zip.
  3590. (let* ((target --out-file)
  3591. (target-name (file-name-nondirectory target))
  3592. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3593. ("zip" "-rmTq" ,target-name "."))))
  3594. ;; If a file with same name as the desired output file
  3595. ;; exists, remove it.
  3596. (when (file-exists-p target)
  3597. (delete-file target))
  3598. ;; Zip up the xml files.
  3599. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3600. (message "Creating ODT file...")
  3601. ;; Switch temporarily to content.xml. This way Zip
  3602. ;; process will inherit `org-odt-zip-dir' as the current
  3603. ;; directory.
  3604. (with-current-buffer
  3605. (find-file-noselect (concat org-odt-zip-dir "content.xml") t)
  3606. (dolist (cmd cmds)
  3607. (message "Running %s" (mapconcat 'identity cmd " "))
  3608. (setq err-string
  3609. (with-output-to-string
  3610. (setq exitcode
  3611. (apply 'call-process (car cmd)
  3612. nil standard-output nil (cdr cmd)))))
  3613. (or (zerop exitcode)
  3614. (error (concat "Unable to create OpenDocument file."
  3615. " Zip failed with error (%s)")
  3616. err-string)))))
  3617. ;; Move the zip file from temporary work directory to
  3618. ;; user-mandated location.
  3619. (rename-file (concat org-odt-zip-dir target-name) target)
  3620. (message "Created %s" (expand-file-name target))
  3621. ;; Cleanup work directory and work files.
  3622. (funcall --cleanup-xml-buffers)
  3623. ;; Open the OpenDocument file in archive-mode for
  3624. ;; examination.
  3625. (find-file-noselect target t)
  3626. ;; Return exported file.
  3627. (cond
  3628. ;; Case 1: Conversion desired on exported file. Run the
  3629. ;; converter on the OpenDocument file. Return the
  3630. ;; converted file.
  3631. (org-odt-preferred-output-format
  3632. (or (org-odt-convert target org-odt-preferred-output-format)
  3633. target))
  3634. ;; Case 2: No further conversion. Return exported
  3635. ;; OpenDocument file.
  3636. (t target))))
  3637. (error
  3638. ;; Cleanup work directory and work files.
  3639. (funcall --cleanup-xml-buffers)
  3640. (message "OpenDocument export failed: %s"
  3641. (error-message-string err))))))
  3642. ;;;; Export to OpenDocument formula
  3643. ;;;###autoload
  3644. (defun org-odt-export-as-odf (latex-frag &optional odf-file)
  3645. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3646. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3647. MathML. When invoked as an interactive command, use
  3648. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3649. region. If no LaTeX fragments are found, prompt for it. Push
  3650. MathML source to kill ring depending on the value of
  3651. `org-export-copy-to-kill-ring'."
  3652. (interactive
  3653. `(,(let (frag)
  3654. (setq frag (and (setq frag (and (region-active-p)
  3655. (buffer-substring (region-beginning)
  3656. (region-end))))
  3657. (cl-loop for e in org-latex-regexps
  3658. thereis (when (string-match (nth 1 e) frag)
  3659. (match-string (nth 2 e) frag)))))
  3660. (read-string "LaTeX Fragment: " frag nil frag))
  3661. ,(let ((odf-filename (expand-file-name
  3662. (concat
  3663. (file-name-sans-extension
  3664. (or (file-name-nondirectory buffer-file-name)))
  3665. "." "odf")
  3666. (file-name-directory buffer-file-name))))
  3667. (read-file-name "ODF filename: " nil odf-filename nil
  3668. (file-name-nondirectory odf-filename)))))
  3669. (let ((filename (or odf-file
  3670. (expand-file-name
  3671. (concat
  3672. (file-name-sans-extension
  3673. (or (file-name-nondirectory buffer-file-name)))
  3674. "." "odf")
  3675. (file-name-directory buffer-file-name)))))
  3676. (org-odt--export-wrap
  3677. filename
  3678. (let* ((buffer (progn
  3679. (require 'nxml-mode)
  3680. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3681. (find-file-noselect (concat org-odt-zip-dir
  3682. "content.xml") t))))
  3683. (coding-system-for-write 'utf-8)
  3684. (save-buffer-coding-system 'utf-8))
  3685. (set-buffer buffer)
  3686. (set-buffer-file-coding-system coding-system-for-write)
  3687. (let ((mathml (org-create-math-formula latex-frag)))
  3688. (unless mathml (error "No Math formula created"))
  3689. (insert mathml)
  3690. ;; Add MathML to kill ring, if needed.
  3691. (when (org-export--copy-to-kill-ring-p)
  3692. (org-kill-new (buffer-string))))))))
  3693. ;;;###autoload
  3694. (defun org-odt-export-as-odf-and-open ()
  3695. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3696. Use `org-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3697. formula file."
  3698. (interactive)
  3699. (org-open-file (call-interactively 'org-odt-export-as-odf) 'system))
  3700. ;;;; Export to OpenDocument Text
  3701. ;;;###autoload
  3702. (defun org-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
  3703. "Export current buffer to a ODT file.
  3704. If narrowing is active in the current buffer, only export its
  3705. narrowed part.
  3706. If a region is active, export that region.
  3707. A non-nil optional argument ASYNC means the process should happen
  3708. asynchronously. The resulting file should be accessible through
  3709. the `org-export-stack' interface.
  3710. When optional argument SUBTREEP is non-nil, export the sub-tree
  3711. at point, extracting information from the headline properties
  3712. first.
  3713. When optional argument VISIBLE-ONLY is non-nil, don't export
  3714. contents of hidden elements.
  3715. EXT-PLIST, when provided, is a property list with external
  3716. parameters overriding Org default settings, but still inferior to
  3717. file-local settings.
  3718. Return output file's name."
  3719. (interactive)
  3720. (let ((outfile (org-export-output-file-name ".odt" subtreep)))
  3721. (if async
  3722. (org-export-async-start (lambda (f) (org-export-add-to-stack f 'odt))
  3723. `(expand-file-name
  3724. (org-odt--export-wrap
  3725. ,outfile
  3726. (let* ((org-odt-embedded-images-count 0)
  3727. (org-odt-embedded-formulas-count 0)
  3728. (org-odt-automatic-styles nil)
  3729. (org-odt-object-counters nil)
  3730. ;; Let `htmlfontify' know that we are interested in
  3731. ;; collecting styles.
  3732. (hfy-user-sheet-assoc nil))
  3733. ;; Initialize content.xml and kick-off the export
  3734. ;; process.
  3735. (let ((out-buf
  3736. (progn
  3737. (require 'nxml-mode)
  3738. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3739. (find-file-noselect
  3740. (concat org-odt-zip-dir "content.xml") t))))
  3741. (output (org-export-as
  3742. 'odt ,subtreep ,visible-only nil ,ext-plist)))
  3743. (with-current-buffer out-buf
  3744. (erase-buffer)
  3745. (insert output)))))))
  3746. (org-odt--export-wrap
  3747. outfile
  3748. (let* ((org-odt-embedded-images-count 0)
  3749. (org-odt-embedded-formulas-count 0)
  3750. (org-odt-automatic-styles nil)
  3751. (org-odt-object-counters nil)
  3752. ;; Let `htmlfontify' know that we are interested in collecting
  3753. ;; styles.
  3754. (hfy-user-sheet-assoc nil))
  3755. ;; Initialize content.xml and kick-off the export process.
  3756. (let ((output (org-export-as 'odt subtreep visible-only nil ext-plist))
  3757. (out-buf (progn
  3758. (require 'nxml-mode)
  3759. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3760. (find-file-noselect
  3761. (concat org-odt-zip-dir "content.xml") t)))))
  3762. (with-current-buffer out-buf (erase-buffer) (insert output))))))))
  3763. ;;;; Convert between OpenDocument and other formats
  3764. (defun org-odt-reachable-p (in-fmt out-fmt)
  3765. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3766. (catch 'done
  3767. (let ((reachable-formats (org-odt-do-reachable-formats in-fmt)))
  3768. (dolist (e reachable-formats)
  3769. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3770. (when out-fmt-spec
  3771. (throw 'done (cons (car e) out-fmt-spec))))))))
  3772. (defun org-odt-do-convert (in-file out-fmt &optional open)
  3773. "Workhorse routine for `org-odt-convert'."
  3774. (require 'browse-url)
  3775. (let* ((in-file (let ((f (expand-file-name (or in-file buffer-file-name))))
  3776. (if (file-readable-p f) f
  3777. (error "Cannot read %s" in-file))))
  3778. (in-fmt (file-name-extension in-file))
  3779. (out-fmt (or out-fmt (error "Output format unspecified")))
  3780. (how (or (org-odt-reachable-p in-fmt out-fmt)
  3781. (error "Cannot convert from %s format to %s format?"
  3782. in-fmt out-fmt)))
  3783. (convert-process (car how))
  3784. (out-file (concat (file-name-sans-extension in-file) "."
  3785. (nth 1 (or (cdr how) out-fmt))))
  3786. (extra-options (or (nth 2 (cdr how)) ""))
  3787. (out-dir (file-name-directory in-file))
  3788. (cmd (format-spec convert-process
  3789. `((?i . ,(shell-quote-argument in-file))
  3790. (?I . ,(browse-url-file-url in-file))
  3791. (?f . ,out-fmt)
  3792. (?o . ,(shell-quote-argument out-file))
  3793. (?O . ,(browse-url-file-url out-file))
  3794. (?d . , (shell-quote-argument out-dir))
  3795. (?D . ,(browse-url-file-url out-dir))
  3796. (?x . ,extra-options)))))
  3797. (when (file-exists-p out-file)
  3798. (delete-file out-file))
  3799. (message "Executing %s" cmd)
  3800. (let ((cmd-output (shell-command-to-string cmd)))
  3801. (message "%s" cmd-output))
  3802. (cond
  3803. ((file-exists-p out-file)
  3804. (message "Exported to %s" out-file)
  3805. (when open
  3806. (message "Opening %s..." out-file)
  3807. (org-open-file out-file 'system))
  3808. out-file)
  3809. (t
  3810. (message "Export to %s failed" out-file)
  3811. nil))))
  3812. (defun org-odt-do-reachable-formats (in-fmt)
  3813. "Return verbose info about formats to which IN-FMT can be converted.
  3814. Return a list where each element is of the
  3815. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3816. `org-odt-convert-processes' for CONVERTER-PROCESS and see
  3817. `org-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3818. (let* ((converter
  3819. (and org-odt-convert-process
  3820. (cadr (assoc-string org-odt-convert-process
  3821. org-odt-convert-processes t))))
  3822. (capabilities
  3823. (and org-odt-convert-process
  3824. (cadr (assoc-string org-odt-convert-process
  3825. org-odt-convert-processes t))
  3826. org-odt-convert-capabilities))
  3827. reachable-formats)
  3828. (when converter
  3829. (dolist (c capabilities)
  3830. (when (member in-fmt (nth 1 c))
  3831. (push (cons converter (nth 2 c)) reachable-formats))))
  3832. reachable-formats))
  3833. (defun org-odt-reachable-formats (in-fmt)
  3834. "Return list of formats to which IN-FMT can be converted.
  3835. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3836. (copy-sequence
  3837. (apply #'append (mapcar
  3838. (lambda (e) (mapcar #'car (cdr e)))
  3839. (org-odt-do-reachable-formats in-fmt)))))
  3840. (defun org-odt-convert-read-params ()
  3841. "Return IN-FILE and OUT-FMT params for `org-odt-do-convert'.
  3842. This is a helper routine for interactive use."
  3843. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3844. (in-file (read-file-name "File to be converted: "
  3845. nil buffer-file-name t))
  3846. (in-fmt (file-name-extension in-file))
  3847. (out-fmt-choices (org-odt-reachable-formats in-fmt))
  3848. (out-fmt
  3849. (or (and out-fmt-choices
  3850. (funcall input "Output format: "
  3851. out-fmt-choices nil nil nil))
  3852. (error
  3853. "No known converter or no known output formats for %s files"
  3854. in-fmt))))
  3855. (list in-file out-fmt)))
  3856. ;;;###autoload
  3857. (defun org-odt-convert (&optional in-file out-fmt open)
  3858. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3859. IN-FILE is the file to be converted. If unspecified, it defaults
  3860. to variable `buffer-file-name'. OUT-FMT is the desired output
  3861. format. Use `org-odt-convert-process' as the converter. If OPEN
  3862. is non-nil then the newly converted file is opened using
  3863. `org-open-file'."
  3864. (interactive
  3865. (append (org-odt-convert-read-params) current-prefix-arg))
  3866. (org-odt-do-convert in-file out-fmt open))
  3867. ;;; Library Initializations
  3868. (dolist (desc org-odt-file-extensions)
  3869. ;; Let Emacs open all OpenDocument files in archive mode.
  3870. (add-to-list 'auto-mode-alist
  3871. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3872. (provide 'ox-odt)
  3873. ;; Local variables:
  3874. ;; generated-autoload-file: "org-loaddefs.el"
  3875. ;; End:
  3876. ;;; ox-odt.el ends here