org-e-odt.el 142 KB

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