org-e-odt.el 143 KB

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