ox-odt.el 159 KB

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