ox-odt.el 158 KB

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