org-e-odt.el 135 KB

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