ox-odt.el 159 KB

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