org-e-odt.el 135 KB

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