org-e-odt.el 144 KB

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