org-e-odt.el 159 KB

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