org-e-odt.el 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207
  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 (a s v b)
  84. (if a (org-e-odt-export-to-odt t s v)
  85. (org-open-file (org-e-odt-export-to-odt nil s v) 'system))))))
  86. :options-alist
  87. ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
  88. (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
  89. ;;; Dependencies
  90. ;;; Hooks
  91. ;;; Function Declarations
  92. (declare-function org-id-find-id-file "org-id" (id))
  93. (declare-function hfy-face-to-style "htmlfontify" (fn))
  94. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  95. (declare-function archive-zip-extract "arc-mode" (archive name))
  96. (declare-function org-create-math-formula "org" (latex-frag &optional 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-expand-wildcards
  325. (expand-file-name "od-manifest-schema*.rnc"
  326. schema-dir))
  327. (file-expand-wildcards
  328. (expand-file-name "od-schema*.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-expand-wildcards
  366. (expand-file-name "od-manifest-schema*.rnc" schema-dir))
  367. (file-expand-wildcards
  368. (expand-file-name "od-schema*.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. ;;;; Src Block
  656. (defcustom org-e-odt-create-custom-styles-for-srcblocks t
  657. "Whether custom styles for colorized source blocks be automatically created.
  658. When this option is turned on, the exporter creates custom styles
  659. for source blocks based on the advice of `htmlfontify'. Creation
  660. of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
  661. When this option is turned off exporter does not create such
  662. styles.
  663. Use the latter option if you do not want the custom styles to be
  664. based on your current display settings. It is necessary that the
  665. styles.xml already contains needed styles for colorizing to work.
  666. This variable is effective only if
  667. `org-e-odt-fontify-srcblocks' is turned on."
  668. :group 'org-export-e-odt
  669. :version "24.1"
  670. :type 'boolean)
  671. (defcustom org-e-odt-fontify-srcblocks t
  672. "Specify whether or not source blocks need to be fontified.
  673. Turn this option on if you want to colorize the source code
  674. blocks in the exported file. For colorization to work, you need
  675. to make available an enhanced version of `htmlfontify' library."
  676. :type 'boolean
  677. :group 'org-export-e-odt
  678. :version "24.1")
  679. ;;;; Table
  680. (defcustom org-e-odt-table-caption-above t
  681. "When non-nil, place caption string at the beginning of the table.
  682. Otherwise, place it near the end."
  683. :group 'org-export-e-odt
  684. :type 'boolean)
  685. (defcustom org-e-odt-table-styles
  686. '(("OrgEquation" "OrgEquation"
  687. ((use-first-column-styles . t)
  688. (use-last-column-styles . t)))
  689. ("TableWithHeaderRowAndColumn" "Custom"
  690. ((use-first-row-styles . t)
  691. (use-first-column-styles . t)))
  692. ("TableWithFirstRowandLastRow" "Custom"
  693. ((use-first-row-styles . t)
  694. (use-last-row-styles . t)))
  695. ("GriddedTable" "Custom" nil))
  696. "Specify how Table Styles should be derived from a Table Template.
  697. This is a list where each element is of the
  698. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  699. TABLE-STYLE-NAME is the style associated with the table through
  700. \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
  701. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  702. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  703. below) that is included in
  704. `org-e-odt-content-template-file'.
  705. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  706. \"TableCell\"
  707. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  708. \"TableParagraph\"
  709. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  710. \"FirstRow\" | \"LastRow\" |
  711. \"EvenRow\" | \"OddRow\" |
  712. \"EvenColumn\" | \"OddColumn\" | \"\"
  713. where \"+\" above denotes string concatenation.
  714. TABLE-CELL-OPTIONS is an alist where each element is of the
  715. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  716. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  717. `use-last-row-styles' |
  718. `use-first-column-styles' |
  719. `use-last-column-styles' |
  720. `use-banding-rows-styles' |
  721. `use-banding-columns-styles' |
  722. `use-first-row-styles'
  723. ON-OR-OFF := `t' | `nil'
  724. For example, with the following configuration
  725. \(setq org-e-odt-table-styles
  726. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  727. \(\(use-first-row-styles . t\)
  728. \(use-first-column-styles . t\)\)\)
  729. \(\"TableWithHeaderColumns\" \"Custom\"
  730. \(\(use-first-column-styles . t\)\)\)\)\)
  731. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  732. style will use the following table-cell styles -
  733. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  734. \"CustomTableCell\" and the following paragraph styles
  735. \"CustomFirstRowTableParagraph\",
  736. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  737. as appropriate.
  738. 2. A table associated with \"TableWithHeaderColumns\" style will
  739. use the following table-cell styles -
  740. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  741. following paragraph styles
  742. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  743. as appropriate..
  744. Note that TABLE-TEMPLATE-NAME corresponds to the
  745. \"<table:table-template>\" elements contained within
  746. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  747. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  748. \"table:template-name\" and \"table:use-first-row-styles\" etc
  749. attributes of \"<table:table>\" element. Refer ODF-1.2
  750. specification for more information. Also consult the
  751. implementation filed under `org-e-odt-get-table-cell-styles'.
  752. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  753. formatting of numbered display equations. Do not delete this
  754. style from the list."
  755. :group 'org-export-e-odt
  756. :version "24.1"
  757. :type '(choice
  758. (const :tag "None" nil)
  759. (repeat :tag "Table Styles"
  760. (list :tag "Table Style Specification"
  761. (string :tag "Table Style Name")
  762. (string :tag "Table Template Name")
  763. (alist :options (use-first-row-styles
  764. use-last-row-styles
  765. use-first-column-styles
  766. use-last-column-styles
  767. use-banding-rows-styles
  768. use-banding-columns-styles)
  769. :key-type symbol
  770. :value-type (const :tag "True" t))))))
  771. ;;;; Timestamps
  772. (defcustom org-e-odt-use-date-fields nil
  773. "Non-nil, if timestamps should be exported as date fields.
  774. When nil, export timestamps as plain text.
  775. When non-nil, map `org-time-stamp-custom-formats' to a pair of
  776. OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
  777. respectively. A timestamp with no time component is formatted
  778. with style \"OrgDate1\" while one with explicit hour and minutes
  779. is formatted with style \"OrgDate2\".
  780. This feature is experimental. Most (but not all) of the common
  781. %-specifiers in `format-time-string' are supported.
  782. Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
  783. formatted as canonical Org timestamps. For finer control, avoid
  784. these %-specifiers.
  785. Textutal specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
  786. etc., are displayed by the application in the default language
  787. and country specified in `org-e-odt-styles-file'. Note that the
  788. default styles file uses language \"en\" and country \"GB\". You
  789. can localize the week day and month strings in the exported
  790. document by setting the default language and country either using
  791. the application UI or through a custom styles file.
  792. See `org-e-odt--build-date-styles' for implementation details."
  793. :group 'org-export-e-odt
  794. :type 'boolean)
  795. ;;; Internal functions
  796. ;;;; Date
  797. (defun org-e-odt--format-timestamp (timestamp &optional end iso-date-p)
  798. (let* ((format-timestamp
  799. (lambda (timestamp format &optional end utc)
  800. (if timestamp
  801. (org-export-format-timestamp timestamp format end utc)
  802. (format-time-string format nil utc))))
  803. (has-time-p (or (not timestamp)
  804. (org-export-timestamp-has-time-p timestamp)))
  805. (iso-date (let ((format (if has-time-p "%Y-%m-%dT%H:%M:%S"
  806. "%Y-%m-%dT%H:%M:%S")))
  807. (funcall format-timestamp timestamp format end))))
  808. (if iso-date-p iso-date
  809. (let* ((style (if has-time-p "OrgDate2" "OrgDate1"))
  810. ;; LibreOffice does not care about end goes as content
  811. ;; within the "<text:date>...</text:date>" field. The
  812. ;; displayed date is automagically corrected to match the
  813. ;; format requested by "style:data-style-name" attribute. So
  814. ;; don't bother about formatting the date contents to be
  815. ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
  816. ;; simple Org-style date should suffice.
  817. (date (let* ((formats
  818. (if org-display-custom-times
  819. (cons (substring
  820. (car org-time-stamp-custom-formats) 1 -1)
  821. (substring
  822. (cdr org-time-stamp-custom-formats) 1 -1))
  823. '("%Y-%m-%d %a" . "%Y-%m-%d %a %H:%M")))
  824. (format (if has-time-p (cdr formats) (car formats))))
  825. (funcall format-timestamp timestamp format end)))
  826. (repeater (let ((repeater-type (org-element-property
  827. :repeater-type timestamp))
  828. (repeater-value (org-element-property
  829. :repeater-value timestamp))
  830. (repeater-unit (org-element-property
  831. :repeater-unit timestamp)))
  832. (concat
  833. (case repeater-type
  834. (catchup "++") (restart ".+") (cumulate "+"))
  835. (when repeater-value
  836. (number-to-string repeater-value))
  837. (case repeater-unit
  838. (hour "h") (day "d") (week "w") (month "m")
  839. (year "y"))))))
  840. (concat
  841. (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
  842. iso-date style date)
  843. (and (not (string= repeater "")) " ")
  844. repeater)))))
  845. ;;;; Frame
  846. (defun org-e-odt--frame (text width height style &optional extra
  847. anchor-type)
  848. (let ((frame-attrs
  849. (concat
  850. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  851. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  852. extra
  853. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  854. (format
  855. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  856. style frame-attrs
  857. (concat text
  858. (let ((title (get-text-property 0 :title text))
  859. (desc (get-text-property 0 :description text)))
  860. (concat (and title
  861. (format "<svg:title>%s</svg:title>"
  862. (org-e-odt--encode-plain-text title t)))
  863. (and desc
  864. (format "<svg:desc>%s</svg:desc>"
  865. (org-e-odt--encode-plain-text desc t)))))))))
  866. ;;;; Library wrappers
  867. (defun org-e-odt--zip-extract (archive members target)
  868. (when (atom members) (setq members (list members)))
  869. (mapc (lambda (archive member target)
  870. (require 'arc-mode)
  871. (let* ((--quote-file-name
  872. ;; This is shamelessly stolen from `archive-zip-extract'.
  873. (lambda (name)
  874. (if (or (not (memq system-type '(windows-nt ms-dos)))
  875. (and (boundp 'w32-quote-process-args)
  876. (null w32-quote-process-args)))
  877. (shell-quote-argument name)
  878. name)))
  879. (target (funcall --quote-file-name target))
  880. (archive (expand-file-name archive))
  881. (archive-zip-extract
  882. (list "unzip" "-qq" "-o" "-d" target))
  883. exit-code command-output)
  884. (setq command-output
  885. (with-temp-buffer
  886. (setq exit-code (archive-zip-extract archive member))
  887. (buffer-string)))
  888. (unless (zerop exit-code)
  889. (message command-output)
  890. (error "Extraction failed"))))
  891. members))
  892. ;;;; Target
  893. (defun org-e-odt--target (text id)
  894. (if (not id) text
  895. (concat
  896. (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
  897. (format "\n<text:bookmark text:name=\"%s\"/>" id) text
  898. (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
  899. ;;;; Textbox
  900. (defun org-e-odt--textbox (text width height style &optional
  901. extra anchor-type)
  902. (org-e-odt--frame
  903. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  904. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  905. (and (not width)
  906. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  907. text)
  908. width nil style extra anchor-type))
  909. ;;;; Table of Contents
  910. (defun org-e-odt-begin-toc (index-title depth)
  911. (concat
  912. (format "
  913. <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
  914. <text:table-of-content-source text:outline-level=\"%d\">
  915. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  916. " depth index-title)
  917. (let ((levels (number-sequence 1 10)))
  918. (mapconcat
  919. (lambda (level)
  920. (format
  921. "
  922. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  923. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  924. <text:index-entry-chapter/>
  925. <text:index-entry-text/>
  926. <text:index-entry-link-end/>
  927. </text:table-of-content-entry-template>
  928. " level level)) levels ""))
  929. (format "
  930. </text:table-of-content-source>
  931. <text:index-body>
  932. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  933. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  934. </text:index-title>
  935. " index-title)))
  936. (defun org-e-odt-end-toc ()
  937. (format "
  938. </text:index-body>
  939. </text:table-of-content>
  940. "))
  941. (defun* org-e-odt-format-toc-headline
  942. (todo todo-type priority text tags
  943. &key level section-number headline-label &allow-other-keys)
  944. (setq text (concat
  945. (and org-export-with-section-numbers
  946. (concat section-number ". "))
  947. text
  948. (and tags
  949. (concat
  950. "<text:tab/>"
  951. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  952. "OrgTag" tags)))))
  953. (when todo
  954. (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
  955. "OrgTodo" text)))
  956. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  957. headline-label text))
  958. (defun org-e-odt-toc (depth info)
  959. (assert (wholenump depth))
  960. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  961. (headlines (org-export-collect-headlines
  962. info (and (wholenump depth) depth))))
  963. (when headlines
  964. (concat
  965. (org-e-odt-begin-toc title depth)
  966. (mapconcat
  967. (lambda (headline)
  968. (let* ((entry (org-e-odt-format-headline--wrap
  969. headline info 'org-e-odt-format-toc-headline))
  970. (level (org-export-get-relative-level headline info))
  971. (style (format "Contents_20_%d" level)))
  972. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  973. style entry)))
  974. headlines "\n")
  975. (org-e-odt-end-toc)))))
  976. ;;;; Document styles
  977. (defun org-e-odt-add-automatic-style (object-type &optional object-props)
  978. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  979. OBJECT-PROPS is (typically) a plist created by passing
  980. \"#+ATTR_ODT: \" option of the object in question to
  981. `org-e-odt-parse-block-attributes'.
  982. Use `org-e-odt-object-counters' to generate an automatic
  983. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  984. new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
  985. . STYLE-NAME)."
  986. (assert (stringp object-type))
  987. (let* ((object (intern object-type))
  988. (seqvar object)
  989. (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
  990. (object-name (format "%s%d" object-type seqno)) style-name)
  991. (setq org-e-odt-object-counters
  992. (plist-put org-e-odt-object-counters seqvar seqno))
  993. (when object-props
  994. (setq style-name (format "Org%s" object-name))
  995. (setq org-e-odt-automatic-styles
  996. (plist-put org-e-odt-automatic-styles object
  997. (append (list (list style-name object-props))
  998. (plist-get org-e-odt-automatic-styles object)))))
  999. (cons object-name style-name)))
  1000. ;;;; Checkbox
  1001. (defun org-e-odt--checkbox (item)
  1002. "Return check-box string associated to ITEM."
  1003. (let ((checkbox (org-element-property :checkbox item)))
  1004. (if (not checkbox) ""
  1005. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1006. "OrgCode" (case checkbox
  1007. (on "[&#x2713;] ") ; CHECK MARK
  1008. (off "[ ] ")
  1009. (trans "[-] "))))))
  1010. ;;; Template
  1011. (defun org-e-odt--build-date-styles (fmt style)
  1012. ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
  1013. ;; to modify the date fields. A date could be modified by
  1014. ;; offsetting in days. That's about it. Also, date and time may
  1015. ;; have to be emitted as two fields - a date field and a time field
  1016. ;; - separately.
  1017. ;; One can add Form Controls to date and time fields so that they
  1018. ;; can be easily modified. But then, the exported document will
  1019. ;; become tightly coupled with LibreOffice and may not function
  1020. ;; properly with other OpenDocument applications.
  1021. ;; I have a strange feeling that Date styles are a bit flaky at the
  1022. ;; moment.
  1023. ;; The feature is experimental.
  1024. (when (and fmt style)
  1025. (let* ((fmt-alist
  1026. '(("%A" . "<number:day-of-week number:style=\"long\"/>")
  1027. ("%B" . "<number:month number:textual=\"true\" number:style=\"long\"/>")
  1028. ("%H" . "<number:hours number:style=\"long\"/>")
  1029. ("%M" . "<number:minutes number:style=\"long\"/>")
  1030. ("%S" . "<number:seconds number:style=\"long\"/>")
  1031. ("%V" . "<number:week-of-year/>")
  1032. ("%Y" . "<number:year number:style=\"long\"/>")
  1033. ("%a" . "<number:day-of-week number:style=\"short\"/>")
  1034. ("%b" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1035. ("%d" . "<number:day number:style=\"long\"/>")
  1036. ("%e" . "<number:day number:style=\"short\"/>")
  1037. ("%h" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1038. ("%k" . "<number:hours number:style=\"short\"/>")
  1039. ("%m" . "<number:month number:style=\"long\"/>")
  1040. ("%p" . "<number:am-pm/>")
  1041. ("%y" . "<number:year number:style=\"short\"/>")))
  1042. (case-fold-search nil)
  1043. (re (mapconcat 'identity (mapcar 'car fmt-alist) "\\|"))
  1044. match rpl (start 0) (filler-beg 0) filler-end filler output)
  1045. (mapc
  1046. (lambda (pair)
  1047. (setq fmt (replace-regexp-in-string (car pair) (cdr pair) fmt t t)))
  1048. '(("\\(?:%[[:digit:]]*N\\)" . "") ; strip ns, us and ns
  1049. ("%C" . "Y") ; replace century with year
  1050. ("%D" . "%m/%d/%y")
  1051. ("%G" . "Y") ; year corresponding to iso week
  1052. ("%I" . "%H") ; hour on a 12-hour clock
  1053. ("%R" . "%H:%M")
  1054. ("%T" . "%H:%M:%S")
  1055. ("%U\\|%W" . "%V") ; week no. starting on Sun./Mon.
  1056. ("%Z" . "") ; time zone name
  1057. ("%c" . "%Y-%M-%d %a %H:%M" ) ; locale's date and time format
  1058. ("%g" . "%y")
  1059. ("%X" . "%x" ) ; locale's pref. time format
  1060. ("%j" . "") ; day of the year
  1061. ("%l" . "%k") ; like %I blank-padded
  1062. ("%s" . "") ; no. of secs since 1970-01-01 00:00:00 +0000
  1063. ("%n" . "<text:line-break/>")
  1064. ("%r" . "%I:%M:%S %p")
  1065. ("%t" . "<text:tab/>")
  1066. ("%u\\|%w" . "") ; numeric day of week - Mon (1-7), Sun(0-6)
  1067. ("%x" . "%Y-%M-%d %a") ; locale's pref. time format
  1068. ("%z" . "") ; time zone in numeric form
  1069. ))
  1070. (while (string-match re fmt start)
  1071. (setq match (match-string 0 fmt))
  1072. (setq rpl (assoc-default match fmt-alist))
  1073. (setq start (match-end 0))
  1074. (setq filler-end (match-beginning 0))
  1075. (setq filler (substring fmt (prog1 filler-beg
  1076. (setq filler-beg (match-end 0)))
  1077. filler-end))
  1078. (setq filler (and (not (string= filler ""))
  1079. (format "<number:text>%s</number:text>"
  1080. (org-e-odt--encode-plain-text filler))))
  1081. (setq output (concat output "\n" filler "\n" rpl)))
  1082. (setq filler (substring fmt filler-beg))
  1083. (unless (string= filler "")
  1084. (setq output (concat output
  1085. (format "\n<number:text>%s</number:text>"
  1086. (org-e-odt--encode-plain-text filler)))))
  1087. (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
  1088. style
  1089. (concat " number:automatic-order=\"true\""
  1090. " number:format-source=\"fixed\"")
  1091. output ))))
  1092. (defun org-e-odt-template (contents info)
  1093. "Return complete document string after HTML conversion.
  1094. CONTENTS is the transcoded contents string. RAW-DATA is the
  1095. original parsed data. INFO is a plist holding export options."
  1096. ;; Write meta file.
  1097. (let ((title (org-export-data (plist-get info :title) info))
  1098. (author (let ((author (plist-get info :author)))
  1099. (if (not author) "" (org-export-data author info))))
  1100. (email (plist-get info :email))
  1101. (keywords (plist-get info :keywords))
  1102. (description (plist-get info :description)))
  1103. (write-region
  1104. (concat
  1105. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1106. <office:document-meta
  1107. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1108. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1109. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1110. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1111. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1112. office:version=\"1.2\">
  1113. <office:meta>\n"
  1114. (format "<dc:creator>%s</dc:creator>\n" author)
  1115. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1116. ;; Date, if required.
  1117. (when (plist-get info :with-date)
  1118. ;; Check if DATE is specified as an Org-timestamp. If yes,
  1119. ;; include it as meta information. Otherwise, just use
  1120. ;; today's date.
  1121. (let* ((date (let ((date (plist-get info :date)))
  1122. (and (not (cdr date))
  1123. (eq (org-element-type (car date)) 'timestamp)
  1124. (car date)))))
  1125. (let ((iso-date (org-e-odt--format-timestamp date nil 'iso-date)))
  1126. (concat
  1127. (format "<dc:date>%s</dc:date>\n" iso-date)
  1128. (format "<meta:creation-date>%s</meta:creation-date>\n"
  1129. iso-date)))))
  1130. (format "<meta:generator>%s</meta:generator>\n"
  1131. (let ((creator-info (plist-get info :with-creator)))
  1132. (if (or (not creator-info) (eq creator-info 'comment)) ""
  1133. (plist-get info :creator))))
  1134. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1135. (format "<dc:subject>%s</dc:subject>\n" description)
  1136. (format "<dc:title>%s</dc:title>\n" title)
  1137. "\n"
  1138. " </office:meta>\n" "</office:document-meta>")
  1139. nil (concat org-e-odt-zip-dir "meta.xml"))
  1140. ;; Add meta.xml in to manifest.
  1141. (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1142. ;; Update styles file.
  1143. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1144. ;; Write styles file.
  1145. (let* ((styles-file (plist-get info :odt-styles-file))
  1146. (styles-file (and styles-file (read (org-trim styles-file))))
  1147. ;; Non-availability of styles.xml is not a critical
  1148. ;; error. For now, throw an error.
  1149. (styles-file (or styles-file
  1150. org-e-odt-styles-file
  1151. (expand-file-name "OrgOdtStyles.xml"
  1152. org-e-odt-styles-dir)
  1153. (error "org-e-odt: Missing styles file?"))))
  1154. (cond
  1155. ((listp styles-file)
  1156. (let ((archive (nth 0 styles-file))
  1157. (members (nth 1 styles-file)))
  1158. (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
  1159. (mapc
  1160. (lambda (member)
  1161. (when (org-file-image-p member)
  1162. (let* ((image-type (file-name-extension member))
  1163. (media-type (format "image/%s" image-type)))
  1164. (org-e-odt-create-manifest-file-entry media-type member))))
  1165. members)))
  1166. ((and (stringp styles-file) (file-exists-p styles-file))
  1167. (let ((styles-file-type (file-name-extension styles-file)))
  1168. (cond
  1169. ((string= styles-file-type "xml")
  1170. (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
  1171. ((member styles-file-type '("odt" "ott"))
  1172. (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
  1173. (t
  1174. (error (format "Invalid specification of styles.xml file: %S"
  1175. org-e-odt-styles-file))))
  1176. ;; create a manifest entry for styles.xml
  1177. (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1178. ;; FIXME: Who is opening an empty styles.xml before this point?
  1179. (with-current-buffer
  1180. (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
  1181. (revert-buffer t t)
  1182. ;; Write custom styles for source blocks
  1183. ;; Save STYLES used for colorizing of source blocks.
  1184. ;; Update styles.xml with styles that were collected as part of
  1185. ;; `org-e-odt-hfy-face-to-css' callbacks.
  1186. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1187. hfy-user-sheet-assoc "")))
  1188. (when styles
  1189. (goto-char (point-min))
  1190. (when (re-search-forward "</office:styles>" nil t)
  1191. (goto-char (match-beginning 0))
  1192. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1193. ;; Update styles.xml - take care of outline numbering
  1194. ;; Don't make automatic backup of styles.xml file. This setting
  1195. ;; prevents the backed-up styles.xml file from being zipped in to
  1196. ;; odt file. This is more of a hackish fix. Better alternative
  1197. ;; would be to fix the zip command so that the output odt file
  1198. ;; includes only the needed files and excludes any auto-generated
  1199. ;; extra files like backups and auto-saves etc etc. Note that
  1200. ;; currently the zip command zips up the entire temp directory so
  1201. ;; that any auto-generated files created under the hood ends up in
  1202. ;; the resulting odt file.
  1203. (set (make-local-variable 'backup-inhibited) t)
  1204. ;; Outline numbering is retained only upto LEVEL.
  1205. ;; To disable outline numbering pass a LEVEL of 0.
  1206. (goto-char (point-min))
  1207. (let ((regex
  1208. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1209. (replacement
  1210. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1211. (while (re-search-forward regex nil t)
  1212. (unless (let ((sec-num (plist-get info :section-numbers))
  1213. (level (string-to-number (match-string 2))))
  1214. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1215. (replace-match replacement t nil))))
  1216. (save-buffer 0)))
  1217. ;; Update content.xml.
  1218. (let* ( ;; `org-display-custom-times' should be accessed right
  1219. ;; within the context of the Org buffer. So obtain it's
  1220. ;; value before moving on to temp-buffer context down below.
  1221. (custom-time-fmts
  1222. (if org-display-custom-times
  1223. (cons (substring (car org-time-stamp-custom-formats) 1 -1)
  1224. (substring (cdr org-time-stamp-custom-formats) 1 -1))
  1225. '("%Y-%M-%d %a" . "%Y-%M-%d %a %H:%M"))))
  1226. (with-temp-buffer
  1227. (insert-file-contents
  1228. (or org-e-odt-content-template-file
  1229. (expand-file-name "OrgOdtContentTemplate.xml"
  1230. org-e-odt-styles-dir)))
  1231. ;; Write automatic styles.
  1232. ;; - Position the cursor.
  1233. (goto-char (point-min))
  1234. (re-search-forward " </office:automatic-styles>" nil t)
  1235. (goto-char (match-beginning 0))
  1236. ;; - Dump automatic table styles.
  1237. (loop for (style-name props) in
  1238. (plist-get org-e-odt-automatic-styles 'Table) do
  1239. (when (setq props (or (plist-get props :rel-width) 96))
  1240. (insert (format org-e-odt-table-style-format style-name props))))
  1241. ;; - Dump date-styles.
  1242. (when org-e-odt-use-date-fields
  1243. (insert (org-e-odt--build-date-styles (car custom-time-fmts)
  1244. "OrgDate1")
  1245. (org-e-odt--build-date-styles (cdr custom-time-fmts)
  1246. "OrgDate2")))
  1247. ;; Update display level.
  1248. ;; - Remove existing sequence decls. Also position the cursor.
  1249. (goto-char (point-min))
  1250. (when (re-search-forward "<text:sequence-decls" nil t)
  1251. (delete-region (match-beginning 0)
  1252. (re-search-forward "</text:sequence-decls>" nil nil)))
  1253. ;; Update sequence decls according to user preference.
  1254. (insert
  1255. (format
  1256. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1257. (mapconcat
  1258. (lambda (x)
  1259. (format
  1260. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1261. org-e-odt-display-outline-level (nth 1 x)))
  1262. org-e-odt-category-map-alist "\n")))
  1263. ;; Position the cursor to document body.
  1264. (goto-char (point-min))
  1265. (re-search-forward "</office:text>" nil nil)
  1266. (goto-char (match-beginning 0))
  1267. ;; Preamble - Title, Author, Date etc.
  1268. (insert
  1269. (let* ((title (org-export-data (plist-get info :title) info))
  1270. (author (and (plist-get info :with-author)
  1271. (let ((auth (plist-get info :author)))
  1272. (and auth (org-export-data auth info)))))
  1273. (email (plist-get info :email))
  1274. ;; Switch on or off above vars based on user settings
  1275. (author (and (plist-get info :with-author) (or author email)))
  1276. (email (and (plist-get info :with-email) email)))
  1277. (concat
  1278. ;; Title.
  1279. (when title
  1280. (concat
  1281. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1282. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1283. ;; Separator.
  1284. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1285. (cond
  1286. ((and author (not email))
  1287. ;; Author only.
  1288. (concat
  1289. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1290. "OrgSubtitle"
  1291. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1292. ;; Separator.
  1293. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1294. ((and author email)
  1295. ;; Author and E-mail.
  1296. (concat
  1297. (format
  1298. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1299. "OrgSubtitle"
  1300. (format
  1301. "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1302. (concat "mailto:" email)
  1303. (format "<text:initial-creator>%s</text:initial-creator>" author)))
  1304. ;; Separator.
  1305. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1306. ;; Date, if required.
  1307. (when (plist-get info :with-date)
  1308. (let* ((date (plist-get info :date))
  1309. ;; Check if DATE is specified as a timestamp.
  1310. (timestamp (and (not (cdr date))
  1311. (eq (org-element-type (car date)) 'timestamp)
  1312. (car date))))
  1313. (concat
  1314. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1315. "OrgSubtitle"
  1316. (if (and org-e-odt-use-date-fields timestamp)
  1317. (org-e-odt--format-timestamp (car date))
  1318. (org-export-data (plist-get info :date) info)))
  1319. ;; Separator
  1320. "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
  1321. ;; Table of Contents
  1322. (let* ((with-toc (plist-get info :with-toc))
  1323. (depth (and with-toc (if (wholenump with-toc)
  1324. with-toc
  1325. (plist-get info :headline-levels)))))
  1326. (when depth (insert (or (org-e-odt-toc depth info) ""))))
  1327. ;; Contents.
  1328. (insert contents)
  1329. ;; Return contents.
  1330. (buffer-substring-no-properties (point-min) (point-max)))))
  1331. ;;; Transcode Functions
  1332. ;;;; Bold
  1333. (defun org-e-odt-bold (bold contents info)
  1334. "Transcode BOLD from Org to ODT.
  1335. CONTENTS is the text with bold markup. INFO is a plist holding
  1336. contextual information."
  1337. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1338. "Bold" contents))
  1339. ;;;; Center Block
  1340. (defun org-e-odt-center-block (center-block contents info)
  1341. "Transcode a CENTER-BLOCK element from Org to ODT.
  1342. CONTENTS holds the contents of the center block. INFO is a plist
  1343. holding contextual information."
  1344. contents)
  1345. ;;;; Clock
  1346. (defun org-e-odt-clock (clock contents info)
  1347. "Transcode a CLOCK element from Org to ODT.
  1348. CONTENTS is nil. INFO is a plist used as a communication
  1349. channel."
  1350. (let ((timestamp (org-element-property :value clock))
  1351. (duration (org-element-property :duration clock)))
  1352. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1353. (if (eq (org-element-type (org-export-get-next-element clock info))
  1354. 'clock) "OrgClock" "OrgClockLastLine")
  1355. (concat
  1356. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1357. "OrgClockKeyword" org-clock-string)
  1358. (org-e-odt-timestamp timestamp contents info)
  1359. (and duration (format " (%s)" duration))))))
  1360. ;;;; Code
  1361. (defun org-e-odt-code (code contents info)
  1362. "Transcode a CODE object from Org to ODT.
  1363. CONTENTS is nil. INFO is a plist used as a communication
  1364. channel."
  1365. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1366. "OrgCode" (org-element-property :value code)))
  1367. ;;;; Comment
  1368. ;; Comments are ignored.
  1369. ;;;; Comment Block
  1370. ;; Comment Blocks are ignored.
  1371. ;;;; Drawer
  1372. (defun org-e-odt-drawer (drawer contents info)
  1373. "Transcode a DRAWER element from Org to ODT.
  1374. CONTENTS holds the contents of the block. INFO is a plist
  1375. holding contextual information."
  1376. (let* ((name (org-element-property :drawer-name drawer))
  1377. (output (if (functionp org-e-odt-format-drawer-function)
  1378. (funcall org-e-odt-format-drawer-function
  1379. name contents)
  1380. ;; If there's no user defined function: simply
  1381. ;; display contents of the drawer.
  1382. contents)))
  1383. output))
  1384. ;;;; Dynamic Block
  1385. (defun org-e-odt-dynamic-block (dynamic-block contents info)
  1386. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1387. CONTENTS holds the contents of the block. INFO is a plist
  1388. holding contextual information. See `org-export-data'."
  1389. contents)
  1390. ;;;; Entity
  1391. (defun org-e-odt-entity (entity contents info)
  1392. "Transcode an ENTITY object from Org to ODT.
  1393. CONTENTS are the definition itself. INFO is a plist holding
  1394. contextual information."
  1395. (org-element-property :utf-8 entity))
  1396. ;;;; Example Block
  1397. (defun org-e-odt-example-block (example-block contents info)
  1398. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1399. CONTENTS is nil. INFO is a plist holding contextual information."
  1400. (org-e-odt-format-code example-block info))
  1401. ;;;; Export Snippet
  1402. (defun org-e-odt-export-snippet (export-snippet contents info)
  1403. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1404. CONTENTS is nil. INFO is a plist holding contextual information."
  1405. (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
  1406. (org-element-property :value export-snippet)))
  1407. ;;;; Export Block
  1408. (defun org-e-odt-export-block (export-block contents info)
  1409. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1410. CONTENTS is nil. INFO is a plist holding contextual information."
  1411. (when (string= (org-element-property :type export-block) "ODT")
  1412. (org-remove-indentation (org-element-property :value export-block))))
  1413. ;;;; Fixed Width
  1414. (defun org-e-odt-fixed-width (fixed-width contents info)
  1415. "Transcode a FIXED-WIDTH element from Org to ODT.
  1416. CONTENTS is nil. INFO is a plist holding contextual information."
  1417. (org-e-odt-do-format-code (org-element-property :value fixed-width)))
  1418. ;;;; Footnote Definition
  1419. ;; Footnote Definitions are ignored.
  1420. ;;;; Footnote Reference
  1421. (defun org-e-odt-footnote-reference (footnote-reference contents info)
  1422. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1423. CONTENTS is nil. INFO is a plist holding contextual information."
  1424. (let ((--format-footnote-definition
  1425. (function
  1426. (lambda (n def)
  1427. (setq n (format "%d" n))
  1428. (let ((id (concat "fn" n))
  1429. (note-class "footnote")
  1430. (par-style "Footnote"))
  1431. (format
  1432. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1433. id note-class
  1434. (concat
  1435. (format "<text:note-citation>%s</text:note-citation>" n)
  1436. (format "<text:note-body>%s</text:note-body>" def)))))))
  1437. (--format-footnote-reference
  1438. (function
  1439. (lambda (n)
  1440. (setq n (format "%d" n))
  1441. (let ((note-class "footnote")
  1442. (ref-format "text")
  1443. (ref-name (concat "fn" n)))
  1444. (format
  1445. "<text:span text:style-name=\"%s\">%s</text:span>"
  1446. "OrgSuperscript"
  1447. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1448. note-class ref-format ref-name n)))))))
  1449. (concat
  1450. ;; Insert separator between two footnotes in a row.
  1451. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1452. (and (eq (org-element-type prev) 'footnote-reference)
  1453. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1454. "OrgSuperscript" ",")))
  1455. ;; Trancode footnote reference.
  1456. (let ((n (org-export-get-footnote-number footnote-reference info)))
  1457. (cond
  1458. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1459. (funcall --format-footnote-reference n))
  1460. ;; Inline definitions are secondary strings.
  1461. ;; Non-inline footnotes definitions are full Org data.
  1462. (t
  1463. (let* ((raw (org-export-get-footnote-definition footnote-reference
  1464. info))
  1465. (def (let ((def (org-trim (org-export-data raw info))))
  1466. (if (eq (org-element-type raw) 'org-data) def
  1467. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1468. "Footnote" def)))))
  1469. (funcall --format-footnote-definition n def))))))))
  1470. ;;;; Headline
  1471. (defun* org-e-odt-format-headline
  1472. (todo todo-type priority text tags
  1473. &key level section-number headline-label &allow-other-keys)
  1474. (concat
  1475. ;; Todo.
  1476. (and todo
  1477. (concat
  1478. (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
  1479. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1480. style todo)) " "))
  1481. ;; Title.
  1482. text
  1483. ;; Tags.
  1484. (and tags
  1485. (concat "<text:tab/>"
  1486. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1487. "OrgTag" (mapconcat 'org-trim tags " : "))))))
  1488. (defun org-e-odt-format-headline--wrap (headline info
  1489. &optional format-function
  1490. &rest extra-keys)
  1491. "Transcode an HEADLINE element from Org to ODT.
  1492. CONTENTS holds the contents of the headline. INFO is a plist
  1493. holding contextual information."
  1494. (let* ((level (+ (org-export-get-relative-level headline info)))
  1495. (headline-number (org-export-get-headline-number headline info))
  1496. (section-number (and (org-export-numbered-headline-p headline info)
  1497. (mapconcat 'number-to-string
  1498. headline-number ".")))
  1499. (todo (and (plist-get info :with-todo-keywords)
  1500. (let ((todo (org-element-property :todo-keyword headline)))
  1501. (and todo (org-export-data todo info)))))
  1502. (todo-type (and todo (org-element-property :todo-type headline)))
  1503. (priority (and (plist-get info :with-priority)
  1504. (org-element-property :priority headline)))
  1505. (text (org-export-data (org-element-property :title headline) info))
  1506. (tags (and (plist-get info :with-tags)
  1507. (org-export-get-tags headline info)))
  1508. (headline-label (concat "sec-" (mapconcat 'number-to-string
  1509. headline-number "-")))
  1510. (format-function (cond
  1511. ((functionp format-function) format-function)
  1512. ((functionp org-e-odt-format-headline-function)
  1513. (function*
  1514. (lambda (todo todo-type priority text tags
  1515. &allow-other-keys)
  1516. (funcall org-e-odt-format-headline-function
  1517. todo todo-type priority text tags))))
  1518. (t 'org-e-odt-format-headline))))
  1519. (apply format-function
  1520. todo todo-type priority text tags
  1521. :headline-label headline-label :level level
  1522. :section-number section-number extra-keys)))
  1523. (defun org-e-odt-headline (headline contents info)
  1524. "Transcode an HEADLINE element from Org to ODT.
  1525. CONTENTS holds the contents of the headline. INFO is a plist
  1526. holding contextual information."
  1527. ;; Case 1: This is a footnote section: ignore it.
  1528. (unless (org-element-property :footnote-section-p headline)
  1529. (let* ((text (org-export-data (org-element-property :title headline) info))
  1530. ;; Create the headline text.
  1531. (full-text (org-e-odt-format-headline--wrap headline info))
  1532. ;; Get level relative to current parsed data.
  1533. (level (org-export-get-relative-level headline info))
  1534. ;; Get canonical label for the headline.
  1535. (id (concat "sec-" (mapconcat 'number-to-string
  1536. (org-export-get-headline-number
  1537. headline info) "-")))
  1538. ;; Get user-specified labels for the headline.
  1539. (extra-ids (list (org-element-property :custom-id headline)
  1540. (org-element-property :id headline)))
  1541. ;; Extra targets.
  1542. (extra-targets
  1543. (mapconcat (lambda (x)
  1544. (when x
  1545. (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1546. (org-e-odt--target
  1547. "" (org-export-solidify-link-text x)))))
  1548. extra-ids ""))
  1549. ;; Title.
  1550. (anchored-title (org-e-odt--target full-text id)))
  1551. (cond
  1552. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1553. ;; Also export as items headlines for which no section
  1554. ;; format has been found.
  1555. ((org-export-low-level-p headline info)
  1556. ;; Build the real contents of the sub-tree.
  1557. (concat
  1558. (and (org-export-first-sibling-p headline info)
  1559. (format "\n<text:list text:style-name=\"%s\" %s>"
  1560. ;; Choose style based on list type.
  1561. (if (org-export-numbered-headline-p headline info)
  1562. "OrgNumberedList" "OrgBulletedList")
  1563. ;; If top-level list, re-start numbering. Otherwise,
  1564. ;; continue numbering.
  1565. (format "text:continue-numbering=\"%s\""
  1566. (let* ((parent (org-export-get-parent-headline
  1567. headline)))
  1568. (if (and parent
  1569. (org-export-low-level-p parent info))
  1570. "true" "false")))))
  1571. (let ((headline-has-table-p
  1572. (let ((section (assq 'section (org-element-contents headline))))
  1573. (assq 'table (and section (org-element-contents section))))))
  1574. (format "\n<text:list-item>\n%s\n%s"
  1575. (concat
  1576. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1577. "Text_20_body"
  1578. (concat extra-targets anchored-title))
  1579. contents)
  1580. (if headline-has-table-p
  1581. "</text:list-header>"
  1582. "</text:list-item>")))
  1583. (and (org-export-last-sibling-p headline info)
  1584. "</text:list>")))
  1585. ;; Case 3. Standard headline. Export it as a section.
  1586. (t
  1587. (concat
  1588. (format
  1589. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
  1590. (format "Heading_20_%s" level)
  1591. level
  1592. (concat extra-targets anchored-title))
  1593. contents))))))
  1594. ;;;; Horizontal Rule
  1595. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  1596. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1597. CONTENTS is nil. INFO is a plist holding contextual information."
  1598. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1599. "Horizontal_20_Line" ""))
  1600. ;;;; Inline Babel Call
  1601. ;; Inline Babel Calls are ignored.
  1602. ;;;; Inline Src Block
  1603. (defun org-e-odt--find-verb-separator (s)
  1604. "Return a character not used in string S.
  1605. This is used to choose a separator for constructs like \\verb."
  1606. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1607. (loop for c across ll
  1608. when (not (string-match (regexp-quote (char-to-string c)) s))
  1609. return (char-to-string c))))
  1610. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  1611. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1612. CONTENTS holds the contents of the item. INFO is a plist holding
  1613. contextual information."
  1614. (let* ((org-lang (org-element-property :language inline-src-block))
  1615. (code (org-element-property :value inline-src-block))
  1616. (separator (org-e-odt--find-verb-separator code)))
  1617. (error "FIXME")))
  1618. ;;;; Inlinetask
  1619. (defun org-e-odt-inlinetask (inlinetask contents info)
  1620. "Transcode an INLINETASK element from Org to ODT.
  1621. CONTENTS holds the contents of the block. INFO is a plist
  1622. holding contextual information."
  1623. (cond
  1624. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  1625. ;; with appropriate arguments.
  1626. ((functionp org-e-odt-format-inlinetask-function)
  1627. (let ((format-function
  1628. (function*
  1629. (lambda (todo todo-type priority text tags
  1630. &key contents &allow-other-keys)
  1631. (funcall org-e-odt-format-inlinetask-function
  1632. todo todo-type priority text tags contents)))))
  1633. (org-e-odt-format-headline--wrap
  1634. inlinetask info format-function :contents contents)))
  1635. ;; Otherwise, use a default template.
  1636. (t
  1637. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1638. "Text_20_body"
  1639. (org-e-odt--textbox
  1640. (concat
  1641. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1642. "OrgInlineTaskHeading"
  1643. (org-e-odt-format-headline--wrap
  1644. inlinetask info))
  1645. contents)
  1646. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\"")))))
  1647. ;;;; Italic
  1648. (defun org-e-odt-italic (italic contents info)
  1649. "Transcode ITALIC from Org to ODT.
  1650. CONTENTS is the text with italic markup. INFO is a plist holding
  1651. contextual information."
  1652. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1653. "Emphasis" contents))
  1654. ;;;; Item
  1655. (defun org-e-odt-item (item contents info)
  1656. "Transcode an ITEM element from Org to ODT.
  1657. CONTENTS holds the contents of the item. INFO is a plist holding
  1658. contextual information."
  1659. (let* ((plain-list (org-export-get-parent item))
  1660. (type (org-element-property :type plain-list))
  1661. (counter (org-element-property :counter item))
  1662. (tag (let ((tag (org-element-property :tag item)))
  1663. (and tag
  1664. (concat (org-e-odt--checkbox item)
  1665. (org-export-data tag info))))))
  1666. (case type
  1667. ((ordered unordered descriptive-1 descriptive-2)
  1668. (format "\n<text:list-item>\n%s\n%s"
  1669. contents
  1670. (let* ((--element-has-a-table-p
  1671. (function
  1672. (lambda (element info)
  1673. (loop for el in (org-element-contents element)
  1674. thereis (eq (org-element-type el) 'table))))))
  1675. (cond
  1676. ((funcall --element-has-a-table-p item info)
  1677. "</text:list-header>")
  1678. (t "</text:list-item>")))))
  1679. (t (error "Unknown list type: %S" type)))))
  1680. ;;;; Keyword
  1681. (defun org-e-odt-keyword (keyword contents info)
  1682. "Transcode a KEYWORD element from Org to ODT.
  1683. CONTENTS is nil. INFO is a plist holding contextual information."
  1684. (let ((key (org-element-property :key keyword))
  1685. (value (org-element-property :value keyword)))
  1686. (cond
  1687. ((string= key "ODT") value)
  1688. ((string= key "INDEX")
  1689. ;; FIXME
  1690. (ignore))
  1691. ((string= key "TARGET") nil)
  1692. ((string= key "toc")
  1693. (let ((value (downcase value)))
  1694. (cond
  1695. ((string-match "\\<headlines\\>" value)
  1696. (let ((depth (or (and (string-match "[0-9]+" value)
  1697. (string-to-number (match-string 0 value)))
  1698. (plist-get info :with-toc))))
  1699. (when (wholenump depth) (org-e-odt-toc depth info))))
  1700. ((member value '("tables" "figures" "listings"))
  1701. ;; FIXME
  1702. (ignore))))))))
  1703. ;;;; Latex Environment
  1704. (eval-after-load 'org-odt
  1705. '(ad-deactivate 'org-format-latex-as-mathml))
  1706. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1707. ;; (after org-e-odt-protect-latex-fragment activate)
  1708. ;; "Encode LaTeX fragment as XML.
  1709. ;; Do this when translation to MathML fails."
  1710. ;; (when (or (not (> (length ad-return-value) 0))
  1711. ;; (get-text-property 0 'org-protected ad-return-value))
  1712. ;; (setq ad-return-value
  1713. ;; (org-propertize (org-e-odt--encode-plain-text (ad-get-arg 0))
  1714. ;; 'org-protected t))))
  1715. (defun org-e-odt-latex-environment (latex-environment contents info)
  1716. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1717. CONTENTS is nil. INFO is a plist holding contextual information."
  1718. (let* ((latex-frag (org-remove-indentation
  1719. (org-element-property :value latex-environment))))
  1720. (org-e-odt-do-format-code latex-frag)))
  1721. ;;;; Latex Fragment
  1722. ;; (when latex-frag ; FIXME
  1723. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1724. ;; :description latex-frag)))
  1725. ;; handle verbatim
  1726. ;; provide descriptions
  1727. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  1728. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1729. CONTENTS is nil. INFO is a plist holding contextual information."
  1730. (let* ((latex-frag (org-element-property :value latex-fragment))
  1731. (processing-type (plist-get info :LaTeX-fragments)))
  1732. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1733. "OrgCode" (org-e-odt--encode-plain-text latex-frag t))))
  1734. ;;;; Line Break
  1735. (defun org-e-odt-line-break (line-break contents info)
  1736. "Transcode a LINE-BREAK object from Org to ODT.
  1737. CONTENTS is nil. INFO is a plist holding contextual information."
  1738. "<text:line-break/>\n")
  1739. ;;;; Link
  1740. ;;;; Links :: Label references
  1741. (defun org-e-odt--enumerate (element info &optional predicate n)
  1742. (when predicate (assert (funcall predicate element info)))
  1743. (let* ((--numbered-parent-headline-at-<=-n
  1744. (function
  1745. (lambda (element n info)
  1746. (loop for x in (org-export-get-genealogy element)
  1747. thereis (and (eq (org-element-type x) 'headline)
  1748. (<= (org-export-get-relative-level x info) n)
  1749. (org-export-numbered-headline-p x info)
  1750. x)))))
  1751. (--enumerate
  1752. (function
  1753. (lambda (element scope info &optional predicate)
  1754. (let ((counter 0))
  1755. (org-element-map
  1756. (or scope (plist-get info :parse-tree))
  1757. (org-element-type element)
  1758. (lambda (el)
  1759. (and (or (not predicate) (funcall predicate el info))
  1760. (incf counter)
  1761. (eq element el)
  1762. counter))
  1763. info 'first-match)))))
  1764. (scope (funcall --numbered-parent-headline-at-<=-n
  1765. element (or n org-e-odt-display-outline-level) info))
  1766. (ordinal (funcall --enumerate element scope info predicate))
  1767. (tag
  1768. (concat
  1769. ;; Section number.
  1770. (and scope
  1771. (mapconcat 'number-to-string
  1772. (org-export-get-headline-number scope info) "."))
  1773. ;; Separator.
  1774. (and scope ".")
  1775. ;; Ordinal.
  1776. (number-to-string ordinal))))
  1777. tag))
  1778. (defun org-e-odt-format-label (element info op)
  1779. (assert (memq (org-element-type element) '(link table src-block paragraph)))
  1780. (let* ((caption-from
  1781. (case (org-element-type element)
  1782. (link (org-export-get-parent-element element))
  1783. (t element)))
  1784. ;; Get label and caption.
  1785. (label (org-element-property :name caption-from))
  1786. (caption (org-export-get-caption caption-from))
  1787. (short-caption (org-export-get-caption caption-from t))
  1788. ;; Transcode captions.
  1789. (caption (and caption (org-export-data caption info)))
  1790. (short-caption (and short-caption
  1791. (org-export-data short-caption info))))
  1792. (when (or label caption)
  1793. (let* ((default-category
  1794. (case (org-element-type element)
  1795. (table "__Table__")
  1796. (src-block "__Listing__")
  1797. ((link paragraph)
  1798. (cond
  1799. ((org-e-odt--enumerable-latex-image-p element info)
  1800. "__DvipngImage__")
  1801. ((org-e-odt--enumerable-image-p element info)
  1802. "__Figure__")
  1803. ((org-e-odt--enumerable-formula-p element info)
  1804. "__MathFormula__")
  1805. (t (error "Don't know how to format label for link: %S"
  1806. element))))
  1807. (t (error "Don't know how to format label for element type: %s"
  1808. (org-element-type element)))))
  1809. seqno)
  1810. (assert default-category)
  1811. (destructuring-bind (counter label-style category predicate)
  1812. (assoc-default default-category org-e-odt-category-map-alist)
  1813. ;; Compute sequence number of the element.
  1814. (setq seqno (org-e-odt--enumerate element info predicate))
  1815. ;; Localize category string.
  1816. (setq category (org-export-translate category :utf-8 info))
  1817. (case op
  1818. ;; Case 1: Handle Label definition.
  1819. (definition
  1820. ;; Assign an internal label, if user has not provided one
  1821. (setq label (or label (format "%s-%s" default-category seqno)))
  1822. (setq label (org-export-solidify-link-text label))
  1823. (cons
  1824. (format-spec
  1825. (cadr (assoc-string label-style org-e-odt-label-styles t))
  1826. `((?e . ,category)
  1827. (?n . ,(format
  1828. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1829. label counter counter seqno))
  1830. (?c . ,(or caption ""))))
  1831. short-caption))
  1832. ;; Case 2: Handle Label reference.
  1833. (reference
  1834. (assert label)
  1835. (setq label (org-export-solidify-link-text label))
  1836. (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
  1837. (fmt1 (car fmt))
  1838. (fmt2 (cadr fmt)))
  1839. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1840. fmt1 label (format-spec fmt2 `((?e . ,category)
  1841. (?n . ,seqno))))))
  1842. (t (error "Unknow %S on label" op))))))))
  1843. ;;;; Links :: Inline Images
  1844. (defun org-e-odt--copy-image-file (path)
  1845. "Returns the internal name of the file"
  1846. (let* ((image-type (file-name-extension path))
  1847. (media-type (format "image/%s" image-type))
  1848. (target-dir "Images/")
  1849. (target-file
  1850. (format "%s%04d.%s" target-dir
  1851. (incf org-e-odt-embedded-images-count) image-type)))
  1852. (message "Embedding %s as %s ..."
  1853. (substring-no-properties path) target-file)
  1854. (when (= 1 org-e-odt-embedded-images-count)
  1855. (make-directory (concat org-e-odt-zip-dir target-dir))
  1856. (org-e-odt-create-manifest-file-entry "" target-dir))
  1857. (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
  1858. (org-e-odt-create-manifest-file-entry media-type target-file)
  1859. target-file))
  1860. (defun org-e-odt--image-size (file &optional user-width
  1861. user-height scale dpi embed-as)
  1862. (let* ((--pixels-to-cms
  1863. (function (lambda (pixels dpi)
  1864. (let ((cms-per-inch 2.54)
  1865. (inches (/ pixels dpi)))
  1866. (* cms-per-inch inches)))))
  1867. (--size-in-cms
  1868. (function
  1869. (lambda (size-in-pixels dpi)
  1870. (and size-in-pixels
  1871. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  1872. (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
  1873. (dpi (or dpi org-e-odt-pixels-per-inch))
  1874. (anchor-type (or embed-as "paragraph"))
  1875. (user-width (and (not scale) user-width))
  1876. (user-height (and (not scale) user-height))
  1877. (size
  1878. (and
  1879. (not (and user-height user-width))
  1880. (or
  1881. ;; Use Imagemagick.
  1882. (and (executable-find "identify")
  1883. (let ((size-in-pixels
  1884. (let ((dim (shell-command-to-string
  1885. (format "identify -format \"%%w:%%h\" \"%s\""
  1886. file))))
  1887. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  1888. (cons (string-to-number (match-string 1 dim))
  1889. (string-to-number (match-string 2 dim)))))))
  1890. (funcall --size-in-cms size-in-pixels dpi)))
  1891. ;; Use Emacs.
  1892. (let ((size-in-pixels
  1893. (ignore-errors ; Emacs could be in batch mode
  1894. (clear-image-cache)
  1895. (image-size (create-image file) 'pixels))))
  1896. (funcall --size-in-cms size-in-pixels dpi))
  1897. ;; Use hard-coded values.
  1898. (cdr (assoc-string anchor-type
  1899. org-e-odt-default-image-sizes-alist))
  1900. ;; Error out.
  1901. (error "Cannot determine image size, aborting"))))
  1902. (width (car size)) (height (cdr size)))
  1903. (cond
  1904. (scale
  1905. (setq width (* width scale) height (* height scale)))
  1906. ((and user-height user-width)
  1907. (setq width user-width height user-height))
  1908. (user-height
  1909. (setq width (* user-height (/ width height)) height user-height))
  1910. (user-width
  1911. (setq height (* user-width (/ height width)) width user-width))
  1912. (t (ignore)))
  1913. ;; ensure that an embedded image fits comfortably within a page
  1914. (let ((max-width (car org-e-odt-max-image-size))
  1915. (max-height (cdr org-e-odt-max-image-size)))
  1916. (when (or (> width max-width) (> height max-height))
  1917. (let* ((scale1 (/ max-width width))
  1918. (scale2 (/ max-height height))
  1919. (scale (min scale1 scale2)))
  1920. (setq width (* scale width) height (* scale height)))))
  1921. (cons width height)))
  1922. (defun org-e-odt-link--inline-image (element info)
  1923. "Return HTML code for an inline image.
  1924. LINK is the link pointing to the inline image. INFO is a plist
  1925. used as a communication channel."
  1926. (assert (eq (org-element-type element) 'link))
  1927. (let* ((src (let* ((type (org-element-property :type element))
  1928. (raw-path (org-element-property :path element)))
  1929. (cond ((member type '("http" "https"))
  1930. (concat type ":" raw-path))
  1931. ((file-name-absolute-p raw-path)
  1932. (expand-file-name raw-path))
  1933. (t raw-path))))
  1934. (src-expanded (if (file-name-absolute-p src) src
  1935. (expand-file-name src (file-name-directory
  1936. (plist-get info :input-file)))))
  1937. (href (format
  1938. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  1939. (org-e-odt--copy-image-file src-expanded)))
  1940. ;; Extract attributes from #+ATTR_ODT line.
  1941. (attr-from (case (org-element-type element)
  1942. (link (org-export-get-parent-element element))
  1943. (t element)))
  1944. ;; Convert attributes to a plist.
  1945. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  1946. ;; Handle `:anchor', `:style' and `:attributes' properties.
  1947. (user-frame-anchor
  1948. (car (assoc-string (plist-get attr-plist :anchor)
  1949. '(("as-char") ("paragraph") ("page")) t)))
  1950. (user-frame-style
  1951. (and user-frame-anchor (plist-get attr-plist :style)))
  1952. (user-frame-attrs
  1953. (and user-frame-anchor (plist-get attr-plist :attributes)))
  1954. (user-frame-params
  1955. (list user-frame-style user-frame-attrs user-frame-anchor))
  1956. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  1957. ;; extrac
  1958. ;; handle `:width', `:height' and `:scale' properties.
  1959. (size (org-e-odt--image-size
  1960. src-expanded (plist-get attr-plist :width)
  1961. (plist-get attr-plist :height)
  1962. (plist-get attr-plist :scale) nil ;; embed-as
  1963. "paragraph" ; FIXME
  1964. ))
  1965. (width (car size)) (height (cdr size))
  1966. (embed-as (if (org-e-odt--standalone-link-p element info) "paragraph"
  1967. "as-char"))
  1968. (captions (org-e-odt-format-label element info 'definition))
  1969. (caption (car captions)) (short-caption (cdr captions))
  1970. (entity (concat (and caption "Captioned") embed-as "Image")))
  1971. (org-e-odt--render-image/formula entity href width height
  1972. captions user-frame-params )))
  1973. ;;;; Links :: Math formula
  1974. (defun org-e-odt-link--inline-formula (element info)
  1975. (let* ((src (let* ((type (org-element-property :type element))
  1976. (raw-path (org-element-property :path element)))
  1977. (cond
  1978. ((file-name-absolute-p raw-path)
  1979. (expand-file-name raw-path))
  1980. (t raw-path))))
  1981. (full-src (if (file-name-absolute-p src) src
  1982. (expand-file-name src (file-name-directory
  1983. (plist-get info :input-file)))))
  1984. (caption-from
  1985. (case (org-element-type element)
  1986. (link (org-export-get-parent-element element))
  1987. (t element)))
  1988. (captions (org-e-odt-format-label caption-from info 'definition))
  1989. (caption (car captions))
  1990. (href
  1991. (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  1992. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  1993. (file-name-directory (org-e-odt--copy-formula-file full-src))))
  1994. (embed-as (if caption 'paragraph 'character))
  1995. width height)
  1996. (cond
  1997. ((eq embed-as 'character)
  1998. (org-e-odt--render-image/formula "InlineFormula" href width height))
  1999. (t
  2000. (let* ((equation (org-e-odt--render-image/formula
  2001. "CaptionedDisplayFormula" href width height captions))
  2002. (label
  2003. (let* ((org-e-odt-category-map-alist
  2004. '(("__MathFormula__" "Text" "math-label" "Equation"
  2005. org-e-odt--enumerable-formula-p))))
  2006. (car (org-e-odt-format-label caption-from info 'definition)))))
  2007. (concat equation "<text:tab/>" label))))))
  2008. (defun org-e-odt--copy-formula-file (src-file)
  2009. "Returns the internal name of the file"
  2010. (let* ((target-dir (format "Formula-%04d/"
  2011. (incf org-e-odt-embedded-formulas-count)))
  2012. (target-file (concat target-dir "content.xml")))
  2013. ;; Create a directory for holding formula file. Also enter it in
  2014. ;; to manifest.
  2015. (make-directory (concat org-e-odt-zip-dir target-dir))
  2016. (org-e-odt-create-manifest-file-entry
  2017. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  2018. ;; Copy over the formula file from user directory to zip
  2019. ;; directory.
  2020. (message "Embedding %s as %s ..." src-file target-file)
  2021. (let ((case-fold-search nil))
  2022. (cond
  2023. ;; Case 1: Mathml.
  2024. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  2025. (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
  2026. ;; Case 2: OpenDocument formula.
  2027. ((string-match "\\.odf\\'" src-file)
  2028. (org-e-odt--zip-extract src-file "content.xml"
  2029. (concat org-e-odt-zip-dir target-dir)))
  2030. (t (error "%s is not a formula file" src-file))))
  2031. ;; Enter the formula file in to manifest.
  2032. (org-e-odt-create-manifest-file-entry "text/xml" target-file)
  2033. target-file))
  2034. ;;;; Targets
  2035. (defun org-e-odt--render-image/formula (cfg-key href width height &optional
  2036. captions user-frame-params)
  2037. (let* ((frame-cfg-alist
  2038. ;; Each element of this alist is of the form (CFG-HANDLE
  2039. ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
  2040. ;; CFG-HANDLE is the key to the alist.
  2041. ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
  2042. ;; frame params for INNER-FRAME and OUTER-FRAME
  2043. ;; respectively. See below.
  2044. ;; Configurations that are meant to be applied to
  2045. ;; non-captioned image/formula specifies no
  2046. ;; OUTER-FRAME-PARAMS.
  2047. ;; TERMINOLOGY
  2048. ;; ===========
  2049. ;; INNER-FRAME :: Frame that directly surrounds an
  2050. ;; image/formula.
  2051. ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
  2052. ;; frame also contains the caption, if any.
  2053. ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
  2054. ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
  2055. ;; that these are the last three arguments
  2056. ;; to `org-e-odt--frame'.
  2057. ;; Note that an un-captioned image/formula requires just an
  2058. ;; INNER-FRAME, while a captioned image/formula requires
  2059. ;; both an INNER and an OUTER-FRAME.
  2060. '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
  2061. ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
  2062. ("PageImage" ("OrgPageImage" nil "page"))
  2063. ("CaptionedAs-CharImage"
  2064. ("OrgCaptionedImage"
  2065. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2066. ("OrgInlineImage" nil "as-char"))
  2067. ("CaptionedParagraphImage"
  2068. ("OrgCaptionedImage"
  2069. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2070. ("OrgImageCaptionFrame" nil "paragraph"))
  2071. ("CaptionedPageImage"
  2072. ("OrgCaptionedImage"
  2073. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2074. ("OrgPageImageCaptionFrame" nil "page"))
  2075. ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
  2076. ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
  2077. ("CaptionedDisplayFormula"
  2078. ("OrgCaptionedFormula" nil "paragraph")
  2079. ("OrgFormulaCaptionFrame" nil "paragraph"))))
  2080. (caption (car captions)) (short-caption (cdr captions))
  2081. ;; Retrieve inner and outer frame params, from configuration.
  2082. (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
  2083. (inner (nth 1 frame-cfg))
  2084. (outer (nth 2 frame-cfg))
  2085. ;; User-specified frame params (from #+ATTR_ODT spec)
  2086. (user user-frame-params)
  2087. (--merge-frame-params (function
  2088. (lambda (default user)
  2089. "Merge default and user frame params."
  2090. (if (not user) default
  2091. (assert (= (length default) 3))
  2092. (assert (= (length user) 3))
  2093. (loop for u in user
  2094. for d in default
  2095. collect (or u d)))))))
  2096. (cond
  2097. ;; Case 1: Image/Formula has no caption.
  2098. ;; There is only one frame, one that surrounds the image
  2099. ;; or formula.
  2100. ((not caption)
  2101. ;; Merge user frame params with that from configuration.
  2102. (setq inner (funcall --merge-frame-params inner user))
  2103. (apply 'org-e-odt--frame href width height inner))
  2104. ;; Case 2: Image/Formula is captioned or labeled.
  2105. ;; There are two frames: The inner one surrounds the
  2106. ;; image or formula. The outer one contains the
  2107. ;; caption/sequence number.
  2108. (t
  2109. ;; Merge user frame params with outer frame params.
  2110. (setq outer (funcall --merge-frame-params outer user))
  2111. ;; Short caption, if specified, goes as part of inner frame.
  2112. (setq inner (let ((frame-params (copy-sequence inner)))
  2113. (setcar (cdr frame-params)
  2114. (concat
  2115. (cadr frame-params)
  2116. (when short-caption
  2117. (format " draw:name=\"%s\" " short-caption))))
  2118. frame-params))
  2119. (apply 'org-e-odt--textbox
  2120. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2121. "Illustration"
  2122. (concat
  2123. (apply 'org-e-odt--frame href width height inner)
  2124. caption))
  2125. width height outer)))))
  2126. (defun org-e-odt--enumerable-p (element info)
  2127. ;; Element should have a caption or label.
  2128. (or (org-element-property :caption element)
  2129. (org-element-property :name element)))
  2130. (defun org-e-odt--enumerable-image-p (element info)
  2131. (org-e-odt--standalone-link-p
  2132. element info
  2133. ;; Paragraph should have a caption or label. It SHOULD NOT be a
  2134. ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
  2135. ;; processing.)
  2136. (lambda (p)
  2137. (and (not (org-element-property :replaces p))
  2138. (or (org-element-property :caption p)
  2139. (org-element-property :name p))))
  2140. ;; Link should point to an image file.
  2141. (lambda (l)
  2142. (assert (eq (org-element-type l) 'link))
  2143. (org-export-inline-image-p l org-e-odt-inline-image-rules))))
  2144. (defun org-e-odt--enumerable-latex-image-p (element info)
  2145. (org-e-odt--standalone-link-p
  2146. element info
  2147. ;; Paragraph should have a caption or label. It SHOULD also be a
  2148. ;; replacement element. (i.e., It SHOULD be a result of LaTeX
  2149. ;; processing.)
  2150. (lambda (p)
  2151. (and (org-element-property :replaces p)
  2152. (or (org-element-property :caption p)
  2153. (org-element-property :name p))))
  2154. ;; Link should point to an image file.
  2155. (lambda (l)
  2156. (assert (eq (org-element-type l) 'link))
  2157. (org-export-inline-image-p l org-e-odt-inline-image-rules))))
  2158. (defun org-e-odt--enumerable-formula-p (element info)
  2159. (org-e-odt--standalone-link-p
  2160. element info
  2161. ;; Paragraph should have a caption or label.
  2162. (lambda (p)
  2163. (or (org-element-property :caption p)
  2164. (org-element-property :name p)))
  2165. ;; Link should point to a MathML or ODF file.
  2166. (lambda (l)
  2167. (assert (eq (org-element-type l) 'link))
  2168. (org-export-inline-image-p l org-e-odt-inline-formula-rules))))
  2169. (defun org-e-odt--standalone-link-p (element info &optional
  2170. paragraph-predicate
  2171. link-predicate)
  2172. "Test if ELEMENT is a standalone link for the purpose ODT export.
  2173. INFO is a plist holding contextual information.
  2174. Return non-nil, if ELEMENT is of type paragraph satisfying
  2175. PARAGRAPH-PREDICATE and it's sole content, save for whitespaces,
  2176. is a link that satisfies LINK-PREDICATE.
  2177. Return non-nil, if ELEMENT is of type link satisfying
  2178. LINK-PREDICATE and it's containing paragraph satisfies
  2179. PARAGRAPH-PREDICATE inaddtion to having no other content save for
  2180. leading and trailing whitespaces.
  2181. Return nil, otherwise."
  2182. (let ((p (case (org-element-type element)
  2183. (paragraph element)
  2184. (link (and (or (not link-predicate)
  2185. (funcall link-predicate element))
  2186. (org-export-get-parent element)))
  2187. (t nil))))
  2188. (when p
  2189. (assert (eq (org-element-type p) 'paragraph))
  2190. (when (or (not paragraph-predicate)
  2191. (funcall paragraph-predicate p))
  2192. (let ((contents (org-element-contents p)))
  2193. (loop for x in contents
  2194. with inline-image-count = 0
  2195. always (case (org-element-type x)
  2196. (plain-text
  2197. (not (org-string-nw-p x)))
  2198. (link
  2199. (and (or (not link-predicate)
  2200. (funcall link-predicate x))
  2201. (= (incf inline-image-count) 1)))
  2202. (t nil))))))))
  2203. (defun org-e-odt-resolve-numbered-paragraph (element info)
  2204. (when (eq (org-element-type element) 'item)
  2205. (let ((el element) ordinal)
  2206. (while (eq (org-element-type el) 'item)
  2207. (push (1+ (length (org-export-get-previous-element el info t))) ordinal)
  2208. (setq el (org-export-get-parent (org-export-get-parent el))))
  2209. ordinal)))
  2210. (defun org-e-odt-link (link desc info)
  2211. "Transcode a LINK object from Org to ODT.
  2212. DESC is the description part of the link, or the empty string.
  2213. INFO is a plist holding contextual information. See
  2214. `org-export-data'."
  2215. (let* ((type (org-element-property :type link))
  2216. (raw-path (org-element-property :path link))
  2217. ;; Ensure DESC really exists, or set it to nil.
  2218. (desc (and (not (string= desc "")) desc))
  2219. (imagep (org-export-inline-image-p
  2220. link org-e-odt-inline-image-rules))
  2221. (path (cond
  2222. ((member type '("http" "https" "ftp" "mailto"))
  2223. (concat type ":" raw-path))
  2224. ((string= type "file")
  2225. (if (file-name-absolute-p raw-path)
  2226. (concat "file://" (expand-file-name raw-path))
  2227. (concat "file://" raw-path)))
  2228. (t raw-path)))
  2229. protocol)
  2230. (cond
  2231. ;; Image file.
  2232. ((and (not desc) (org-export-inline-image-p
  2233. link org-e-odt-inline-image-rules))
  2234. (org-e-odt-link--inline-image link info))
  2235. ;; Formula file.
  2236. ((and (not desc) (org-export-inline-image-p
  2237. link org-e-odt-inline-formula-rules))
  2238. (org-e-odt-link--inline-formula link info))
  2239. ;; Radio target: Transcode target's contents and use them as
  2240. ;; link's description.
  2241. ((string= type "radio")
  2242. (let ((destination (org-export-resolve-radio-link link info)))
  2243. (when destination
  2244. (let ((desc (org-export-data (org-element-contents destination) info))
  2245. (href (org-export-solidify-link-text path)))
  2246. (format
  2247. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2248. href desc)))))
  2249. ;; Links pointing to an headline: Find destination and build
  2250. ;; appropriate referencing command.
  2251. ((member type '("custom-id" "fuzzy" "id"))
  2252. (let ((destination (if (string= type "fuzzy")
  2253. (org-export-resolve-fuzzy-link link info)
  2254. (org-export-resolve-id-link link info))))
  2255. (case (org-element-type destination)
  2256. ;; Fuzzy link points nowhere.
  2257. ('nil
  2258. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2259. "Emphasis" (or desc (org-export-data
  2260. (org-element-property
  2261. :raw-link link) info))))
  2262. ;; Fuzzy link points to an invisible target.
  2263. (keyword nil)
  2264. ;; LINK points to an headline. Check if LINK should display
  2265. ;; section numbers.
  2266. (headline
  2267. (let* ((headline-no (org-export-get-headline-number destination info))
  2268. (label (format "sec-%s" (mapconcat 'number-to-string
  2269. headline-no "-"))))
  2270. (cond
  2271. ;; Case 1: Headline is numbered and LINK has no
  2272. ;; description or LINK's description matches headline's
  2273. ;; title. Display section number.
  2274. ((and (org-export-numbered-headline-p destination info)
  2275. (or (not desc) (string= desc (org-element-property
  2276. :raw-value destination))))
  2277. (format
  2278. "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2279. label (mapconcat 'number-to-string headline-no ".")))
  2280. ;; Case 2: Either the headline is un-numbered or
  2281. ;; LINK has a custom description. Display LINK's
  2282. ;; description or headline's title.
  2283. (t
  2284. (let ((desc (or desc (org-export-data
  2285. (org-element-property :title destination)
  2286. info))))
  2287. (format
  2288. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2289. label desc))))))
  2290. ;; Fuzzy link points to a target. Do as above.
  2291. (target
  2292. ;; Identify nearest meaningful container
  2293. (let ((container
  2294. (loop for parent in (org-export-get-genealogy destination)
  2295. when
  2296. (memq
  2297. (org-element-type parent)
  2298. '(footnote-definition footnote-reference headline item
  2299. table))
  2300. return parent)))
  2301. ;; There is a meaningful container
  2302. (when container
  2303. (case (org-element-type container)
  2304. ;; Container is item
  2305. (item
  2306. (format
  2307. "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2308. (org-export-solidify-link-text path)
  2309. (mapconcat 'number-to-string
  2310. (org-e-odt-resolve-numbered-paragraph
  2311. container info) ".")))))))
  2312. (otherwise
  2313. ;; (unless desc
  2314. ;; (setq number (cond
  2315. ;; ((org-e-odt--standalone-link-p destination info)
  2316. ;; (org-export-get-ordinal
  2317. ;; (assoc 'link (org-element-contents destination))
  2318. ;; info 'link 'org-e-odt--standalone-link-p))
  2319. ;; (t (org-export-get-ordinal destination info))))
  2320. ;; (setq desc (when number
  2321. ;; (if (atom number) (number-to-string number)
  2322. ;; (mapconcat 'number-to-string number ".")))))
  2323. (let ((label-reference
  2324. (org-e-odt-format-label destination info 'reference)))
  2325. (assert label-reference)
  2326. label-reference)))))
  2327. ;; Coderef: replace link with the reference name or the
  2328. ;; equivalent line number.
  2329. ((string= type "coderef")
  2330. (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
  2331. (href (concat "coderef-" path)))
  2332. (format
  2333. (org-export-get-coderef-format path desc)
  2334. (format
  2335. "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2336. href line-no))))
  2337. ;; Link type is handled by a special function.
  2338. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2339. (funcall protocol (org-link-unescape path) desc 'odt))
  2340. ;; External link with a description part.
  2341. ((and path desc)
  2342. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2343. path desc))
  2344. ;; External link without a description part.
  2345. (path
  2346. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2347. path path))
  2348. ;; No path, only description. Try to do something useful.
  2349. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2350. "Emphasis" desc)))))
  2351. ;;;; Paragraph
  2352. (defun org-e-odt-paragraph (paragraph contents info)
  2353. "Transcode a PARAGRAPH element from Org to ODT.
  2354. CONTENTS is the contents of the paragraph, as a string. INFO is
  2355. the plist used as a communication channel."
  2356. (let* ((parent (org-export-get-parent paragraph))
  2357. (parent-type (org-element-type parent))
  2358. (style (case parent-type
  2359. (quote-block "Quotations")
  2360. (center-block "OrgCenter")
  2361. (footnote-definition "Footnote")
  2362. (t (or (org-element-property :style paragraph)
  2363. "Text_20_body")))))
  2364. ;; If this paragraph is a leading paragraph in an item and the
  2365. ;; item has a checkbox, splice the checkbox and paragraph contents
  2366. ;; together.
  2367. (when (and (eq (org-element-type parent) 'item)
  2368. (eq paragraph (car (org-element-contents parent))))
  2369. (setq contents (concat (org-e-odt--checkbox parent) contents)))
  2370. (assert style)
  2371. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2372. ;;;; Plain List
  2373. (defun org-e-odt-plain-list (plain-list contents info)
  2374. "Transcode a PLAIN-LIST element from Org to ODT.
  2375. CONTENTS is the contents of the list. INFO is a plist holding
  2376. contextual information."
  2377. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2378. ;; Choose style based on list type.
  2379. (case (org-element-property :type plain-list)
  2380. (ordered "OrgNumberedList")
  2381. (unordered "OrgBulletedList")
  2382. (descriptive-1 "OrgDescriptionList")
  2383. (descriptive-2 "OrgDescriptionList"))
  2384. ;; If top-level list, re-start numbering. Otherwise,
  2385. ;; continue numbering.
  2386. (format "text:continue-numbering=\"%s\""
  2387. (let* ((parent (org-export-get-parent plain-list)))
  2388. (if (and parent (eq (org-element-type parent) 'item))
  2389. "true" "false")))
  2390. contents))
  2391. ;;;; Plain Text
  2392. (defun org-e-odt--encode-tabs-and-spaces (line)
  2393. (replace-regexp-in-string
  2394. "\\([\t]\\|\\([ ]+\\)\\)"
  2395. (lambda (s)
  2396. (cond
  2397. ((string= s "\t") "<text:tab/>")
  2398. (t (let ((n (length s)))
  2399. (cond
  2400. ((= n 1) " ")
  2401. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2402. (t ""))))))
  2403. line))
  2404. (defun org-e-odt--encode-plain-text (text &optional no-whitespace-filling)
  2405. (mapc
  2406. (lambda (pair)
  2407. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2408. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2409. (if no-whitespace-filling text
  2410. (org-e-odt--encode-tabs-and-spaces text)))
  2411. (defun org-e-odt-plain-text (text info)
  2412. "Transcode a TEXT string from Org to ODT.
  2413. TEXT is the string to transcode. INFO is a plist holding
  2414. contextual information."
  2415. (let ((output text))
  2416. ;; Protect &, < and >.
  2417. (setq output (org-e-odt--encode-plain-text output t))
  2418. ;; Handle smart quotes. Be sure to provide original string since
  2419. ;; OUTPUT may have been modified.
  2420. (setq output (org-export-activate-smart-quotes output :utf-8 info text))
  2421. ;; Convert special strings.
  2422. (when (plist-get info :with-special-strings)
  2423. (mapc
  2424. (lambda (pair)
  2425. (setq output
  2426. (replace-regexp-in-string (car pair) (cdr pair) output t nil)))
  2427. org-e-odt-special-string-regexps))
  2428. ;; Handle break preservation if required.
  2429. (when (plist-get info :preserve-breaks)
  2430. (setq output (replace-regexp-in-string
  2431. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" output t)))
  2432. ;; Return value.
  2433. output))
  2434. ;;;; Planning
  2435. (defun org-e-odt-planning (planning contents info)
  2436. "Transcode a PLANNING element from Org to ODT.
  2437. CONTENTS is nil. INFO is a plist used as a communication
  2438. channel."
  2439. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2440. "OrgPlanning"
  2441. (concat
  2442. (let ((closed (org-element-property :closed planning)))
  2443. (when closed
  2444. (concat
  2445. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2446. "OrgClosedKeyword" org-closed-string)
  2447. (org-e-odt-timestamp closed contents info))))
  2448. (let ((deadline (org-element-property :deadline planning)))
  2449. (when deadline
  2450. (concat
  2451. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2452. "OrgDeadlineKeyword" org-deadline-string)
  2453. (org-e-odt-timestamp deadline contents info))))
  2454. (let ((scheduled (org-element-property :scheduled planning)))
  2455. (when scheduled
  2456. (concat
  2457. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2458. "OrgScheduledKeyword" org-deadline-string)
  2459. (org-e-odt-timestamp scheduled contents info)))))))
  2460. ;;;; Property Drawer
  2461. (defun org-e-odt-property-drawer (property-drawer contents info)
  2462. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2463. CONTENTS is nil. INFO is a plist holding contextual
  2464. information."
  2465. ;; The property drawer isn't exported but we want separating blank
  2466. ;; lines nonetheless.
  2467. "")
  2468. ;;;; Quote Block
  2469. (defun org-e-odt-quote-block (quote-block contents info)
  2470. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2471. CONTENTS holds the contents of the block. INFO is a plist
  2472. holding contextual information."
  2473. contents)
  2474. ;;;; Quote Section
  2475. (defun org-e-odt-quote-section (quote-section contents info)
  2476. "Transcode a QUOTE-SECTION element from Org to ODT.
  2477. CONTENTS is nil. INFO is a plist holding contextual information."
  2478. (let ((value (org-remove-indentation
  2479. (org-element-property :value quote-section))))
  2480. (when value (org-e-odt-do-format-code value))))
  2481. ;;;; Section
  2482. (defun org-e-odt-format-section (text style &optional name)
  2483. (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
  2484. (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
  2485. style
  2486. (format "text:name=\"%s\"" (or name default-name))
  2487. text)))
  2488. (defun org-e-odt-section (section contents info) ; FIXME
  2489. "Transcode a SECTION element from Org to ODT.
  2490. CONTENTS holds the contents of the section. INFO is a plist
  2491. holding contextual information."
  2492. contents)
  2493. ;;;; Radio Target
  2494. (defun org-e-odt-radio-target (radio-target text info)
  2495. "Transcode a RADIO-TARGET object from Org to ODT.
  2496. TEXT is the text of the target. INFO is a plist holding
  2497. contextual information."
  2498. (org-e-odt--target
  2499. text (org-export-solidify-link-text
  2500. (org-element-property :value radio-target))))
  2501. ;;;; Special Block
  2502. (defun org-e-odt-special-block (special-block contents info)
  2503. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2504. CONTENTS holds the contents of the block. INFO is a plist
  2505. holding contextual information."
  2506. (let ((type (downcase (org-element-property :type special-block)))
  2507. (attributes (org-export-read-attribute :attr_odt special-block)))
  2508. (cond
  2509. ;; Annotation.
  2510. ((string= type "annotation")
  2511. (let* ((author (or (plist-get attributes :author)
  2512. (let ((author (plist-get info :author)))
  2513. (and author (org-export-data author info)))))
  2514. (date (or (plist-get attributes :date)
  2515. ;; FIXME: Is `car' right thing to do below?
  2516. (car (plist-get info :date)))))
  2517. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2518. "Text_20_body"
  2519. (format "<office:annotation>\n%s\n</office:annotation>"
  2520. (concat
  2521. (and author
  2522. (format "<dc:creator>%s</dc:creator>" author))
  2523. (and date
  2524. (format "<dc:date>%s</dc:date>"
  2525. (org-e-odt--format-timestamp date nil 'iso-date)))
  2526. contents)))))
  2527. ;; Textbox.
  2528. ((string= type "textbox")
  2529. (let ((width (plist-get attributes :width))
  2530. (height (plist-get attributes :height))
  2531. (style (plist-get attributes :style))
  2532. (extra (plist-get attributes :extra))
  2533. (anchor (plist-get attributes :anchor)))
  2534. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2535. "Text_20_body" (org-e-odt--textbox contents width height
  2536. style extra anchor))))
  2537. (t contents))))
  2538. ;;;; Src Block
  2539. (defun org-e-odt-hfy-face-to-css (fn)
  2540. "Create custom style for face FN.
  2541. When FN is the default face, use it's foreground and background
  2542. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2543. use it's color attribute to create a character style whose name
  2544. is obtained from FN. Currently all attributes of FN other than
  2545. color are ignored.
  2546. The style name for a face FN is derived using the following
  2547. operations on the face name in that order - de-dash, CamelCase
  2548. and prefix with \"OrgSrc\". For example,
  2549. `font-lock-function-name-face' is associated with
  2550. \"OrgSrcFontLockFunctionNameFace\"."
  2551. (let* ((css-list (hfy-face-to-style fn))
  2552. (style-name ((lambda (fn)
  2553. (concat "OrgSrc"
  2554. (mapconcat
  2555. 'capitalize (split-string
  2556. (hfy-face-or-def-to-name fn) "-")
  2557. ""))) fn))
  2558. (color-val (cdr (assoc "color" css-list)))
  2559. (background-color-val (cdr (assoc "background" css-list)))
  2560. (style (and org-e-odt-create-custom-styles-for-srcblocks
  2561. (cond
  2562. ((eq fn 'default)
  2563. (format org-e-odt-src-block-paragraph-format
  2564. background-color-val color-val))
  2565. (t
  2566. (format
  2567. "
  2568. <style:style style:name=\"%s\" style:family=\"text\">
  2569. <style:text-properties fo:color=\"%s\"/>
  2570. </style:style>" style-name color-val))))))
  2571. (cons style-name style)))
  2572. (defun org-e-odt-htmlfontify-string (line)
  2573. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2574. (hfy-html-quote-map '(("\"" "&quot;")
  2575. ("<" "&lt;")
  2576. ("&" "&amp;")
  2577. (">" "&gt;")
  2578. (" " "<text:s/>")
  2579. (" " "<text:tab/>")))
  2580. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2581. (hfy-optimisations-1 (copy-sequence hfy-optimisations))
  2582. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2583. 'body-text-only))
  2584. (hfy-begin-span-handler
  2585. (lambda (style text-block text-id text-begins-block-p)
  2586. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2587. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2588. (with-no-warnings (htmlfontify-string line))))
  2589. (defun org-e-odt-do-format-code
  2590. (code &optional lang refs retain-labels num-start)
  2591. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2592. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2593. (code-lines (org-split-string code "\n"))
  2594. (code-length (length code-lines))
  2595. (use-htmlfontify-p (and (functionp lang-mode)
  2596. org-e-odt-fontify-srcblocks
  2597. (require 'htmlfontify nil t)
  2598. (fboundp 'htmlfontify-string)))
  2599. (code (if (not use-htmlfontify-p) code
  2600. (with-temp-buffer
  2601. (insert code)
  2602. (funcall lang-mode)
  2603. (font-lock-fontify-buffer)
  2604. (buffer-string))))
  2605. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2606. 'org-e-odt--encode-plain-text))
  2607. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2608. "OrgFixedWidthBlock"))
  2609. (i 0))
  2610. (assert (= code-length (length (org-split-string code "\n"))))
  2611. (setq code
  2612. (org-export-format-code
  2613. code
  2614. (lambda (loc line-num ref)
  2615. (setq par-style
  2616. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2617. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2618. (setq loc (funcall fontifier loc))
  2619. (when ref
  2620. (setq loc (org-e-odt--target loc (concat "coderef-" ref))))
  2621. (assert par-style)
  2622. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2623. par-style loc))
  2624. (if (not line-num) loc
  2625. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2626. num-start refs))
  2627. (cond
  2628. ((not num-start) code)
  2629. ((= num-start 0)
  2630. (format
  2631. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2632. " text:continue-numbering=\"false\"" code))
  2633. (t
  2634. (format
  2635. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2636. " text:continue-numbering=\"true\"" code)))))
  2637. (defun org-e-odt-format-code (element info)
  2638. (let* ((lang (org-element-property :language element))
  2639. ;; Extract code and references.
  2640. (code-info (org-export-unravel-code element))
  2641. (code (car code-info))
  2642. (refs (cdr code-info))
  2643. ;; Does the src block contain labels?
  2644. (retain-labels (org-element-property :retain-labels element))
  2645. ;; Does it have line numbers?
  2646. (num-start (case (org-element-property :number-lines element)
  2647. (continued (org-export-get-loc element info))
  2648. (new 0))))
  2649. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2650. (defun org-e-odt-src-block (src-block contents info)
  2651. "Transcode a SRC-BLOCK element from Org to ODT.
  2652. CONTENTS holds the contents of the item. INFO is a plist holding
  2653. contextual information."
  2654. (let* ((lang (org-element-property :language src-block))
  2655. (attributes (org-export-read-attribute :attr_odt src-block))
  2656. (captions (org-e-odt-format-label src-block info 'definition))
  2657. (caption (car captions)) (short-caption (cdr captions)))
  2658. (concat
  2659. (and caption
  2660. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2661. "Listing" caption))
  2662. (let ((--src-block (org-e-odt-format-code src-block info)))
  2663. (if (not (plist-get attributes :textbox)) --src-block
  2664. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2665. "Text_20_body"
  2666. (org-e-odt--textbox --src-block nil nil nil)))))))
  2667. ;;;; Statistics Cookie
  2668. (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
  2669. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2670. CONTENTS is nil. INFO is a plist holding contextual information."
  2671. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2672. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2673. "OrgCode" cookie-value)))
  2674. ;;;; Strike-Through
  2675. (defun org-e-odt-strike-through (strike-through contents info)
  2676. "Transcode STRIKE-THROUGH from Org to ODT.
  2677. CONTENTS is the text with strike-through markup. INFO is a plist
  2678. holding contextual information."
  2679. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2680. "Strikethrough" contents))
  2681. ;;;; Subscript
  2682. (defun org-e-odt-subscript (subscript contents info)
  2683. "Transcode a SUBSCRIPT object from Org to ODT.
  2684. CONTENTS is the contents of the object. INFO is a plist holding
  2685. contextual information."
  2686. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2687. "OrgSubscript" contents))
  2688. ;;;; Superscript
  2689. (defun org-e-odt-superscript (superscript contents info)
  2690. "Transcode a SUPERSCRIPT object from Org to ODT.
  2691. CONTENTS is the contents of the object. INFO is a plist holding
  2692. contextual information."
  2693. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2694. "OrgSuperscript" contents))
  2695. ;;;; Table Cell
  2696. (defun org-e-odt-table-style-spec (element info)
  2697. (let* ((table (org-export-get-parent-table element))
  2698. (table-attributes (org-export-read-attribute :attr_odt table))
  2699. (table-style (plist-get table-attributes :style)))
  2700. (assoc table-style org-e-odt-table-styles)))
  2701. (defun org-e-odt-get-table-cell-styles (table-cell info)
  2702. "Retrieve styles applicable to a table cell.
  2703. R and C are (zero-based) row and column numbers of the table
  2704. cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
  2705. applicable to the current table. It is `nil' if the table is not
  2706. associated with any style attributes.
  2707. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2708. When STYLE-SPEC is nil, style the table cell the conventional way
  2709. - choose cell borders based on row and column groupings and
  2710. choose paragraph alignment based on `org-col-cookies' text
  2711. property. See also
  2712. `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
  2713. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2714. styles congruent with the ODF-1.2 specification."
  2715. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2716. (r (car table-cell-address)) (c (cdr table-cell-address))
  2717. (style-spec (org-e-odt-table-style-spec table-cell info))
  2718. (table-dimensions (org-export-table-dimensions
  2719. (org-export-get-parent-table table-cell)
  2720. info)))
  2721. (when style-spec
  2722. ;; LibreOffice - particularly the Writer - honors neither table
  2723. ;; templates nor custom table-cell styles. Inorder to retain
  2724. ;; inter-operability with LibreOffice, only automatic styles are
  2725. ;; used for styling of table-cells. The current implementation is
  2726. ;; congruent with ODF-1.2 specification and hence is
  2727. ;; future-compatible.
  2728. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2729. ;; which recognizes as many as 16 different cell types - is much
  2730. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2731. ;; by hand.
  2732. (let* ((template-name (nth 1 style-spec))
  2733. (cell-style-selectors (nth 2 style-spec))
  2734. (cell-type
  2735. (cond
  2736. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2737. (= c 0)) "FirstColumn")
  2738. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2739. (= (1+ c) (cdr table-dimensions)))
  2740. "LastColumn")
  2741. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2742. (= r 0)) "FirstRow")
  2743. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2744. (= (1+ r) (car table-dimensions)))
  2745. "LastRow")
  2746. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2747. (= (% r 2) 1)) "EvenRow")
  2748. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2749. (= (% r 2) 0)) "OddRow")
  2750. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2751. (= (% c 2) 1)) "EvenColumn")
  2752. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2753. (= (% c 2) 0)) "OddColumn")
  2754. (t ""))))
  2755. (concat template-name cell-type)))))
  2756. (defun org-e-odt-table-cell (table-cell contents info)
  2757. "Transcode a TABLE-CELL element from Org to ODT.
  2758. CONTENTS is nil. INFO is a plist used as a communication
  2759. channel."
  2760. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2761. (r (car table-cell-address))
  2762. (c (cdr table-cell-address))
  2763. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2764. (table-row (org-export-get-parent table-cell))
  2765. (custom-style-prefix (org-e-odt-get-table-cell-styles
  2766. table-cell info))
  2767. (paragraph-style
  2768. (or
  2769. (and custom-style-prefix
  2770. (format "%sTableParagraph" custom-style-prefix))
  2771. (concat
  2772. (cond
  2773. ((and (= 1 (org-export-table-row-group table-row info))
  2774. (org-export-table-has-header-p
  2775. (org-export-get-parent-table table-row) info))
  2776. "OrgTableHeading")
  2777. ((let* ((table (org-export-get-parent-table table-cell))
  2778. (table-attrs (org-export-read-attribute :attr_odt table))
  2779. (table-header-columns (plist-get table-attrs
  2780. :header-columns)))
  2781. (<= c (cond ((wholenump table-header-columns)
  2782. (- table-header-columns 1))
  2783. (table-header-columns 0)
  2784. (t -1))))
  2785. "OrgTableHeading")
  2786. (t "OrgTableContents"))
  2787. (capitalize (symbol-name (org-export-table-cell-alignment
  2788. table-cell info))))))
  2789. (cell-style-name
  2790. (or
  2791. (and custom-style-prefix (format "%sTableCell"
  2792. custom-style-prefix))
  2793. (concat
  2794. "OrgTblCell"
  2795. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2796. (zerop r)) "T")
  2797. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2798. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2799. (not (zerop c)) ) "L"))))
  2800. (cell-attributes
  2801. (concat
  2802. (format " table:style-name=\"%s\"" cell-style-name)
  2803. (and (> horiz-span 0)
  2804. (format " table:number-columns-spanned=\"%d\""
  2805. (1+ horiz-span))))))
  2806. (unless contents (setq contents ""))
  2807. (concat
  2808. (assert paragraph-style)
  2809. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2810. cell-attributes
  2811. (let ((table-cell-contents (org-element-contents table-cell)))
  2812. (if (memq (org-element-type (car table-cell-contents))
  2813. org-element-all-elements)
  2814. contents
  2815. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2816. paragraph-style contents))))
  2817. (let (s)
  2818. (dotimes (i horiz-span s)
  2819. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2820. "\n")))
  2821. ;;;; Table Row
  2822. (defun org-e-odt-table-row (table-row contents info)
  2823. "Transcode a TABLE-ROW element from Org to ODT.
  2824. CONTENTS is the contents of the row. INFO is a plist used as a
  2825. communication channel."
  2826. ;; Rules are ignored since table separators are deduced from
  2827. ;; borders of the current row.
  2828. (when (eq (org-element-property :type table-row) 'standard)
  2829. (let* ((rowgroup-tags
  2830. (if (and (= 1 (org-export-table-row-group table-row info))
  2831. (org-export-table-has-header-p
  2832. (org-export-get-parent-table table-row) info))
  2833. ;; If the row belongs to the first rowgroup and the
  2834. ;; table has more than one row groups, then this row
  2835. ;; belongs to the header row group.
  2836. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2837. ;; Otherwise, it belongs to non-header row group.
  2838. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2839. (concat
  2840. ;; Does this row begin a rowgroup?
  2841. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2842. (car rowgroup-tags))
  2843. ;; Actual table row
  2844. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2845. ;; Does this row end a rowgroup?
  2846. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2847. (cdr rowgroup-tags))))))
  2848. ;;;; Table
  2849. (defun org-e-odt-table-first-row-data-cells (table info)
  2850. (let ((table-row
  2851. (org-element-map
  2852. table 'table-row
  2853. (lambda (row)
  2854. (unless (eq (org-element-property :type row) 'rule) row))
  2855. info 'first-match))
  2856. (special-column-p (org-export-table-has-special-column-p table)))
  2857. (if (not special-column-p) (org-element-contents table-row)
  2858. (cdr (org-element-contents table-row)))))
  2859. (defun org-e-odt--table (table contents info)
  2860. "Transcode a TABLE element from Org to ODT.
  2861. CONTENTS is the contents of the table. INFO is a plist holding
  2862. contextual information."
  2863. (case (org-element-property :type table)
  2864. ;; Case 1: table.el doesn't support export to OD format. Strip
  2865. ;; such tables from export.
  2866. (table.el
  2867. (prog1 nil
  2868. (message
  2869. (concat
  2870. "(org-e-odt): Found table.el-type table in the source Org file."
  2871. " table.el doesn't support export to ODT format."
  2872. " Stripping the table from export."))))
  2873. ;; Case 2: Native Org tables.
  2874. (otherwise
  2875. (let* ((captions (org-e-odt-format-label table info 'definition))
  2876. (caption (car captions)) (short-caption (cdr captions))
  2877. (attributes (org-export-read-attribute :attr_odt table))
  2878. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  2879. (table-column-specs
  2880. (function
  2881. (lambda (table info)
  2882. (let* ((table-style (or custom-table-style "OrgTable"))
  2883. (column-style (format "%sColumn" table-style)))
  2884. (mapconcat
  2885. (lambda (table-cell)
  2886. (let ((width (1+ (or (org-export-table-cell-width
  2887. table-cell info) 0)))
  2888. (s (format
  2889. "\n<table:table-column table:style-name=\"%s\"/>"
  2890. column-style))
  2891. out)
  2892. (dotimes (i width out) (setq out (concat s out)))))
  2893. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  2894. (concat
  2895. ;; caption.
  2896. (when caption
  2897. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2898. "Table" caption))
  2899. ;; begin table.
  2900. (let* ((automatic-name
  2901. (org-e-odt-add-automatic-style "Table" attributes)))
  2902. (format
  2903. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
  2904. (or short-caption (car automatic-name))
  2905. (or custom-table-style (cdr automatic-name) "OrgTable")))
  2906. ;; column specification.
  2907. (funcall table-column-specs table info)
  2908. ;; actual contents.
  2909. "\n" contents
  2910. ;; end table.
  2911. "</table:table>")))))
  2912. (defun org-e-odt-table (table contents info)
  2913. "Transcode a TABLE element from Org to ODT.
  2914. CONTENTS is the contents of the table. INFO is a plist holding
  2915. contextual information.
  2916. Use `org-e-odt--table' to typeset the table. Handle details
  2917. pertaining to indentation here."
  2918. (let* ((--element-preceded-by-table-p
  2919. (function
  2920. (lambda (element info)
  2921. (loop for el in (org-export-get-previous-element element info t)
  2922. thereis (eq (org-element-type el) 'table)))))
  2923. (--walk-list-genealogy-and-collect-tags
  2924. (function
  2925. (lambda (table info)
  2926. (let* ((genealogy (org-export-get-genealogy table))
  2927. (list-genealogy
  2928. (when (eq (org-element-type (car genealogy)) 'item)
  2929. (loop for el in genealogy
  2930. when (memq (org-element-type el)
  2931. '(item plain-list))
  2932. collect el)))
  2933. (llh-genealogy
  2934. (apply 'nconc
  2935. (loop for el in genealogy
  2936. when (and (eq (org-element-type el) 'headline)
  2937. (org-export-low-level-p el info))
  2938. collect
  2939. (list el
  2940. (assq 'headline
  2941. (org-element-contents
  2942. (org-export-get-parent el)))))))
  2943. parent-list)
  2944. (nconc
  2945. ;; Handle list genealogy.
  2946. (loop for el in list-genealogy collect
  2947. (case (org-element-type el)
  2948. (plain-list
  2949. (setq parent-list el)
  2950. (cons "</text:list>"
  2951. (format "\n<text:list text:style-name=\"%s\" %s>"
  2952. (case (org-element-property :type el)
  2953. (ordered "OrgNumberedList")
  2954. (unordered "OrgBulletedList")
  2955. (descriptive-1 "OrgDescriptionList")
  2956. (descriptive-2 "OrgDescriptionList"))
  2957. "text:continue-numbering=\"true\"")))
  2958. (item
  2959. (cond
  2960. ((not parent-list)
  2961. (if (funcall --element-preceded-by-table-p table info)
  2962. '("</text:list-header>" . "<text:list-header>")
  2963. '("</text:list-item>" . "<text:list-header>")))
  2964. ((funcall --element-preceded-by-table-p
  2965. parent-list info)
  2966. '("</text:list-header>" . "<text:list-header>"))
  2967. (t '("</text:list-item>" . "<text:list-item>"))))))
  2968. ;; Handle low-level headlines.
  2969. (loop for el in llh-genealogy
  2970. with step = 'item collect
  2971. (case step
  2972. (plain-list
  2973. (setq step 'item) ; Flip-flop
  2974. (setq parent-list el)
  2975. (cons "</text:list>"
  2976. (format "\n<text:list text:style-name=\"%s\" %s>"
  2977. (if (org-export-numbered-headline-p
  2978. el info)
  2979. "OrgNumberedList"
  2980. "OrgBulletedList")
  2981. "text:continue-numbering=\"true\"")))
  2982. (item
  2983. (setq step 'plain-list) ; Flip-flop
  2984. (cond
  2985. ((not parent-list)
  2986. (if (funcall --element-preceded-by-table-p table info)
  2987. '("</text:list-header>" . "<text:list-header>")
  2988. '("</text:list-item>" . "<text:list-header>")))
  2989. ((let ((section? (org-export-get-previous-element
  2990. parent-list info)))
  2991. (and section?
  2992. (eq (org-element-type section?) 'section)
  2993. (assq 'table (org-element-contents section?))))
  2994. '("</text:list-header>" . "<text:list-header>"))
  2995. (t
  2996. '("</text:list-item>" . "<text:list-item>")))))))))))
  2997. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  2998. table info)))
  2999. ;; OpenDocument schema does not permit table to occur within a
  3000. ;; list item.
  3001. ;; One solution - the easiest and lightweight, in terms of
  3002. ;; implementation - is to put the table in an indented text box
  3003. ;; and make the text box part of the list-item. Unfortunately if
  3004. ;; the table is big and spans multiple pages, the text box could
  3005. ;; overflow. In this case, the following attribute will come
  3006. ;; handy.
  3007. ;; ,---- From OpenDocument-v1.1.pdf
  3008. ;; | 15.27.28 Overflow behavior
  3009. ;; |
  3010. ;; | For text boxes contained within text document, the
  3011. ;; | style:overflow-behavior property specifies the behavior of text
  3012. ;; | boxes where the containing text does not fit into the text
  3013. ;; | box.
  3014. ;; |
  3015. ;; | If the attribute's value is clip, the text that does not fit
  3016. ;; | into the text box is not displayed.
  3017. ;; |
  3018. ;; | If the attribute value is auto-create-new-frame, a new frame
  3019. ;; | will be created on the next page, with the same position and
  3020. ;; | dimensions of the original frame.
  3021. ;; |
  3022. ;; | If the style:overflow-behavior property's value is
  3023. ;; | auto-create-new-frame and the text box has a minimum width or
  3024. ;; | height specified, then the text box will grow until the page
  3025. ;; | bounds are reached before a new frame is created.
  3026. ;; `----
  3027. ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
  3028. ;; auto-create-new-frame property and always resorts to clipping
  3029. ;; the text box. This results in table being truncated.
  3030. ;; So we solve the problem the hard (and fun) way using list
  3031. ;; continuations.
  3032. ;; The problem only becomes more interesting if you take in to
  3033. ;; account the following facts:
  3034. ;;
  3035. ;; - Description lists are simulated as plain lists.
  3036. ;; - Low-level headlines can be listified.
  3037. ;; - In Org-mode, a table can occur not only as a regular list
  3038. ;; item, but also within description lists and low-level
  3039. ;; headlines.
  3040. ;; See `org-e-odt-translate-description-lists' and
  3041. ;; `org-e-odt-translate-low-level-headlines' for how this is
  3042. ;; tackled.
  3043. (concat "\n"
  3044. ;; Discontinue the list.
  3045. (mapconcat 'car close-open-tags "\n")
  3046. ;; Put the table in an indented section.
  3047. (let* ((table (org-e-odt--table table contents info))
  3048. (level (/ (length (mapcar 'car close-open-tags)) 2))
  3049. (style (format "OrgIndentedSection-Level-%d" level)))
  3050. (when table (org-e-odt-format-section table style)))
  3051. ;; Continue the list.
  3052. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  3053. ;;;; Target
  3054. (defun org-e-odt-target (target contents info)
  3055. "Transcode a TARGET object from Org to ODT.
  3056. CONTENTS is nil. INFO is a plist holding contextual
  3057. information."
  3058. (let ((value (org-element-property :value target)))
  3059. (org-e-odt--target "" (org-export-solidify-link-text value))))
  3060. ;;;; Timestamp
  3061. (defun org-e-odt-timestamp (timestamp contents info)
  3062. "Transcode a TIMESTAMP object from Org to ODT.
  3063. CONTENTS is nil. INFO is a plist used as a communication
  3064. channel."
  3065. (let* ((raw-value (org-element-property :raw-value timestamp))
  3066. (type (org-element-property :type timestamp)))
  3067. (if (not org-e-odt-use-date-fields)
  3068. (let ((value (org-e-odt-plain-text
  3069. (org-export-translate-timestamp timestamp) info)))
  3070. (case (org-element-property :type timestamp)
  3071. ((active active-range)
  3072. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3073. "OrgActiveTimestamp" value))
  3074. ((inactive inactive-range)
  3075. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3076. "OrgInactiveTimestamp" value))
  3077. (otherwise value)))
  3078. (case type
  3079. (active
  3080. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3081. "OrgActiveTimestamp"
  3082. (format "&lt;%s&gt;" (org-e-odt--format-timestamp timestamp))))
  3083. (inactive
  3084. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3085. "OrgInactiveTimestamp"
  3086. (format "[%s]" (org-e-odt--format-timestamp timestamp))))
  3087. (active-range
  3088. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3089. "OrgActiveTimestamp"
  3090. (format "&lt;%s&gt;&#x2013;&lt;%s&gt;"
  3091. (org-e-odt--format-timestamp timestamp)
  3092. (org-e-odt--format-timestamp timestamp 'end))))
  3093. (inactive-range
  3094. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3095. "OrgInactiveTimestamp"
  3096. (format "[%s]&#x2013;[%s]"
  3097. (org-e-odt--format-timestamp timestamp)
  3098. (org-e-odt--format-timestamp timestamp 'end))))
  3099. (otherwise
  3100. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3101. "OrgDiaryTimestamp"
  3102. (org-e-odt-plain-text (org-export-translate-timestamp
  3103. timestamp) info)))))))
  3104. ;;;; Underline
  3105. (defun org-e-odt-underline (underline contents info)
  3106. "Transcode UNDERLINE from Org to ODT.
  3107. CONTENTS is the text with underline markup. INFO is a plist
  3108. holding contextual information."
  3109. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3110. "Underline" contents))
  3111. ;;;; Verbatim
  3112. (defun org-e-odt-verbatim (verbatim contents info)
  3113. "Transcode a VERBATIM object from Org to ODT.
  3114. CONTENTS is nil. INFO is a plist used as a communication
  3115. channel."
  3116. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3117. "OrgCode" (org-element-property :value verbatim)))
  3118. ;;;; Verse Block
  3119. (defun org-e-odt-verse-block (verse-block contents info)
  3120. "Transcode a VERSE-BLOCK element from Org to ODT.
  3121. CONTENTS is verse block contents. INFO is a plist holding
  3122. contextual information."
  3123. ;; Add line breaks to each line of verse.
  3124. (setq contents (replace-regexp-in-string
  3125. "\\(<text:line-break/>\\)?[ \t]*\n"
  3126. "<text:line-break/>" contents))
  3127. ;; Replace tabs and spaces.
  3128. (setq contents (org-e-odt--encode-tabs-and-spaces contents))
  3129. ;; Surround it in a verse environment.
  3130. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3131. "OrgVerse" contents))
  3132. ;;; Filters
  3133. ;;;; LaTeX fragments
  3134. (defun org-e-odt--translate-latex-fragments (tree backend info)
  3135. (let ((processing-type (plist-get info :LaTeX-fragments))
  3136. (count 0))
  3137. ;; Normalize processing-type to one of dvipng, mathml or verbatim.
  3138. ;; If the desired converter is not available, force verbatim
  3139. ;; processing.
  3140. (case processing-type
  3141. ((t mathml)
  3142. (if (and (fboundp 'org-format-latex-mathml-available-p)
  3143. (org-format-latex-mathml-available-p))
  3144. (setq processing-type 'mathml)
  3145. (message "LaTeX to MathML converter not available.")
  3146. (setq processing-type 'verbatim)))
  3147. (dvipng
  3148. (unless (and (org-check-external-command "latex" "" t)
  3149. (org-check-external-command "dvipng" "" t))
  3150. (message "LaTeX to PNG converter not available.")
  3151. (setq processing-type 'verbatim)))
  3152. (otherwise
  3153. (message "Unknown LaTeX option. Forcing verbatim.")
  3154. (setq processing-type 'verbatim)))
  3155. ;; Store normalized value for later use.
  3156. (when (plist-get info :LaTeX-fragments)
  3157. (plist-put info :LaTeX-fragments processing-type))
  3158. (message "Formatting LaTeX using %s" processing-type)
  3159. ;; Convert `latex-fragment's and `latex-environment's.
  3160. (when (memq processing-type '(mathml dvipng))
  3161. (org-element-map
  3162. tree '(latex-fragment latex-environment)
  3163. (lambda (latex-*)
  3164. (incf count)
  3165. (let* ((latex-frag (org-element-property :value latex-*))
  3166. (input-file (plist-get info :input-file))
  3167. (cache-dir (file-name-directory input-file))
  3168. (cache-subdir (concat
  3169. (case processing-type
  3170. (dvipng "ltxpng/")
  3171. (mathml "ltxmathml/"))
  3172. (file-name-sans-extension
  3173. (file-name-nondirectory input-file))))
  3174. (display-msg
  3175. (case processing-type
  3176. (dvipng (format "Creating LaTeX Image %d..." count))
  3177. (mathml (format "Creating MathML snippet %d..." count))))
  3178. ;; Get an Org-style link to PNG image or the MathML
  3179. ;; file.
  3180. (org-link
  3181. (let ((link (with-temp-buffer
  3182. (insert latex-frag)
  3183. (org-format-latex cache-subdir cache-dir
  3184. nil display-msg
  3185. nil nil processing-type)
  3186. (buffer-substring-no-properties
  3187. (point-min) (point-max)))))
  3188. (if (not (string-match "file:\\([^]]*\\)" link))
  3189. (prog1 nil (message "LaTeX Conversion failed."))
  3190. link))))
  3191. (when org-link
  3192. ;; Conversion succeeded. Parse above Org-style link to a
  3193. ;; `link' object.
  3194. (let* ((link (car (org-element-map (with-temp-buffer
  3195. (org-mode)
  3196. (insert org-link)
  3197. (org-element-parse-buffer))
  3198. 'link 'identity))))
  3199. ;; Orphan the link.
  3200. (org-element-put-property link :parent nil)
  3201. (let* (
  3202. (replacement
  3203. (case (org-element-type latex-*)
  3204. ;; Case 1: LaTeX environment.
  3205. ;; Mimic a "standalone image or formula" by
  3206. ;; enclosing the `link' in a `paragraph'.
  3207. ;; Copy over original attributes, captions to
  3208. ;; the enclosing paragraph.
  3209. (latex-environment
  3210. (org-element-adopt-elements
  3211. (list 'paragraph
  3212. (list :style "OrgFormula"
  3213. :name (org-element-property :name
  3214. latex-*)
  3215. :caption (org-element-property :caption
  3216. latex-*)))
  3217. link))
  3218. ;; Case 2: LaTeX fragment.
  3219. ;; No special action.
  3220. (latex-fragment link))))
  3221. ;; Note down the object that link replaces.
  3222. (org-element-put-property replacement :replaces
  3223. (list (org-element-type latex-*)
  3224. (list :value latex-frag)))
  3225. ;; Replace now.
  3226. (org-element-set-element latex-* replacement))))))
  3227. info)))
  3228. tree)
  3229. ;;;; Description lists
  3230. ;; This translator is necessary to handle indented tables in a uniform
  3231. ;; manner. See comment in `org-e-odt--table'.
  3232. (defun org-e-odt--translate-description-lists (tree backend info)
  3233. ;; OpenDocument has no notion of a description list. So simulate it
  3234. ;; using plain lists. Description lists in the exported document
  3235. ;; are typeset in the same manner as they are in a typical HTML
  3236. ;; document.
  3237. ;;
  3238. ;; Specifically, a description list like this:
  3239. ;;
  3240. ;; ,----
  3241. ;; | - term-1 :: definition-1
  3242. ;; | - term-2 :: definition-2
  3243. ;; `----
  3244. ;;
  3245. ;; gets translated in to the following form:
  3246. ;;
  3247. ;; ,----
  3248. ;; | - term-1
  3249. ;; | - definition-1
  3250. ;; | - term-2
  3251. ;; | - definition-2
  3252. ;; `----
  3253. ;;
  3254. ;; Further effect is achieved by fixing the OD styles as below:
  3255. ;;
  3256. ;; 1. Set the :type property of the simulated lists to
  3257. ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
  3258. ;; that has *no* bullets whatsoever.
  3259. ;;
  3260. ;; 2. The paragraph containing the definition term is styled to be
  3261. ;; in bold.
  3262. ;;
  3263. (org-element-map
  3264. tree 'plain-list
  3265. (lambda (el)
  3266. (when (equal (org-element-property :type el) 'descriptive)
  3267. (org-element-set-element
  3268. el
  3269. (apply 'org-element-adopt-elements
  3270. (list 'plain-list (list :type 'descriptive-1))
  3271. (mapcar
  3272. (lambda (item)
  3273. (org-element-adopt-elements
  3274. (list 'item (list :checkbox (org-element-property
  3275. :checkbox item)))
  3276. (list 'paragraph (list :style "Text_20_body_20_bold")
  3277. (or (org-element-property :tag item) "(no term)"))
  3278. (org-element-adopt-elements
  3279. (list 'plain-list (list :type 'descriptive-2))
  3280. (apply 'org-element-adopt-elements
  3281. (list 'item nil)
  3282. (org-element-contents item)))))
  3283. (org-element-contents el)))))
  3284. nil)
  3285. info)
  3286. tree)
  3287. ;;;; List tables
  3288. ;; Lists that are marked with attribute `:list-table' are called as
  3289. ;; list tables. They will be rendered as a table within the exported
  3290. ;; document.
  3291. ;; Consider an example. The following list table
  3292. ;;
  3293. ;; #+attr_odt :list-table t
  3294. ;; - Row 1
  3295. ;; - 1.1
  3296. ;; - 1.2
  3297. ;; - 1.3
  3298. ;; - Row 2
  3299. ;; - 2.1
  3300. ;; - 2.2
  3301. ;; - 2.3
  3302. ;;
  3303. ;; will be exported as though it were an Org table like the one show
  3304. ;; below.
  3305. ;;
  3306. ;; | Row 1 | 1.1 | 1.2 | 1.3 |
  3307. ;; | Row 2 | 2.1 | 2.2 | 2.3 |
  3308. ;;
  3309. ;; Note that org-tables are NOT multi-line and each line is mapped to
  3310. ;; a unique row in the exported document. So if an exported table
  3311. ;; needs to contain a single paragraph (with copious text) it needs to
  3312. ;; be typed up in a single line. Editing such long lines using the
  3313. ;; table editor will be a cumbersome task. Furthermore inclusion of
  3314. ;; multi-paragraph text in a table cell is well-nigh impossible.
  3315. ;;
  3316. ;; A LIST-TABLE circumvents above problems.
  3317. ;;
  3318. ;; Note that in the example above the list items could be paragraphs
  3319. ;; themselves and the list can be arbitrarily deep.
  3320. ;;
  3321. ;; Inspired by following thread:
  3322. ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
  3323. ;; Translate lists to tables
  3324. (defun org-e-odt--translate-list-tables (tree backend info)
  3325. (org-element-map
  3326. tree 'plain-list
  3327. (lambda (l1-list)
  3328. (when (org-export-read-attribute :attr_odt l1-list :list-table)
  3329. ;; Replace list with table.
  3330. (org-element-set-element
  3331. l1-list
  3332. ;; Build replacement table.
  3333. (apply 'org-element-adopt-elements
  3334. (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
  3335. (org-element-map
  3336. l1-list
  3337. 'item
  3338. (lambda (l1-item)
  3339. (let* ((l1-item-contents (org-element-contents l1-item))
  3340. l1-item-leading-text l2-list)
  3341. ;; Remove Level-2 list from the Level-item. It
  3342. ;; will be subsequently attached as table-cells.
  3343. (let ((cur l1-item-contents) prev)
  3344. (while (and cur (not (eq (org-element-type (car cur))
  3345. 'plain-list)))
  3346. (setq prev cur)
  3347. (setq cur (cdr cur)))
  3348. (when prev
  3349. (setcdr prev nil)
  3350. (setq l2-list (car cur)))
  3351. (setq l1-item-leading-text l1-item-contents))
  3352. ;; Level-1 items start a table row.
  3353. (apply 'org-element-adopt-elements
  3354. (list 'table-row (list :type 'standard))
  3355. ;; Leading text of level-1 item define the
  3356. ;; first table-cell.
  3357. (apply 'org-element-adopt-elements
  3358. (list 'table-cell nil)
  3359. l1-item-leading-text)
  3360. ;; Level-2 items define subsequent
  3361. ;; table-cells of the row.
  3362. (org-element-map
  3363. l2-list
  3364. 'item
  3365. (lambda (l2-item)
  3366. (apply 'org-element-adopt-elements
  3367. (list 'table-cell nil)
  3368. (org-element-contents l2-item)))
  3369. info nil 'item))))
  3370. info nil 'item))))
  3371. nil)
  3372. info)
  3373. tree)
  3374. ;;; Interactive functions
  3375. (defun org-e-odt-create-manifest-file-entry (&rest args)
  3376. (push args org-e-odt-manifest-file-entries))
  3377. (defun org-e-odt-write-manifest-file ()
  3378. (make-directory (concat org-e-odt-zip-dir "META-INF"))
  3379. (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
  3380. (with-current-buffer
  3381. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3382. (find-file-noselect manifest-file t))
  3383. (insert
  3384. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  3385. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  3386. (mapc
  3387. (lambda (file-entry)
  3388. (let* ((version (nth 2 file-entry))
  3389. (extra (if (not version) ""
  3390. (format " manifest:version=\"%s\"" version))))
  3391. (insert
  3392. (format org-e-odt-manifest-file-entry-tag
  3393. (nth 0 file-entry) (nth 1 file-entry) extra))))
  3394. org-e-odt-manifest-file-entries)
  3395. (insert "\n</manifest:manifest>"))))
  3396. (defmacro org-e-odt--export-wrap (out-file &rest body)
  3397. `(let* ((--out-file ,out-file)
  3398. (out-file-type (file-name-extension --out-file))
  3399. (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  3400. "meta.xml" "styles.xml"))
  3401. ;; Initialize temporary workarea. All files that end up in
  3402. ;; the exported document get parked/created here.
  3403. (org-e-odt-zip-dir (file-name-as-directory
  3404. (make-temp-file (format "%s-" out-file-type) t)))
  3405. (org-e-odt-manifest-file-entries nil)
  3406. (--cleanup-xml-buffers
  3407. (function
  3408. (lambda nil
  3409. ;; Kill all XML buffers.
  3410. (mapc (lambda (file)
  3411. (let ((buf (find-buffer-visiting
  3412. (concat org-e-odt-zip-dir file))))
  3413. (when buf
  3414. (set-buffer-modified-p nil)
  3415. (kill-buffer buf))))
  3416. org-e-odt-xml-files)
  3417. ;; Delete temporary directory and also other embedded
  3418. ;; files that get copied there.
  3419. (delete-directory org-e-odt-zip-dir t)))))
  3420. (org-condition-case-unless-debug err
  3421. (progn
  3422. (unless (executable-find "zip")
  3423. ;; Not at all OSes ship with zip by default
  3424. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3425. ;; Do export. This creates a bunch of xml files ready to be
  3426. ;; saved and zipped.
  3427. (progn ,@body)
  3428. ;; Create a manifest entry for content.xml.
  3429. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  3430. ;; Write mimetype file
  3431. (let* ((mimetypes
  3432. '(("odt" . "application/vnd.oasis.opendocument.text")
  3433. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3434. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3435. (unless mimetype
  3436. (error "Unknown OpenDocument backend %S" out-file-type))
  3437. (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
  3438. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3439. ;; Write out the manifest entries before zipping
  3440. (org-e-odt-write-manifest-file)
  3441. ;; Save all XML files.
  3442. (mapc (lambda (file)
  3443. (let ((buf (find-buffer-visiting
  3444. (concat org-e-odt-zip-dir file))))
  3445. (when buf
  3446. (with-current-buffer buf
  3447. ;; Prettify output if needed.
  3448. (when org-e-odt-prettify-xml
  3449. (indent-region (point-min) (point-max)))
  3450. (save-buffer 0)))))
  3451. org-e-odt-xml-files)
  3452. ;; Run zip.
  3453. (let* ((target --out-file)
  3454. (target-name (file-name-nondirectory target))
  3455. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3456. ("zip" "-rmTq" ,target-name "."))))
  3457. ;; If a file with same name as the desired output file
  3458. ;; exists, remove it.
  3459. (when (file-exists-p target)
  3460. (delete-file target))
  3461. ;; Zip up the xml files.
  3462. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3463. (message "Creating ODT file...")
  3464. ;; Switch temporarily to content.xml. This way Zip
  3465. ;; process will inherit `org-e-odt-zip-dir' as the current
  3466. ;; directory.
  3467. (with-current-buffer
  3468. (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
  3469. (mapc
  3470. (lambda (cmd)
  3471. (message "Running %s" (mapconcat 'identity cmd " "))
  3472. (setq err-string
  3473. (with-output-to-string
  3474. (setq exitcode
  3475. (apply 'call-process (car cmd)
  3476. nil standard-output nil (cdr cmd)))))
  3477. (or (zerop exitcode)
  3478. (error (concat "Unable to create OpenDocument file."
  3479. (format " Zip failed with error (%s)"
  3480. err-string)))))
  3481. cmds)))
  3482. ;; Move the zip file from temporary work directory to
  3483. ;; user-mandated location.
  3484. (rename-file (concat org-e-odt-zip-dir target-name) target)
  3485. (message "Created %s" (expand-file-name target))
  3486. ;; Cleanup work directory and work files.
  3487. (funcall --cleanup-xml-buffers)
  3488. ;; Open the OpenDocument file in archive-mode for
  3489. ;; examination.
  3490. (find-file-noselect target t)
  3491. ;; Return exported file.
  3492. (cond
  3493. ;; Case 1: Conversion desired on exported file. Run the
  3494. ;; converter on the OpenDocument file. Return the
  3495. ;; converted file.
  3496. (org-e-odt-preferred-output-format
  3497. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3498. target))
  3499. ;; Case 2: No further conversion. Return exported
  3500. ;; OpenDocument file.
  3501. (t target))))
  3502. (error
  3503. ;; Cleanup work directory and work files.
  3504. (funcall --cleanup-xml-buffers)
  3505. (message "OpenDocument export failed: %s"
  3506. (error-message-string err))))))
  3507. ;;;; Export to OpenDocument formula
  3508. ;;;###autoload
  3509. (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
  3510. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3511. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3512. MathML. When invoked as an interactive command, use
  3513. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3514. region. If no LaTeX fragments are found, prompt for it. Push
  3515. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  3516. non-nil."
  3517. (interactive
  3518. `(,(let (frag)
  3519. (setq frag (and (setq frag (and (region-active-p)
  3520. (buffer-substring (region-beginning)
  3521. (region-end))))
  3522. (loop for e in org-latex-regexps
  3523. thereis (when (string-match (nth 1 e) frag)
  3524. (match-string (nth 2 e) frag)))))
  3525. (read-string "LaTeX Fragment: " frag nil frag))
  3526. ,(let ((odf-filename (expand-file-name
  3527. (concat
  3528. (file-name-sans-extension
  3529. (or (file-name-nondirectory buffer-file-name)))
  3530. "." "odf")
  3531. (file-name-directory buffer-file-name))))
  3532. (read-file-name "ODF filename: " nil odf-filename nil
  3533. (file-name-nondirectory odf-filename)))))
  3534. (let ((filename (or odf-file
  3535. (expand-file-name
  3536. (concat
  3537. (file-name-sans-extension
  3538. (or (file-name-nondirectory buffer-file-name)))
  3539. "." "odf")
  3540. (file-name-directory buffer-file-name)))))
  3541. (org-e-odt--export-wrap
  3542. filename
  3543. (let* ((buffer (progn
  3544. (require 'nxml-mode)
  3545. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3546. (find-file-noselect (concat org-e-odt-zip-dir
  3547. "content.xml") t))))
  3548. (coding-system-for-write 'utf-8)
  3549. (save-buffer-coding-system 'utf-8))
  3550. (set-buffer buffer)
  3551. (set-buffer-file-coding-system coding-system-for-write)
  3552. (let ((mathml (org-create-math-formula latex-frag)))
  3553. (unless mathml (error "No Math formula created"))
  3554. (insert mathml)
  3555. ;; Add MathML to kill ring, if needed.
  3556. (when org-export-copy-to-kill-ring
  3557. (org-kill-new (buffer-string))))))))
  3558. ;;;###autoload
  3559. (defun org-e-odt-export-as-odf-and-open ()
  3560. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3561. Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3562. formula file."
  3563. (interactive)
  3564. (org-open-file (call-interactively 'org-e-odt-export-as-odf) 'system))
  3565. ;;;; Export to OpenDocument Text
  3566. ;;;###autoload
  3567. (defun org-e-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
  3568. "Export current buffer to a HTML file.
  3569. If narrowing is active in the current buffer, only export its
  3570. narrowed part.
  3571. If a region is active, export that region.
  3572. A non-nil optional argument ASYNC means the process should happen
  3573. asynchronously. The resulting file should be accessible through
  3574. the `org-export-stack' interface.
  3575. When optional argument SUBTREEP is non-nil, export the sub-tree
  3576. at point, extracting information from the headline properties
  3577. first.
  3578. When optional argument VISIBLE-ONLY is non-nil, don't export
  3579. contents of hidden elements.
  3580. EXT-PLIST, when provided, is a property list with external
  3581. parameters overriding Org default settings, but still inferior to
  3582. file-local settings.
  3583. Return output file's name."
  3584. (interactive)
  3585. (let ((outfile (org-export-output-file-name ".odt" subtreep)))
  3586. (if async
  3587. (org-export-async-start (lambda (f) (org-export-add-to-stack f 'e-odt))
  3588. `(expand-file-name
  3589. (org-e-odt--export-wrap
  3590. ,outfile
  3591. (let* ((org-e-odt-embedded-images-count 0)
  3592. (org-e-odt-embedded-formulas-count 0)
  3593. (org-e-odt-automatic-styles nil)
  3594. (org-e-odt-object-counters nil)
  3595. ;; Let `htmlfontify' know that we are interested in
  3596. ;; collecting styles.
  3597. (hfy-user-sheet-assoc nil))
  3598. ;; Initialize content.xml and kick-off the export
  3599. ;; process.
  3600. (let ((out-buf
  3601. (progn
  3602. (require 'nxml-mode)
  3603. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3604. (find-file-noselect
  3605. (concat org-e-odt-zip-dir "content.xml") t)))))
  3606. (org-export-to-buffer
  3607. 'e-odt out-buf ,subtreep ,visible-only nil ',ext-plist))))))
  3608. (org-e-odt--export-wrap
  3609. outfile
  3610. (let* ((org-e-odt-embedded-images-count 0)
  3611. (org-e-odt-embedded-formulas-count 0)
  3612. (org-e-odt-automatic-styles nil)
  3613. (org-e-odt-object-counters nil)
  3614. ;; Let `htmlfontify' know that we are interested in collecting
  3615. ;; styles.
  3616. (hfy-user-sheet-assoc nil))
  3617. ;; Initialize content.xml and kick-off the export process.
  3618. (let ((out-buf (progn
  3619. (require 'nxml-mode)
  3620. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3621. (find-file-noselect
  3622. (concat org-e-odt-zip-dir "content.xml") t)))))
  3623. (org-export-to-buffer
  3624. 'e-odt out-buf subtreep visible-only nil ext-plist)))))))
  3625. ;;;; Convert between OpenDocument and other formats
  3626. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3627. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3628. (catch 'done
  3629. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3630. (dolist (e reachable-formats)
  3631. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3632. (when out-fmt-spec
  3633. (throw 'done (cons (car e) out-fmt-spec))))))))
  3634. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3635. "Workhorse routine for `org-e-odt-convert'."
  3636. (require 'browse-url)
  3637. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3638. (dummy (or (file-readable-p in-file)
  3639. (error "Cannot read %s" in-file)))
  3640. (in-fmt (file-name-extension in-file))
  3641. (out-fmt (or out-fmt (error "Output format unspecified")))
  3642. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3643. (error "Cannot convert from %s format to %s format?"
  3644. in-fmt out-fmt)))
  3645. (convert-process (car how))
  3646. (out-file (concat (file-name-sans-extension in-file) "."
  3647. (nth 1 (or (cdr how) out-fmt))))
  3648. (extra-options (or (nth 2 (cdr how)) ""))
  3649. (out-dir (file-name-directory in-file))
  3650. (cmd (format-spec convert-process
  3651. `((?i . ,(shell-quote-argument in-file))
  3652. (?I . ,(browse-url-file-url in-file))
  3653. (?f . ,out-fmt)
  3654. (?o . ,out-file)
  3655. (?O . ,(browse-url-file-url out-file))
  3656. (?d . , (shell-quote-argument out-dir))
  3657. (?D . ,(browse-url-file-url out-dir))
  3658. (?x . ,extra-options)))))
  3659. (when (file-exists-p out-file)
  3660. (delete-file out-file))
  3661. (message "Executing %s" cmd)
  3662. (let ((cmd-output (shell-command-to-string cmd)))
  3663. (message "%s" cmd-output))
  3664. (cond
  3665. ((file-exists-p out-file)
  3666. (message "Exported to %s" out-file)
  3667. (when prefix-arg
  3668. (message "Opening %s..." out-file)
  3669. (org-open-file out-file 'system))
  3670. out-file)
  3671. (t
  3672. (message "Export to %s failed" out-file)
  3673. nil))))
  3674. (defun org-e-odt-do-reachable-formats (in-fmt)
  3675. "Return verbose info about formats to which IN-FMT can be converted.
  3676. Return a list where each element is of the
  3677. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3678. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3679. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3680. (let* ((converter
  3681. (and org-e-odt-convert-process
  3682. (cadr (assoc-string org-e-odt-convert-process
  3683. org-e-odt-convert-processes t))))
  3684. (capabilities
  3685. (and org-e-odt-convert-process
  3686. (cadr (assoc-string org-e-odt-convert-process
  3687. org-e-odt-convert-processes t))
  3688. org-e-odt-convert-capabilities))
  3689. reachable-formats)
  3690. (when converter
  3691. (dolist (c capabilities)
  3692. (when (member in-fmt (nth 1 c))
  3693. (push (cons converter (nth 2 c)) reachable-formats))))
  3694. reachable-formats))
  3695. (defun org-e-odt-reachable-formats (in-fmt)
  3696. "Return list of formats to which IN-FMT can be converted.
  3697. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3698. (let (l)
  3699. (mapc (lambda (e) (add-to-list 'l e))
  3700. (apply 'append (mapcar
  3701. (lambda (e) (mapcar 'car (cdr e)))
  3702. (org-e-odt-do-reachable-formats in-fmt))))
  3703. l))
  3704. (defun org-e-odt-convert-read-params ()
  3705. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3706. This is a helper routine for interactive use."
  3707. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3708. (in-file (read-file-name "File to be converted: "
  3709. nil buffer-file-name t))
  3710. (in-fmt (file-name-extension in-file))
  3711. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3712. (out-fmt
  3713. (or (and out-fmt-choices
  3714. (funcall input "Output format: "
  3715. out-fmt-choices nil nil nil))
  3716. (error
  3717. "No known converter or no known output formats for %s files"
  3718. in-fmt))))
  3719. (list in-file out-fmt)))
  3720. ;;;###autoload
  3721. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3722. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3723. IN-FILE is the file to be converted. If unspecified, it defaults
  3724. to variable `buffer-file-name'. OUT-FMT is the desired output
  3725. format. Use `org-e-odt-convert-process' as the converter.
  3726. If PREFIX-ARG is non-nil then the newly converted file is opened
  3727. using `org-open-file'."
  3728. (interactive
  3729. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3730. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3731. ;;; Library Initializations
  3732. (mapc
  3733. (lambda (desc)
  3734. ;; Let Emacs open all OpenDocument files in archive mode
  3735. (add-to-list 'auto-mode-alist
  3736. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3737. org-e-odt-file-extensions)
  3738. ;;; FIXME
  3739. ;;;; Links whose description is image
  3740. ;; (org-lparse-link-description-is-image
  3741. ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  3742. ;; href desc))
  3743. (provide 'org-e-odt)
  3744. ;;; org-e-odt.el ends here