org-e-odt.el 159 KB

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