ox-odt.el 157 KB

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