ox-odt.el 159 KB

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