org-e-odt.el 153 KB

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