org-e-odt.el 144 KB

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