ox-odt.el 159 KB

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