ox-odt.el 159 KB

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