ox-odt.el 159 KB

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