ox-odt.el 157 KB

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