org-e-odt.el 142 KB

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