ox-odt.el 159 KB

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