ox-odt.el 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423
  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=\"%s%%\" 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.3")
  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.4"
  423. :package-version '(Org . "8.0")
  424. :type 'integer)
  425. ;;;; Document conversion
  426. (defcustom org-odt-convert-processes
  427. '(("LibreOffice"
  428. "soffice --headless --convert-to %f%x --outdir %d %i")
  429. ("unoconv"
  430. "unoconv -f %f -o %d %i"))
  431. "Specify a list of document converters and their usage.
  432. The converters in this list are offered as choices while
  433. customizing `org-odt-convert-process'.
  434. This variable is a list where each element is of the
  435. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  436. of the converter. CONVERTER-CMD is the shell command for the
  437. converter and can contain format specifiers. These format
  438. specifiers are interpreted as below:
  439. %i input file name in full
  440. %I input file name as a URL
  441. %f format of the output file
  442. %o output file name in full
  443. %O output file name as a URL
  444. %d output dir in full
  445. %D output dir as a URL.
  446. %x extra options as set in `org-odt-convert-capabilities'."
  447. :group 'org-export-odt
  448. :version "24.1"
  449. :type
  450. '(choice
  451. (const :tag "None" nil)
  452. (alist :tag "Converters"
  453. :key-type (string :tag "Converter Name")
  454. :value-type (group (string :tag "Command line")))))
  455. (defcustom org-odt-convert-process "LibreOffice"
  456. "Use this converter to convert from \"odt\" format to other formats.
  457. During customization, the list of converter names are populated
  458. from `org-odt-convert-processes'."
  459. :group 'org-export-odt
  460. :version "24.1"
  461. :type '(choice :convert-widget
  462. (lambda (w)
  463. (apply 'widget-convert (widget-type w)
  464. (eval (car (widget-get w :args)))))
  465. `((const :tag "None" nil)
  466. ,@(mapcar (lambda (c)
  467. `(const :tag ,(car c) ,(car c)))
  468. org-odt-convert-processes))))
  469. (defcustom org-odt-convert-capabilities
  470. '(("Text"
  471. ("odt" "ott" "doc" "rtf" "docx")
  472. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  473. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  474. ("Web"
  475. ("html")
  476. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  477. ("Spreadsheet"
  478. ("ods" "ots" "xls" "csv" "xlsx")
  479. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  480. ("xls" "xls") ("xlsx" "xlsx")))
  481. ("Presentation"
  482. ("odp" "otp" "ppt" "pptx")
  483. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  484. ("pptx" "pptx") ("odg" "odg"))))
  485. "Specify input and output formats of `org-odt-convert-process'.
  486. More correctly, specify the set of input and output formats that
  487. the user is actually interested in.
  488. This variable is an alist where each element is of the
  489. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  490. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  491. alist where each element is of the form (OUTPUT-FMT
  492. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  493. The variable is interpreted as follows:
  494. `org-odt-convert-process' can take any document that is in
  495. INPUT-FMT-LIST and produce any document that is in the
  496. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  497. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  498. serves dual purposes:
  499. - It is used for populating completion candidates during
  500. `org-odt-convert' commands.
  501. - It is used as the value of \"%f\" specifier in
  502. `org-odt-convert-process'.
  503. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  504. `org-odt-convert-process'.
  505. DOCUMENT-CLASS is used to group a set of file formats in
  506. INPUT-FMT-LIST in to a single class.
  507. Note that this variable inherently captures how LibreOffice based
  508. converters work. LibreOffice maps documents of various formats
  509. to classes like Text, Web, Spreadsheet, Presentation etc and
  510. allow document of a given class (irrespective of its source
  511. format) to be converted to any of the export formats associated
  512. with that class.
  513. See default setting of this variable for an typical
  514. configuration."
  515. :group 'org-export-odt
  516. :version "24.1"
  517. :type
  518. '(choice
  519. (const :tag "None" nil)
  520. (alist :tag "Capabilities"
  521. :key-type (string :tag "Document Class")
  522. :value-type
  523. (group (repeat :tag "Input formats" (string :tag "Input format"))
  524. (alist :tag "Output formats"
  525. :key-type (string :tag "Output format")
  526. :value-type
  527. (group (string :tag "Output file extension")
  528. (choice
  529. (const :tag "None" nil)
  530. (string :tag "Extra options"))))))))
  531. (defcustom org-odt-preferred-output-format nil
  532. "Automatically post-process to this format after exporting to \"odt\".
  533. Command `org-odt-export-to-odt' exports first to \"odt\" format
  534. and then uses `org-odt-convert-process' to convert the
  535. resulting document to this format. During customization of this
  536. variable, the list of valid values are populated based on
  537. `org-odt-convert-capabilities'.
  538. You can set this option on per-file basis using file local
  539. values. See Info node `(emacs) File Variables'."
  540. :group 'org-export-odt
  541. :version "24.1"
  542. :type '(choice :convert-widget
  543. (lambda (w)
  544. (apply 'widget-convert (widget-type w)
  545. (eval (car (widget-get w :args)))))
  546. `((const :tag "None" nil)
  547. ,@(mapcar (lambda (c)
  548. `(const :tag ,c ,c))
  549. (org-odt-reachable-formats "odt")))))
  550. ;;;###autoload
  551. (put 'org-odt-preferred-output-format 'safe-local-variable 'stringp)
  552. ;;;; Drawers
  553. (defcustom org-odt-format-drawer-function
  554. (lambda (name contents) contents)
  555. "Function called to format a drawer in ODT code.
  556. The function must accept two parameters:
  557. NAME the drawer name, like \"LOGBOOK\"
  558. CONTENTS the contents of the drawer.
  559. The function should return the string to be exported.
  560. The default value simply returns the value of CONTENTS."
  561. :group 'org-export-odt
  562. :version "24.4"
  563. :package-version '(Org . "8.3")
  564. :type 'function)
  565. ;;;; Headline
  566. (defcustom org-odt-format-headline-function 'ignore
  567. "Function to format headline text.
  568. This function will be called with 5 arguments:
  569. TODO the todo keyword \(string or nil\).
  570. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  571. PRIORITY the priority of the headline \(integer or nil\)
  572. TEXT the main headline text \(string\).
  573. TAGS the tags string, separated with colons \(string or nil\).
  574. The function result will be used as headline text."
  575. :group 'org-export-odt
  576. :version "24.4"
  577. :package-version '(Org . "8.0")
  578. :type 'function)
  579. ;;;; Inlinetasks
  580. (defcustom org-odt-format-inlinetask-function 'ignore
  581. "Function called to format an inlinetask in ODT code.
  582. The function must accept six parameters:
  583. TODO the todo keyword, as a string
  584. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  585. PRIORITY the inlinetask priority, as a string
  586. NAME the inlinetask name, as a string.
  587. TAGS the inlinetask tags, as a string.
  588. CONTENTS the contents of the inlinetask, as a string.
  589. The function should return the string to be exported."
  590. :group 'org-export-odt
  591. :version "24.4"
  592. :package-version '(Org . "8.0")
  593. :type 'function)
  594. ;;;; LaTeX
  595. (defcustom org-odt-with-latex org-export-with-latex
  596. "Non-nil means process LaTeX math snippets.
  597. When set, the exporter will process LaTeX environments and
  598. fragments.
  599. This option can also be set with the +OPTIONS line,
  600. e.g. \"tex:mathjax\". Allowed values are:
  601. nil Ignore math snippets.
  602. `verbatim' Keep everything in verbatim
  603. `dvipng' Process the LaTeX fragments to images. This will also
  604. include processing of non-math environments.
  605. `imagemagick' Convert the LaTeX fragments to pdf files and use
  606. imagemagick to convert pdf files to png files.
  607. `mathjax' Do MathJax preprocessing and arrange for MathJax.js to
  608. be loaded.
  609. t Synonym for `mathjax'."
  610. :group 'org-export-odt
  611. :version "24.4"
  612. :package-version '(Org . "8.0")
  613. :type '(choice
  614. (const :tag "Do not process math in any way" nil)
  615. (const :tag "Use dvipng to make images" dvipng)
  616. (const :tag "Use imagemagick to make images" imagemagick)
  617. (const :tag "Use MathJax to display math" mathjax)
  618. (const :tag "Leave math verbatim" verbatim)))
  619. ;;;; Links
  620. (defcustom org-odt-inline-formula-rules
  621. '(("file" . "\\.\\(mathml\\|mml\\|odf\\)\\'"))
  622. "Rules characterizing formula files that can be inlined into ODT.
  623. A rule consists in an association whose key is the type of link
  624. to consider, and value is a regexp that will be matched against
  625. link's path."
  626. :group 'org-export-odt
  627. :version "24.4"
  628. :package-version '(Org . "8.0")
  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. :version "24.4"
  639. :package-version '(Org . "8.0")
  640. :type '(alist :key-type (string :tag "Type")
  641. :value-type (regexp :tag "Path")))
  642. (defcustom org-odt-pixels-per-inch 96.0
  643. "Scaling factor for converting images pixels to inches.
  644. Use this for sizing of embedded images. See Info node `(org)
  645. Images in ODT export' for more information."
  646. :type 'float
  647. :group 'org-export-odt
  648. :version "24.4"
  649. :package-version '(Org . "8.1"))
  650. ;;;; Src Block
  651. (defcustom org-odt-create-custom-styles-for-srcblocks t
  652. "Whether custom styles for colorized source blocks be automatically created.
  653. When this option is turned on, the exporter creates custom styles
  654. for source blocks based on the advice of `htmlfontify'. Creation
  655. of custom styles happen as part of `org-odt-hfy-face-to-css'.
  656. When this option is turned off exporter does not create such
  657. styles.
  658. Use the latter option if you do not want the custom styles to be
  659. based on your current display settings. It is necessary that the
  660. styles.xml already contains needed styles for colorizing to work.
  661. This variable is effective only if
  662. `org-odt-fontify-srcblocks' is turned on."
  663. :group 'org-export-odt
  664. :version "24.1"
  665. :type 'boolean)
  666. (defcustom org-odt-fontify-srcblocks t
  667. "Specify whether or not source blocks need to be fontified.
  668. Turn this option on if you want to colorize the source code
  669. blocks in the exported file. For colorization to work, you need
  670. to make available an enhanced version of `htmlfontify' library."
  671. :type 'boolean
  672. :group 'org-export-odt
  673. :version "24.1")
  674. ;;;; Table
  675. (defcustom org-odt-table-styles
  676. '(("OrgEquation" "OrgEquation"
  677. ((use-first-column-styles . t)
  678. (use-last-column-styles . t)))
  679. ("TableWithHeaderRowAndColumn" "Custom"
  680. ((use-first-row-styles . t)
  681. (use-first-column-styles . t)))
  682. ("TableWithFirstRowandLastRow" "Custom"
  683. ((use-first-row-styles . t)
  684. (use-last-row-styles . t)))
  685. ("GriddedTable" "Custom" nil))
  686. "Specify how Table Styles should be derived from a Table Template.
  687. This is a list where each element is of the
  688. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  689. TABLE-STYLE-NAME is the style associated with the table through
  690. \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
  691. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  692. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  693. below) that is included in
  694. `org-odt-content-template-file'.
  695. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  696. \"TableCell\"
  697. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  698. \"TableParagraph\"
  699. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  700. \"FirstRow\" | \"LastRow\" |
  701. \"EvenRow\" | \"OddRow\" |
  702. \"EvenColumn\" | \"OddColumn\" | \"\"
  703. where \"+\" above denotes string concatenation.
  704. TABLE-CELL-OPTIONS is an alist where each element is of the
  705. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  706. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  707. `use-last-row-styles' |
  708. `use-first-column-styles' |
  709. `use-last-column-styles' |
  710. `use-banding-rows-styles' |
  711. `use-banding-columns-styles' |
  712. `use-first-row-styles'
  713. ON-OR-OFF := `t' | `nil'
  714. For example, with the following configuration
  715. \(setq org-odt-table-styles
  716. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  717. \(\(use-first-row-styles . t\)
  718. \(use-first-column-styles . t\)\)\)
  719. \(\"TableWithHeaderColumns\" \"Custom\"
  720. \(\(use-first-column-styles . t\)\)\)\)\)
  721. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  722. style will use the following table-cell styles -
  723. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  724. \"CustomTableCell\" and the following paragraph styles
  725. \"CustomFirstRowTableParagraph\",
  726. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  727. as appropriate.
  728. 2. A table associated with \"TableWithHeaderColumns\" style will
  729. use the following table-cell styles -
  730. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  731. following paragraph styles
  732. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  733. as appropriate..
  734. Note that TABLE-TEMPLATE-NAME corresponds to the
  735. \"<table:table-template>\" elements contained within
  736. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  737. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  738. \"table:template-name\" and \"table:use-first-row-styles\" etc
  739. attributes of \"<table:table>\" element. Refer ODF-1.2
  740. specification for more information. Also consult the
  741. implementation filed under `org-odt-get-table-cell-styles'.
  742. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  743. formatting of numbered display equations. Do not delete this
  744. style from the list."
  745. :group 'org-export-odt
  746. :version "24.1"
  747. :type '(choice
  748. (const :tag "None" nil)
  749. (repeat :tag "Table Styles"
  750. (list :tag "Table Style Specification"
  751. (string :tag "Table Style Name")
  752. (string :tag "Table Template Name")
  753. (alist :options (use-first-row-styles
  754. use-last-row-styles
  755. use-first-column-styles
  756. use-last-column-styles
  757. use-banding-rows-styles
  758. use-banding-columns-styles)
  759. :key-type symbol
  760. :value-type (const :tag "True" t))))))
  761. ;;;; Timestamps
  762. (defcustom org-odt-use-date-fields nil
  763. "Non-nil, if timestamps should be exported as date fields.
  764. When nil, export timestamps as plain text.
  765. When non-nil, map `org-time-stamp-custom-formats' to a pair of
  766. OpenDocument date-styles with names \"OrgDate1\" and \"OrgDate2\"
  767. respectively. A timestamp with no time component is formatted
  768. with style \"OrgDate1\" while one with explicit hour and minutes
  769. is formatted with style \"OrgDate2\".
  770. This feature is experimental. Most (but not all) of the common
  771. %-specifiers in `format-time-string' are supported.
  772. Specifically, locale-dependent specifiers like \"%c\", \"%x\" are
  773. formatted as canonical Org timestamps. For finer control, avoid
  774. these %-specifiers.
  775. Textual specifiers like \"%b\", \"%h\", \"%B\", \"%a\", \"%A\"
  776. etc., are displayed by the application in the default language
  777. and country specified in `org-odt-styles-file'. Note that the
  778. default styles file uses language \"en\" and country \"GB\". You
  779. can localize the week day and month strings in the exported
  780. document by setting the default language and country either using
  781. the application UI or through a custom styles file.
  782. See `org-odt--build-date-styles' for implementation details."
  783. :group 'org-export-odt
  784. :version "24.4"
  785. :package-version '(Org . "8.0")
  786. :type 'boolean)
  787. ;;; Internal functions
  788. ;;;; Date
  789. (defun org-odt--format-timestamp (timestamp &optional end iso-date-p)
  790. (let* ((format-timestamp
  791. (lambda (timestamp format &optional end utc)
  792. (if timestamp
  793. (org-timestamp-format timestamp format end utc)
  794. (format-time-string format nil utc))))
  795. (has-time-p (or (not timestamp)
  796. (org-timestamp-has-time-p timestamp)))
  797. (iso-date (let ((format (if has-time-p "%Y-%m-%dT%H:%M:%S"
  798. "%Y-%m-%dT%H:%M:%S")))
  799. (funcall format-timestamp timestamp format end))))
  800. (if iso-date-p iso-date
  801. (let* ((style (if has-time-p "OrgDate2" "OrgDate1"))
  802. ;; LibreOffice does not care about end goes as content
  803. ;; within the "<text:date>...</text:date>" field. The
  804. ;; displayed date is automagically corrected to match the
  805. ;; format requested by "style:data-style-name" attribute. So
  806. ;; don't bother about formatting the date contents to be
  807. ;; compatible with "OrgDate1" and "OrgDateTime" styles. A
  808. ;; simple Org-style date should suffice.
  809. (date (let* ((formats
  810. (if org-display-custom-times
  811. (cons (substring
  812. (car org-time-stamp-custom-formats) 1 -1)
  813. (substring
  814. (cdr org-time-stamp-custom-formats) 1 -1))
  815. '("%Y-%m-%d %a" . "%Y-%m-%d %a %H:%M")))
  816. (format (if has-time-p (cdr formats) (car formats))))
  817. (funcall format-timestamp timestamp format end)))
  818. (repeater (let ((repeater-type (org-element-property
  819. :repeater-type timestamp))
  820. (repeater-value (org-element-property
  821. :repeater-value timestamp))
  822. (repeater-unit (org-element-property
  823. :repeater-unit timestamp)))
  824. (concat
  825. (case repeater-type
  826. (catchup "++") (restart ".+") (cumulate "+"))
  827. (when repeater-value
  828. (number-to-string repeater-value))
  829. (case repeater-unit
  830. (hour "h") (day "d") (week "w") (month "m")
  831. (year "y"))))))
  832. (concat
  833. (format "<text:date text:date-value=\"%s\" style:data-style-name=\"%s\" text:fixed=\"true\">%s</text:date>"
  834. iso-date style date)
  835. (and (not (string= repeater "")) " ")
  836. repeater)))))
  837. ;;;; Frame
  838. (defun org-odt--frame (text width height style &optional extra
  839. anchor-type &rest title-and-desc)
  840. (let ((frame-attrs
  841. (concat
  842. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  843. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  844. extra
  845. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  846. (format
  847. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  848. style frame-attrs
  849. (concat text
  850. (let ((title (car title-and-desc))
  851. (desc (cadr title-and-desc)))
  852. (concat (when title
  853. (format "<svg:title>%s</svg:title>"
  854. (org-odt--encode-plain-text title t)))
  855. (when desc
  856. (format "<svg:desc>%s</svg:desc>"
  857. (org-odt--encode-plain-text desc t)))))))))
  858. ;;;; Library wrappers
  859. (defun org-odt--zip-extract (archive members target)
  860. (when (atom members) (setq members (list members)))
  861. (mapc (lambda (member)
  862. (require 'arc-mode)
  863. (let* ((--quote-file-name
  864. ;; This is shamelessly stolen from `archive-zip-extract'.
  865. (lambda (name)
  866. (if (or (not (memq system-type '(windows-nt ms-dos)))
  867. (and (boundp 'w32-quote-process-args)
  868. (null w32-quote-process-args)))
  869. (shell-quote-argument name)
  870. name)))
  871. (target (funcall --quote-file-name target))
  872. (archive (expand-file-name archive))
  873. (archive-zip-extract
  874. (list "unzip" "-qq" "-o" "-d" target))
  875. exit-code command-output)
  876. (setq command-output
  877. (with-temp-buffer
  878. (setq exit-code (archive-zip-extract archive member))
  879. (buffer-string)))
  880. (unless (zerop exit-code)
  881. (message command-output)
  882. (error "Extraction failed"))))
  883. members))
  884. ;;;; Target
  885. (defun org-odt--target (text id)
  886. (if (not id) text
  887. (concat
  888. (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
  889. (format "\n<text:bookmark text:name=\"%s\"/>" id) text
  890. (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
  891. ;;;; Textbox
  892. (defun org-odt--textbox (text width height style &optional
  893. extra anchor-type)
  894. (org-odt--frame
  895. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  896. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  897. (and (not width)
  898. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  899. text)
  900. width nil style extra anchor-type))
  901. ;;;; Table of Contents
  902. (defun org-odt-begin-toc (index-title depth)
  903. (concat
  904. (format "
  905. <text:table-of-content text:style-name=\"OrgIndexSection\" text:protected=\"true\" text:name=\"Table of Contents\">
  906. <text:table-of-content-source text:outline-level=\"%d\">
  907. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  908. " depth index-title)
  909. (let ((levels (number-sequence 1 10)))
  910. (mapconcat
  911. (lambda (level)
  912. (format
  913. "
  914. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  915. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  916. <text:index-entry-chapter/>
  917. <text:index-entry-text/>
  918. <text:index-entry-link-end/>
  919. </text:table-of-content-entry-template>
  920. " level level)) levels ""))
  921. (format "
  922. </text:table-of-content-source>
  923. <text:index-body>
  924. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  925. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  926. </text:index-title>
  927. " index-title)))
  928. (defun org-odt-end-toc ()
  929. (format "
  930. </text:index-body>
  931. </text:table-of-content>
  932. "))
  933. (defun* org-odt-format-toc-headline
  934. (todo todo-type priority text tags
  935. &key level section-number headline-label &allow-other-keys)
  936. (setq text
  937. (concat
  938. ;; Section number.
  939. (when section-number (concat section-number ". "))
  940. ;; Todo.
  941. (when todo
  942. (let ((style (if (member todo org-done-keywords)
  943. "OrgDone" "OrgTodo")))
  944. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  945. style todo)))
  946. (when priority
  947. (let* ((style (format "OrgPriority-%s" priority))
  948. (priority (format "[#%c]" priority)))
  949. (format "<text:span text:style-name=\"%s\">%s</text:span> "
  950. style priority)))
  951. ;; Title.
  952. text
  953. ;; Tags.
  954. (when tags
  955. (concat
  956. (format " <text:span text:style-name=\"%s\">[%s]</text:span>"
  957. "OrgTags"
  958. (mapconcat
  959. (lambda (tag)
  960. (format
  961. "<text:span text:style-name=\"%s\">%s</text:span>"
  962. "OrgTag" tag)) tags " : "))))))
  963. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  964. headline-label text))
  965. (defun org-odt-toc (depth info)
  966. (assert (wholenump depth))
  967. ;; When a headline is marked as a radio target, as in the example below:
  968. ;;
  969. ;; ** <<<Some Heading>>>
  970. ;; Some text.
  971. ;;
  972. ;; suppress generation of radio targets. i.e., Radio targets are to
  973. ;; be marked as targets within /document body/ and *not* within
  974. ;; /TOC/, as otherwise there will be duplicated anchors one in TOC
  975. ;; and one in the document body.
  976. ;;
  977. ;; FIXME-1: Currently exported headings are memoized. `org-export.el'
  978. ;; doesn't provide a way to disable memoization. So this doesn't
  979. ;; work.
  980. ;;
  981. ;; FIXME-2: Are there any other objects that need to be suppressed
  982. ;; within TOC?
  983. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  984. (headlines (org-export-collect-headlines
  985. info (and (wholenump depth) depth)))
  986. (backend (org-export-create-backend
  987. :parent (org-export-backend-name
  988. (plist-get info :back-end))
  989. :transcoders (mapcar
  990. (lambda (type) (cons type (lambda (d c i) c)))
  991. (list 'radio-target)))))
  992. (when headlines
  993. (concat
  994. (org-odt-begin-toc title depth)
  995. (mapconcat
  996. (lambda (headline)
  997. (let* ((entry (org-odt-format-headline--wrap
  998. headline backend info 'org-odt-format-toc-headline))
  999. (level (org-export-get-relative-level headline info))
  1000. (style (format "Contents_20_%d" level)))
  1001. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1002. style entry)))
  1003. headlines "\n")
  1004. (org-odt-end-toc)))))
  1005. ;;;; Document styles
  1006. (defun org-odt-add-automatic-style (object-type &optional object-props)
  1007. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  1008. OBJECT-PROPS is (typically) a plist created by passing
  1009. \"#+ATTR_ODT: \" option of the object in question to
  1010. `org-odt-parse-block-attributes'.
  1011. Use `org-odt-object-counters' to generate an automatic
  1012. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  1013. new entry in `org-odt-automatic-styles'. Return (OBJECT-NAME
  1014. . STYLE-NAME)."
  1015. (assert (stringp object-type))
  1016. (let* ((object (intern object-type))
  1017. (seqvar object)
  1018. (seqno (1+ (or (plist-get org-odt-object-counters seqvar) 0)))
  1019. (object-name (format "%s%d" object-type seqno)) style-name)
  1020. (setq org-odt-object-counters
  1021. (plist-put org-odt-object-counters seqvar seqno))
  1022. (when object-props
  1023. (setq style-name (format "Org%s" object-name))
  1024. (setq org-odt-automatic-styles
  1025. (plist-put org-odt-automatic-styles object
  1026. (append (list (list style-name object-props))
  1027. (plist-get org-odt-automatic-styles object)))))
  1028. (cons object-name style-name)))
  1029. ;;;; Checkbox
  1030. (defun org-odt--checkbox (item)
  1031. "Return check-box string associated to ITEM."
  1032. (let ((checkbox (org-element-property :checkbox item)))
  1033. (if (not checkbox) ""
  1034. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1035. "OrgCode" (case checkbox
  1036. (on "[&#x2713;] ") ; CHECK MARK
  1037. (off "[ ] ")
  1038. (trans "[-] "))))))
  1039. ;;; Template
  1040. (defun org-odt--build-date-styles (fmt style)
  1041. ;; In LibreOffice 3.4.6, there doesn't seem to be a convenient way
  1042. ;; to modify the date fields. A date could be modified by
  1043. ;; offsetting in days. That's about it. Also, date and time may
  1044. ;; have to be emitted as two fields - a date field and a time field
  1045. ;; - separately.
  1046. ;; One can add Form Controls to date and time fields so that they
  1047. ;; can be easily modified. But then, the exported document will
  1048. ;; become tightly coupled with LibreOffice and may not function
  1049. ;; properly with other OpenDocument applications.
  1050. ;; I have a strange feeling that Date styles are a bit flaky at the
  1051. ;; moment.
  1052. ;; The feature is experimental.
  1053. (when (and fmt style)
  1054. (let* ((fmt-alist
  1055. '(("%A" . "<number:day-of-week number:style=\"long\"/>")
  1056. ("%B" . "<number:month number:textual=\"true\" number:style=\"long\"/>")
  1057. ("%H" . "<number:hours number:style=\"long\"/>")
  1058. ("%M" . "<number:minutes number:style=\"long\"/>")
  1059. ("%S" . "<number:seconds number:style=\"long\"/>")
  1060. ("%V" . "<number:week-of-year/>")
  1061. ("%Y" . "<number:year number:style=\"long\"/>")
  1062. ("%a" . "<number:day-of-week number:style=\"short\"/>")
  1063. ("%b" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1064. ("%d" . "<number:day number:style=\"long\"/>")
  1065. ("%e" . "<number:day number:style=\"short\"/>")
  1066. ("%h" . "<number:month number:textual=\"true\" number:style=\"short\"/>")
  1067. ("%k" . "<number:hours number:style=\"short\"/>")
  1068. ("%m" . "<number:month number:style=\"long\"/>")
  1069. ("%p" . "<number:am-pm/>")
  1070. ("%y" . "<number:year number:style=\"short\"/>")))
  1071. (case-fold-search nil)
  1072. (re (mapconcat 'identity (mapcar 'car fmt-alist) "\\|"))
  1073. match rpl (start 0) (filler-beg 0) filler-end filler output)
  1074. (mapc
  1075. (lambda (pair)
  1076. (setq fmt (replace-regexp-in-string (car pair) (cdr pair) fmt t t)))
  1077. '(("\\(?:%[[:digit:]]*N\\)" . "") ; strip ns, us and ns
  1078. ("%C" . "Y") ; replace century with year
  1079. ("%D" . "%m/%d/%y")
  1080. ("%G" . "Y") ; year corresponding to iso week
  1081. ("%I" . "%H") ; hour on a 12-hour clock
  1082. ("%R" . "%H:%M")
  1083. ("%T" . "%H:%M:%S")
  1084. ("%U\\|%W" . "%V") ; week no. starting on Sun./Mon.
  1085. ("%Z" . "") ; time zone name
  1086. ("%c" . "%Y-%M-%d %a %H:%M" ) ; locale's date and time format
  1087. ("%g" . "%y")
  1088. ("%X" . "%x" ) ; locale's pref. time format
  1089. ("%j" . "") ; day of the year
  1090. ("%l" . "%k") ; like %I blank-padded
  1091. ("%s" . "") ; no. of secs since 1970-01-01 00:00:00 +0000
  1092. ("%n" . "<text:line-break/>")
  1093. ("%r" . "%I:%M:%S %p")
  1094. ("%t" . "<text:tab/>")
  1095. ("%u\\|%w" . "") ; numeric day of week - Mon (1-7), Sun(0-6)
  1096. ("%x" . "%Y-%M-%d %a") ; locale's pref. time format
  1097. ("%z" . "") ; time zone in numeric form
  1098. ))
  1099. (while (string-match re fmt start)
  1100. (setq match (match-string 0 fmt))
  1101. (setq rpl (assoc-default match fmt-alist))
  1102. (setq start (match-end 0))
  1103. (setq filler-end (match-beginning 0))
  1104. (setq filler (substring fmt (prog1 filler-beg
  1105. (setq filler-beg (match-end 0)))
  1106. filler-end))
  1107. (setq filler (and (not (string= filler ""))
  1108. (format "<number:text>%s</number:text>"
  1109. (org-odt--encode-plain-text filler))))
  1110. (setq output (concat output "\n" filler "\n" rpl)))
  1111. (setq filler (substring fmt filler-beg))
  1112. (unless (string= filler "")
  1113. (setq output (concat output
  1114. (format "\n<number:text>%s</number:text>"
  1115. (org-odt--encode-plain-text filler)))))
  1116. (format "\n<number:date-style style:name=\"%s\" %s>%s\n</number:date-style>"
  1117. style
  1118. (concat " number:automatic-order=\"true\""
  1119. " number:format-source=\"fixed\"")
  1120. output ))))
  1121. (defun org-odt-template (contents info)
  1122. "Return complete document string after ODT conversion.
  1123. CONTENTS is the transcoded contents string. RAW-DATA is the
  1124. original parsed data. INFO is a plist holding export options."
  1125. ;; Write meta file.
  1126. (let ((title (org-export-data (plist-get info :title) info))
  1127. (author (let ((author (plist-get info :author)))
  1128. (if (not author) "" (org-export-data author info))))
  1129. (email (plist-get info :email))
  1130. (keywords (plist-get info :keywords))
  1131. (description (plist-get info :description)))
  1132. (write-region
  1133. (concat
  1134. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1135. <office:document-meta
  1136. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1137. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1138. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1139. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1140. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1141. office:version=\"1.2\">
  1142. <office:meta>\n"
  1143. (format "<dc:creator>%s</dc:creator>\n" author)
  1144. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1145. ;; Date, if required.
  1146. (when (plist-get info :with-date)
  1147. ;; Check if DATE is specified as an Org-timestamp. If yes,
  1148. ;; include it as meta information. Otherwise, just use
  1149. ;; today's date.
  1150. (let* ((date (let ((date (plist-get info :date)))
  1151. (and (not (cdr date))
  1152. (eq (org-element-type (car date)) 'timestamp)
  1153. (car date)))))
  1154. (let ((iso-date (org-odt--format-timestamp date nil 'iso-date)))
  1155. (concat
  1156. (format "<dc:date>%s</dc:date>\n" iso-date)
  1157. (format "<meta:creation-date>%s</meta:creation-date>\n"
  1158. iso-date)))))
  1159. (format "<meta:generator>%s</meta:generator>\n"
  1160. (let ((creator-info (plist-get info :with-creator)))
  1161. (if (or (not creator-info) (eq creator-info 'comment)) ""
  1162. (plist-get info :creator))))
  1163. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1164. (format "<dc:subject>%s</dc:subject>\n" description)
  1165. (format "<dc:title>%s</dc:title>\n" title)
  1166. "\n"
  1167. " </office:meta>\n" "</office:document-meta>")
  1168. nil (concat org-odt-zip-dir "meta.xml"))
  1169. ;; Add meta.xml in to manifest.
  1170. (org-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1171. ;; Update styles file.
  1172. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1173. ;; Write styles file.
  1174. (let* ((styles-file (plist-get info :odt-styles-file))
  1175. (styles-file (and styles-file (read (org-trim styles-file))))
  1176. ;; Non-availability of styles.xml is not a critical
  1177. ;; error. For now, throw an error.
  1178. (styles-file (or styles-file
  1179. org-odt-styles-file
  1180. (expand-file-name "OrgOdtStyles.xml"
  1181. org-odt-styles-dir)
  1182. (error "org-odt: Missing styles file?"))))
  1183. (cond
  1184. ((listp styles-file)
  1185. (let ((archive (nth 0 styles-file))
  1186. (members (nth 1 styles-file)))
  1187. (org-odt--zip-extract archive members org-odt-zip-dir)
  1188. (mapc
  1189. (lambda (member)
  1190. (when (org-file-image-p member)
  1191. (let* ((image-type (file-name-extension member))
  1192. (media-type (format "image/%s" image-type)))
  1193. (org-odt-create-manifest-file-entry media-type member))))
  1194. members)))
  1195. ((and (stringp styles-file) (file-exists-p styles-file))
  1196. (let ((styles-file-type (file-name-extension styles-file)))
  1197. (cond
  1198. ((string= styles-file-type "xml")
  1199. (copy-file styles-file (concat org-odt-zip-dir "styles.xml") t))
  1200. ((member styles-file-type '("odt" "ott"))
  1201. (org-odt--zip-extract styles-file "styles.xml" org-odt-zip-dir)))))
  1202. (t
  1203. (error (format "Invalid specification of styles.xml file: %S"
  1204. org-odt-styles-file))))
  1205. ;; create a manifest entry for styles.xml
  1206. (org-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1207. ;; FIXME: Who is opening an empty styles.xml before this point?
  1208. (with-current-buffer
  1209. (find-file-noselect (concat org-odt-zip-dir "styles.xml") t)
  1210. (revert-buffer t t)
  1211. ;; Write custom styles for source blocks
  1212. ;; Save STYLES used for colorizing of source blocks.
  1213. ;; Update styles.xml with styles that were collected as part of
  1214. ;; `org-odt-hfy-face-to-css' callbacks.
  1215. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1216. hfy-user-sheet-assoc "")))
  1217. (when styles
  1218. (goto-char (point-min))
  1219. (when (re-search-forward "</office:styles>" nil t)
  1220. (goto-char (match-beginning 0))
  1221. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1222. ;; Update styles.xml - take care of outline numbering
  1223. ;; Don't make automatic backup of styles.xml file. This setting
  1224. ;; prevents the backed-up styles.xml file from being zipped in to
  1225. ;; odt file. This is more of a hackish fix. Better alternative
  1226. ;; would be to fix the zip command so that the output odt file
  1227. ;; includes only the needed files and excludes any auto-generated
  1228. ;; extra files like backups and auto-saves etc etc. Note that
  1229. ;; currently the zip command zips up the entire temp directory so
  1230. ;; that any auto-generated files created under the hood ends up in
  1231. ;; the resulting odt file.
  1232. (set (make-local-variable 'backup-inhibited) t)
  1233. ;; Outline numbering is retained only upto LEVEL.
  1234. ;; To disable outline numbering pass a LEVEL of 0.
  1235. (goto-char (point-min))
  1236. (let ((regex
  1237. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1238. (replacement
  1239. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1240. (while (re-search-forward regex nil t)
  1241. (unless (let ((sec-num (plist-get info :section-numbers))
  1242. (level (string-to-number (match-string 2))))
  1243. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1244. (replace-match replacement t nil))))
  1245. (save-buffer 0)))
  1246. ;; Update content.xml.
  1247. (let* ( ;; `org-display-custom-times' should be accessed right
  1248. ;; within the context of the Org buffer. So obtain its
  1249. ;; value before moving on to temp-buffer context down below.
  1250. (custom-time-fmts
  1251. (if org-display-custom-times
  1252. (cons (substring (car org-time-stamp-custom-formats) 1 -1)
  1253. (substring (cdr org-time-stamp-custom-formats) 1 -1))
  1254. '("%Y-%M-%d %a" . "%Y-%M-%d %a %H:%M"))))
  1255. (with-temp-buffer
  1256. (insert-file-contents
  1257. (or org-odt-content-template-file
  1258. (expand-file-name "OrgOdtContentTemplate.xml"
  1259. org-odt-styles-dir)))
  1260. ;; Write automatic styles.
  1261. ;; - Position the cursor.
  1262. (goto-char (point-min))
  1263. (re-search-forward " </office:automatic-styles>" nil t)
  1264. (goto-char (match-beginning 0))
  1265. ;; - Dump automatic table styles.
  1266. (loop for (style-name props) in
  1267. (plist-get org-odt-automatic-styles 'Table) do
  1268. (when (setq props (or (plist-get props :rel-width) "96"))
  1269. (insert (format org-odt-table-style-format style-name props))))
  1270. ;; - Dump date-styles.
  1271. (when org-odt-use-date-fields
  1272. (insert (org-odt--build-date-styles (car custom-time-fmts)
  1273. "OrgDate1")
  1274. (org-odt--build-date-styles (cdr custom-time-fmts)
  1275. "OrgDate2")))
  1276. ;; Update display level.
  1277. ;; - Remove existing sequence decls. Also position the cursor.
  1278. (goto-char (point-min))
  1279. (when (re-search-forward "<text:sequence-decls" nil t)
  1280. (delete-region (match-beginning 0)
  1281. (re-search-forward "</text:sequence-decls>" nil nil)))
  1282. ;; Update sequence decls according to user preference.
  1283. (insert
  1284. (format
  1285. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1286. (mapconcat
  1287. (lambda (x)
  1288. (format
  1289. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1290. org-odt-display-outline-level (nth 1 x)))
  1291. org-odt-category-map-alist "\n")))
  1292. ;; Position the cursor to document body.
  1293. (goto-char (point-min))
  1294. (re-search-forward "</office:text>" nil nil)
  1295. (goto-char (match-beginning 0))
  1296. ;; Preamble - Title, Author, Date etc.
  1297. (insert
  1298. (let* ((title (org-export-data (plist-get info :title) info))
  1299. (author (and (plist-get info :with-author)
  1300. (let ((auth (plist-get info :author)))
  1301. (and auth (org-export-data auth info)))))
  1302. (email (plist-get info :email))
  1303. ;; Switch on or off above vars based on user settings
  1304. (author (and (plist-get info :with-author) (or author email)))
  1305. (email (and (plist-get info :with-email) email)))
  1306. (concat
  1307. ;; Title.
  1308. (when title
  1309. (concat
  1310. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1311. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1312. ;; Separator.
  1313. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1314. (cond
  1315. ((and author (not email))
  1316. ;; Author only.
  1317. (concat
  1318. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1319. "OrgSubtitle"
  1320. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1321. ;; Separator.
  1322. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1323. ((and author email)
  1324. ;; Author and E-mail.
  1325. (concat
  1326. (format
  1327. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1328. "OrgSubtitle"
  1329. (format
  1330. "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1331. (concat "mailto:" email)
  1332. (format "<text:initial-creator>%s</text:initial-creator>" author)))
  1333. ;; Separator.
  1334. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1335. ;; Date, if required.
  1336. (when (plist-get info :with-date)
  1337. (let* ((date (plist-get info :date))
  1338. ;; Check if DATE is specified as a timestamp.
  1339. (timestamp (and (not (cdr date))
  1340. (eq (org-element-type (car date)) 'timestamp)
  1341. (car date))))
  1342. (concat
  1343. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1344. "OrgSubtitle"
  1345. (if (and org-odt-use-date-fields timestamp)
  1346. (org-odt--format-timestamp (car date))
  1347. (org-export-data (plist-get info :date) info)))
  1348. ;; Separator
  1349. "<text:p text:style-name=\"OrgSubtitle\"/>"))))))
  1350. ;; Table of Contents
  1351. (let* ((with-toc (plist-get info :with-toc))
  1352. (depth (and with-toc (if (wholenump with-toc)
  1353. with-toc
  1354. (plist-get info :headline-levels)))))
  1355. (when depth (insert (or (org-odt-toc depth info) ""))))
  1356. ;; Contents.
  1357. (insert contents)
  1358. ;; Return contents.
  1359. (buffer-substring-no-properties (point-min) (point-max)))))
  1360. ;;; Transcode Functions
  1361. ;;;; Bold
  1362. (defun org-odt-bold (bold contents info)
  1363. "Transcode BOLD from Org to ODT.
  1364. CONTENTS is the text with bold markup. INFO is a plist holding
  1365. contextual information."
  1366. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1367. "Bold" contents))
  1368. ;;;; Center Block
  1369. (defun org-odt-center-block (center-block contents info)
  1370. "Transcode a CENTER-BLOCK element from Org to ODT.
  1371. CONTENTS holds the contents of the center block. INFO is a plist
  1372. holding contextual information."
  1373. contents)
  1374. ;;;; Clock
  1375. (defun org-odt-clock (clock contents info)
  1376. "Transcode a CLOCK element from Org to ODT.
  1377. CONTENTS is nil. INFO is a plist used as a communication
  1378. channel."
  1379. (let ((timestamp (org-element-property :value clock))
  1380. (duration (org-element-property :duration clock)))
  1381. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1382. (if (eq (org-element-type (org-export-get-next-element clock info))
  1383. 'clock) "OrgClock" "OrgClockLastLine")
  1384. (concat
  1385. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1386. "OrgClockKeyword" org-clock-string)
  1387. (org-odt-timestamp timestamp contents info)
  1388. (and duration (format " (%s)" duration))))))
  1389. ;;;; Code
  1390. (defun org-odt-code (code contents info)
  1391. "Transcode a CODE object from Org to ODT.
  1392. CONTENTS is nil. INFO is a plist used as a communication
  1393. channel."
  1394. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1395. "OrgCode" (org-odt--encode-plain-text
  1396. (org-element-property :value code))))
  1397. ;;;; Comment
  1398. ;; Comments are ignored.
  1399. ;;;; Comment Block
  1400. ;; Comment Blocks are ignored.
  1401. ;;;; Drawer
  1402. (defun org-odt-drawer (drawer contents info)
  1403. "Transcode a DRAWER element from Org to ODT.
  1404. CONTENTS holds the contents of the block. INFO is a plist
  1405. holding contextual information."
  1406. (let* ((name (org-element-property :drawer-name drawer))
  1407. (output (funcall org-odt-format-drawer-function
  1408. name 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. ;; Transcode 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. ((not (eq org-odt-format-headline-function 'ignore))
  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 not 'ignore, call it
  1674. ;; with appropriate arguments.
  1675. ((not (eq org-odt-format-inlinetask-function 'ignore))
  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 its 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. ;;
  2052. ;; Handle `:width', `:height' and `:scale' properties. Read
  2053. ;; them as numbers since we need them for computations.
  2054. (size (org-odt--image-size
  2055. src-expanded
  2056. (let ((width (plist-get attr-plist :width)))
  2057. (and width (read width)))
  2058. (let ((length (plist-get attr-plist :length)))
  2059. (and length (read length)))
  2060. (let ((scale (plist-get attr-plist :scale)))
  2061. (and scale (read scale)))
  2062. nil ; embed-as
  2063. "paragraph" ; FIXME
  2064. ))
  2065. (width (car size)) (height (cdr size))
  2066. (standalone-link-p (org-odt--standalone-link-p element info))
  2067. (embed-as (if standalone-link-p "paragraph" "as-char"))
  2068. (captions (org-odt-format-label element info 'definition))
  2069. (caption (car captions)) (short-caption (cdr captions))
  2070. (entity (concat (and caption "Captioned") embed-as "Image"))
  2071. ;; Check if this link was created by LaTeX-to-PNG converter.
  2072. (replaces (org-element-property
  2073. :replaces (if (not standalone-link-p) element
  2074. (org-export-get-parent-element element))))
  2075. ;; If yes, note down the type of the element - LaTeX Fragment
  2076. ;; or LaTeX environment. It will go in to frame title.
  2077. (title (and replaces (capitalize
  2078. (symbol-name (org-element-type replaces)))))
  2079. ;; If yes, note down its contents. It will go in to frame
  2080. ;; description. This quite useful for debugging.
  2081. (desc (and replaces (org-element-property :value replaces))))
  2082. (org-odt--render-image/formula entity href width height
  2083. captions user-frame-params title desc)))
  2084. ;;;; Links :: Math formula
  2085. (defun org-odt-link--inline-formula (element info)
  2086. (let* ((src (let* ((type (org-element-property :type element))
  2087. (raw-path (org-element-property :path element)))
  2088. (cond
  2089. ((file-name-absolute-p raw-path)
  2090. (expand-file-name raw-path))
  2091. (t raw-path))))
  2092. (src-expanded (if (file-name-absolute-p src) src
  2093. (expand-file-name src (file-name-directory
  2094. (plist-get info :input-file)))))
  2095. (href
  2096. (format
  2097. "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  2098. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  2099. (file-name-directory (org-odt--copy-formula-file src-expanded))))
  2100. (standalone-link-p (org-odt--standalone-link-p element info))
  2101. (embed-as (if standalone-link-p 'paragraph 'character))
  2102. (captions (org-odt-format-label element info 'definition))
  2103. (caption (car captions)) (short-caption (cdr captions))
  2104. ;; Check if this link was created by LaTeX-to-MathML
  2105. ;; converter.
  2106. (replaces (org-element-property
  2107. :replaces (if (not standalone-link-p) element
  2108. (org-export-get-parent-element element))))
  2109. ;; If yes, note down the type of the element - LaTeX Fragment
  2110. ;; or LaTeX environment. It will go in to frame title.
  2111. (title (and replaces (capitalize
  2112. (symbol-name (org-element-type replaces)))))
  2113. ;; If yes, note down its contents. It will go in to frame
  2114. ;; description. This quite useful for debugging.
  2115. (desc (and replaces (org-element-property :value replaces)))
  2116. width height)
  2117. (cond
  2118. ((eq embed-as 'character)
  2119. (org-odt--render-image/formula "InlineFormula" href width height
  2120. nil nil title desc))
  2121. (t
  2122. (let* ((equation (org-odt--render-image/formula
  2123. "CaptionedDisplayFormula" href width height
  2124. captions nil title desc))
  2125. (label
  2126. (let* ((org-odt-category-map-alist
  2127. '(("__MathFormula__" "Text" "math-label" "Equation"
  2128. org-odt--enumerable-formula-p))))
  2129. (car (org-odt-format-label element info 'definition)))))
  2130. (concat equation "<text:tab/>" label))))))
  2131. (defun org-odt--copy-formula-file (src-file)
  2132. "Returns the internal name of the file"
  2133. (let* ((target-dir (format "Formula-%04d/"
  2134. (incf org-odt-embedded-formulas-count)))
  2135. (target-file (concat target-dir "content.xml")))
  2136. ;; Create a directory for holding formula file. Also enter it in
  2137. ;; to manifest.
  2138. (make-directory (concat org-odt-zip-dir target-dir))
  2139. (org-odt-create-manifest-file-entry
  2140. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  2141. ;; Copy over the formula file from user directory to zip
  2142. ;; directory.
  2143. (message "Embedding %s as %s..." src-file target-file)
  2144. (let ((case-fold-search nil))
  2145. (cond
  2146. ;; Case 1: Mathml.
  2147. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  2148. (copy-file src-file (concat org-odt-zip-dir target-file) 'overwrite))
  2149. ;; Case 2: OpenDocument formula.
  2150. ((string-match "\\.odf\\'" src-file)
  2151. (org-odt--zip-extract src-file "content.xml"
  2152. (concat org-odt-zip-dir target-dir)))
  2153. (t (error "%s is not a formula file" src-file))))
  2154. ;; Enter the formula file in to manifest.
  2155. (org-odt-create-manifest-file-entry "text/xml" target-file)
  2156. target-file))
  2157. ;;;; Targets
  2158. (defun org-odt--render-image/formula (cfg-key href width height &optional
  2159. captions user-frame-params
  2160. &rest title-and-desc)
  2161. (let* ((frame-cfg-alist
  2162. ;; Each element of this alist is of the form (CFG-HANDLE
  2163. ;; INNER-FRAME-PARAMS OUTER-FRAME-PARAMS).
  2164. ;; CFG-HANDLE is the key to the alist.
  2165. ;; INNER-FRAME-PARAMS and OUTER-FRAME-PARAMS specify the
  2166. ;; frame params for INNER-FRAME and OUTER-FRAME
  2167. ;; respectively. See below.
  2168. ;; Configurations that are meant to be applied to
  2169. ;; non-captioned image/formula specifies no
  2170. ;; OUTER-FRAME-PARAMS.
  2171. ;; TERMINOLOGY
  2172. ;; ===========
  2173. ;; INNER-FRAME :: Frame that directly surrounds an
  2174. ;; image/formula.
  2175. ;; OUTER-FRAME :: Frame that encloses the INNER-FRAME. This
  2176. ;; frame also contains the caption, if any.
  2177. ;; FRAME-PARAMS :: List of the form (FRAME-STYLE-NAME
  2178. ;; FRAME-ATTRIBUTES FRAME-ANCHOR). Note
  2179. ;; that these are the last three arguments
  2180. ;; to `org-odt--frame'.
  2181. ;; Note that an un-captioned image/formula requires just an
  2182. ;; INNER-FRAME, while a captioned image/formula requires
  2183. ;; both an INNER and an OUTER-FRAME.
  2184. '(("As-CharImage" ("OrgInlineImage" nil "as-char"))
  2185. ("ParagraphImage" ("OrgDisplayImage" nil "paragraph"))
  2186. ("PageImage" ("OrgPageImage" nil "page"))
  2187. ("CaptionedAs-CharImage"
  2188. ("OrgCaptionedImage"
  2189. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2190. ("OrgInlineImage" nil "as-char"))
  2191. ("CaptionedParagraphImage"
  2192. ("OrgCaptionedImage"
  2193. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2194. ("OrgImageCaptionFrame" nil "paragraph"))
  2195. ("CaptionedPageImage"
  2196. ("OrgCaptionedImage"
  2197. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  2198. ("OrgPageImageCaptionFrame" nil "page"))
  2199. ("InlineFormula" ("OrgInlineFormula" nil "as-char"))
  2200. ("DisplayFormula" ("OrgDisplayFormula" nil "as-char"))
  2201. ("CaptionedDisplayFormula"
  2202. ("OrgCaptionedFormula" nil "paragraph")
  2203. ("OrgFormulaCaptionFrame" nil "paragraph"))))
  2204. (caption (car captions)) (short-caption (cdr captions))
  2205. ;; Retrieve inner and outer frame params, from configuration.
  2206. (frame-cfg (assoc-string cfg-key frame-cfg-alist t))
  2207. (inner (nth 1 frame-cfg))
  2208. (outer (nth 2 frame-cfg))
  2209. ;; User-specified frame params (from #+ATTR_ODT spec)
  2210. (user user-frame-params)
  2211. (--merge-frame-params (function
  2212. (lambda (default user)
  2213. "Merge default and user frame params."
  2214. (if (not user) default
  2215. (assert (= (length default) 3))
  2216. (assert (= (length user) 3))
  2217. (loop for u in user
  2218. for d in default
  2219. collect (or u d)))))))
  2220. (cond
  2221. ;; Case 1: Image/Formula has no caption.
  2222. ;; There is only one frame, one that surrounds the image
  2223. ;; or formula.
  2224. ((not caption)
  2225. ;; Merge user frame params with that from configuration.
  2226. (setq inner (funcall --merge-frame-params inner user))
  2227. (apply 'org-odt--frame href width height
  2228. (append inner title-and-desc)))
  2229. ;; Case 2: Image/Formula is captioned or labeled.
  2230. ;; There are two frames: The inner one surrounds the
  2231. ;; image or formula. The outer one contains the
  2232. ;; caption/sequence number.
  2233. (t
  2234. ;; Merge user frame params with outer frame params.
  2235. (setq outer (funcall --merge-frame-params outer user))
  2236. ;; Short caption, if specified, goes as part of inner frame.
  2237. (setq inner (let ((frame-params (copy-sequence inner)))
  2238. (setcar (cdr frame-params)
  2239. (concat
  2240. (cadr frame-params)
  2241. (when short-caption
  2242. (format " draw:name=\"%s\" " short-caption))))
  2243. frame-params))
  2244. (apply 'org-odt--textbox
  2245. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2246. "Illustration"
  2247. (concat
  2248. (apply 'org-odt--frame href width height
  2249. (append inner title-and-desc))
  2250. caption))
  2251. width height outer)))))
  2252. (defun org-odt--enumerable-p (element info)
  2253. ;; Element should have a caption or label.
  2254. (or (org-element-property :caption element)
  2255. (org-element-property :name element)))
  2256. (defun org-odt--enumerable-image-p (element info)
  2257. (org-odt--standalone-link-p
  2258. element info
  2259. ;; Paragraph should have a caption or label. It SHOULD NOT be a
  2260. ;; replacement element. (i.e., It SHOULD NOT be a result of LaTeX
  2261. ;; processing.)
  2262. (lambda (p)
  2263. (and (not (org-element-property :replaces p))
  2264. (or (org-element-property :caption p)
  2265. (org-element-property :name p))))
  2266. ;; Link should point to an image file.
  2267. (lambda (l)
  2268. (assert (eq (org-element-type l) 'link))
  2269. (org-export-inline-image-p l org-odt-inline-image-rules))))
  2270. (defun org-odt--enumerable-latex-image-p (element info)
  2271. (org-odt--standalone-link-p
  2272. element info
  2273. ;; Paragraph should have a caption or label. It SHOULD also be a
  2274. ;; replacement element. (i.e., It SHOULD be a result of LaTeX
  2275. ;; processing.)
  2276. (lambda (p)
  2277. (and (org-element-property :replaces p)
  2278. (or (org-element-property :caption p)
  2279. (org-element-property :name p))))
  2280. ;; Link should point to an image file.
  2281. (lambda (l)
  2282. (assert (eq (org-element-type l) 'link))
  2283. (org-export-inline-image-p l org-odt-inline-image-rules))))
  2284. (defun org-odt--enumerable-formula-p (element info)
  2285. (org-odt--standalone-link-p
  2286. element info
  2287. ;; Paragraph should have a caption or label.
  2288. (lambda (p)
  2289. (or (org-element-property :caption p)
  2290. (org-element-property :name p)))
  2291. ;; Link should point to a MathML or ODF file.
  2292. (lambda (l)
  2293. (assert (eq (org-element-type l) 'link))
  2294. (org-export-inline-image-p l org-odt-inline-formula-rules))))
  2295. (defun org-odt--standalone-link-p (element info &optional
  2296. paragraph-predicate
  2297. link-predicate)
  2298. "Test if ELEMENT is a standalone link for the purpose ODT export.
  2299. INFO is a plist holding contextual information.
  2300. Return non-nil, if ELEMENT is of type paragraph satisfying
  2301. PARAGRAPH-PREDICATE and its sole content, save for whitespaces,
  2302. is a link that satisfies LINK-PREDICATE.
  2303. Return non-nil, if ELEMENT is of type link satisfying
  2304. LINK-PREDICATE and its containing paragraph satisfies
  2305. PARAGRAPH-PREDICATE in addition to having no other content save for
  2306. leading and trailing whitespaces.
  2307. Return nil, otherwise."
  2308. (let ((p (case (org-element-type element)
  2309. (paragraph element)
  2310. (link (and (or (not link-predicate)
  2311. (funcall link-predicate element))
  2312. (org-export-get-parent element)))
  2313. (t nil))))
  2314. (when (and p (eq (org-element-type p) 'paragraph))
  2315. (when (or (not paragraph-predicate)
  2316. (funcall paragraph-predicate p))
  2317. (let ((contents (org-element-contents p)))
  2318. (loop for x in contents
  2319. with inline-image-count = 0
  2320. always (case (org-element-type x)
  2321. (plain-text
  2322. (not (org-string-nw-p x)))
  2323. (link
  2324. (and (or (not link-predicate)
  2325. (funcall link-predicate x))
  2326. (= (incf inline-image-count) 1)))
  2327. (t nil))))))))
  2328. (defun org-odt-link--infer-description (destination info)
  2329. ;; DESTINATION is a HEADLINE, a "<<target>>" or an element (like
  2330. ;; paragraph, verse-block etc) to which a "#+NAME: label" can be
  2331. ;; attached. Note that labels that are attached to captioned
  2332. ;; entities - inline images, math formulae and tables - get resolved
  2333. ;; as part of `org-odt-format-label' and `org-odt--enumerate'.
  2334. ;; Create a cross-reference to DESTINATION but make best-efforts to
  2335. ;; create a *meaningful* description. Check item numbers, section
  2336. ;; number and section title in that order.
  2337. ;; NOTE: Counterpart of `org-export-get-ordinal'.
  2338. ;; FIXME: Handle footnote-definition footnote-reference?
  2339. (let* ((genealogy (org-export-get-genealogy destination))
  2340. (data (reverse genealogy))
  2341. (label (case (org-element-type destination)
  2342. (headline
  2343. (format "sec-%s" (mapconcat 'number-to-string
  2344. (org-export-get-headline-number
  2345. destination info) "-")))
  2346. (target
  2347. (org-element-property :value destination))
  2348. (t (error "FIXME: Resolve %S" destination)))))
  2349. (or
  2350. (let* ( ;; Locate top-level list.
  2351. (top-level-list
  2352. (loop for x on data
  2353. when (eq (org-element-type (car x)) 'plain-list)
  2354. return x))
  2355. ;; Get list item nos.
  2356. (item-numbers
  2357. (loop for (plain-list item . rest) on top-level-list by #'cddr
  2358. until (not (eq (org-element-type plain-list) 'plain-list))
  2359. collect (when (eq (org-element-property :type
  2360. plain-list)
  2361. 'ordered)
  2362. (1+ (length (org-export-get-previous-element
  2363. item info t))))))
  2364. ;; Locate top-most listified headline.
  2365. (listified-headlines
  2366. (loop for x on data
  2367. when (and (eq (org-element-type (car x)) 'headline)
  2368. (org-export-low-level-p (car x) info))
  2369. return x))
  2370. ;; Get listified headline numbers.
  2371. (listified-headline-nos
  2372. (loop for el in listified-headlines
  2373. when (eq (org-element-type el) 'headline)
  2374. collect (when (org-export-numbered-headline-p el info)
  2375. (1+ (length (org-export-get-previous-element
  2376. el info t)))))))
  2377. ;; Combine item numbers from both the listified headlines and
  2378. ;; regular list items.
  2379. ;; Case 1: Check if all the parents of list item are numbered.
  2380. ;; If yes, link to the item proper.
  2381. (let ((item-numbers (append listified-headline-nos item-numbers)))
  2382. (when (and item-numbers (not (memq nil item-numbers)))
  2383. (format "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"%s\">%s</text:bookmark-ref>"
  2384. (org-export-solidify-link-text label)
  2385. (mapconcat (lambda (n) (if (not n) " "
  2386. (concat (number-to-string n) ".")))
  2387. item-numbers "")))))
  2388. ;; Case 2: Locate a regular and numbered headline in the
  2389. ;; hierarchy. Display its section number.
  2390. (let ((headline (loop for el in (cons destination genealogy)
  2391. when (and (eq (org-element-type el) 'headline)
  2392. (not (org-export-low-level-p el info))
  2393. (org-export-numbered-headline-p el info))
  2394. return el)))
  2395. ;; We found one.
  2396. (when headline
  2397. (format "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2398. (org-export-solidify-link-text label)
  2399. (mapconcat 'number-to-string (org-export-get-headline-number
  2400. headline info) "."))))
  2401. ;; Case 4: Locate a regular headline in the hierarchy. Display
  2402. ;; its title.
  2403. (let ((headline (loop for el in (cons destination genealogy)
  2404. when (and (eq (org-element-type el) 'headline)
  2405. (not (org-export-low-level-p el info)))
  2406. return el)))
  2407. ;; We found one.
  2408. (when headline
  2409. (format "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2410. (org-export-solidify-link-text label)
  2411. (let ((title (org-element-property :title headline)))
  2412. (org-export-data title info)))))
  2413. (error "FIXME?"))))
  2414. (defun org-odt-link (link desc info)
  2415. "Transcode a LINK object from Org to ODT.
  2416. DESC is the description part of the link, or the empty string.
  2417. INFO is a plist holding contextual information. See
  2418. `org-export-data'."
  2419. (let* ((type (org-element-property :type link))
  2420. (raw-path (org-element-property :path link))
  2421. ;; Ensure DESC really exists, or set it to nil.
  2422. (desc (and (not (string= desc "")) desc))
  2423. (imagep (org-export-inline-image-p
  2424. link org-odt-inline-image-rules))
  2425. (path (cond
  2426. ((member type '("http" "https" "ftp" "mailto"))
  2427. (concat type ":" raw-path))
  2428. ((string= type "file")
  2429. (if (file-name-absolute-p raw-path)
  2430. (concat "file://" (expand-file-name raw-path))
  2431. (concat "file://" raw-path)))
  2432. (t raw-path)))
  2433. ;; Convert & to &amp; for correct XML representation
  2434. (path (replace-regexp-in-string "&" "&amp;" path))
  2435. protocol)
  2436. (cond
  2437. ;; Image file.
  2438. ((and (not desc) (org-export-inline-image-p
  2439. link org-odt-inline-image-rules))
  2440. (org-odt-link--inline-image link info))
  2441. ;; Formula file.
  2442. ((and (not desc) (org-export-inline-image-p
  2443. link org-odt-inline-formula-rules))
  2444. (org-odt-link--inline-formula link info))
  2445. ;; Radio target: Transcode target's contents and use them as
  2446. ;; link's description.
  2447. ((string= type "radio")
  2448. (let ((destination (org-export-resolve-radio-link link info)))
  2449. (when destination
  2450. (let ((desc (org-export-data (org-element-contents destination) info))
  2451. (href (org-export-solidify-link-text path)))
  2452. (format
  2453. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2454. href desc)))))
  2455. ;; Links pointing to a headline: Find destination and build
  2456. ;; appropriate referencing command.
  2457. ((member type '("custom-id" "fuzzy" "id"))
  2458. (let ((destination (if (string= type "fuzzy")
  2459. (org-export-resolve-fuzzy-link link info)
  2460. (org-export-resolve-id-link link info))))
  2461. (case (org-element-type destination)
  2462. ;; Case 1: Fuzzy link points nowhere.
  2463. ('nil
  2464. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2465. "Emphasis"
  2466. (or desc
  2467. (org-export-data (org-element-property :raw-link link)
  2468. info))))
  2469. ;; Case 2: Fuzzy link points to a headline.
  2470. (headline
  2471. ;; If there's a description, create a hyperlink.
  2472. ;; Otherwise, try to provide a meaningful description.
  2473. (if (not desc) (org-odt-link--infer-description destination info)
  2474. (let* ((headline-no
  2475. (org-export-get-headline-number destination info))
  2476. (label
  2477. (format "sec-%s"
  2478. (mapconcat 'number-to-string headline-no "-"))))
  2479. (format
  2480. "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2481. label desc))))
  2482. ;; Case 3: Fuzzy link points to a target.
  2483. (target
  2484. ;; If there's a description, create a hyperlink.
  2485. ;; Otherwise, try to provide a meaningful description.
  2486. (if (not desc) (org-odt-link--infer-description destination info)
  2487. (let ((label (org-element-property :value destination)))
  2488. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2489. (org-export-solidify-link-text label)
  2490. desc))))
  2491. ;; Case 4: Fuzzy link points to some element (e.g., an
  2492. ;; inline image, a math formula or a table).
  2493. (otherwise
  2494. (let ((label-reference
  2495. (ignore-errors (org-odt-format-label
  2496. destination info 'reference))))
  2497. (cond ((not label-reference)
  2498. (org-odt-link--infer-description destination info))
  2499. ;; LINK has no description. Create
  2500. ;; a cross-reference showing entity's sequence
  2501. ;; number.
  2502. ((not desc) label-reference)
  2503. ;; LINK has description. Insert a hyperlink with
  2504. ;; user-provided description.
  2505. (t
  2506. (let ((label (org-element-property :name destination)))
  2507. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  2508. (org-export-solidify-link-text label)
  2509. desc)))))))))
  2510. ;; Coderef: replace link with the reference name or the
  2511. ;; equivalent line number.
  2512. ((string= type "coderef")
  2513. (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
  2514. (href (concat "coderef-" path)))
  2515. (format
  2516. (org-export-get-coderef-format path desc)
  2517. (format
  2518. "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2519. href line-no))))
  2520. ;; Link type is handled by a special function.
  2521. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2522. (funcall protocol (org-link-unescape path) desc 'odt))
  2523. ;; External link with a description part.
  2524. ((and path desc)
  2525. (let ((link-contents (org-element-contents link)))
  2526. ;; Check if description is a link to an inline image.
  2527. (if (and (not (cdr link-contents))
  2528. (let ((desc-element (car link-contents)))
  2529. (and (eq (org-element-type desc-element) 'link)
  2530. (org-export-inline-image-p
  2531. desc-element org-odt-inline-image-rules))))
  2532. ;; Format link as a clickable image.
  2533. (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  2534. path desc)
  2535. ;; Otherwise, format it as a regular link.
  2536. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2537. path desc))))
  2538. ;; External link without a description part.
  2539. (path
  2540. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2541. path path))
  2542. ;; No path, only description. Try to do something useful.
  2543. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2544. "Emphasis" desc)))))
  2545. ;;;; Node Property
  2546. (defun org-odt-node-property (node-property contents info)
  2547. "Transcode a NODE-PROPERTY element from Org to ODT.
  2548. CONTENTS is nil. INFO is a plist holding contextual
  2549. information."
  2550. (org-odt--encode-plain-text
  2551. (format "%s:%s"
  2552. (org-element-property :key node-property)
  2553. (let ((value (org-element-property :value node-property)))
  2554. (if value (concat " " value) "")))))
  2555. ;;;; Paragraph
  2556. (defun org-odt--format-paragraph (paragraph contents default center quote)
  2557. "Format paragraph according to given styles.
  2558. PARAGRAPH is a paragraph type element. CONTENTS is the
  2559. transcoded contents of that paragraph, as a string. DEFAULT,
  2560. CENTER and QUOTE are, respectively, style to use when paragraph
  2561. belongs to no special environment, a center block, or a quote
  2562. block."
  2563. (let* ((parent (org-export-get-parent paragraph))
  2564. (parent-type (org-element-type parent))
  2565. (style (case parent-type
  2566. (quote-block quote)
  2567. (center-block center)
  2568. (t default))))
  2569. ;; If this paragraph is a leading paragraph in an item and the
  2570. ;; item has a checkbox, splice the checkbox and paragraph contents
  2571. ;; together.
  2572. (when (and (eq (org-element-type parent) 'item)
  2573. (eq paragraph (car (org-element-contents parent))))
  2574. (setq contents (concat (org-odt--checkbox parent) contents)))
  2575. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2576. (defun org-odt-paragraph (paragraph contents info)
  2577. "Transcode a PARAGRAPH element from Org to ODT.
  2578. CONTENTS is the contents of the paragraph, as a string. INFO is
  2579. the plist used as a communication channel."
  2580. (org-odt--format-paragraph
  2581. paragraph contents
  2582. (or (org-element-property :style paragraph) "Text_20_body")
  2583. "OrgCenter"
  2584. "Quotations"))
  2585. ;;;; Plain List
  2586. (defun org-odt-plain-list (plain-list contents info)
  2587. "Transcode a PLAIN-LIST element from Org to ODT.
  2588. CONTENTS is the contents of the list. INFO is a plist holding
  2589. contextual information."
  2590. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2591. ;; Choose style based on list type.
  2592. (case (org-element-property :type plain-list)
  2593. (ordered "OrgNumberedList")
  2594. (unordered "OrgBulletedList")
  2595. (descriptive-1 "OrgDescriptionList")
  2596. (descriptive-2 "OrgDescriptionList"))
  2597. ;; If top-level list, re-start numbering. Otherwise,
  2598. ;; continue numbering.
  2599. (format "text:continue-numbering=\"%s\""
  2600. (let* ((parent (org-export-get-parent plain-list)))
  2601. (if (and parent (eq (org-element-type parent) 'item))
  2602. "true" "false")))
  2603. contents))
  2604. ;;;; Plain Text
  2605. (defun org-odt--encode-tabs-and-spaces (line)
  2606. (replace-regexp-in-string
  2607. "\\([\t]\\|\\([ ]+\\)\\)"
  2608. (lambda (s)
  2609. (cond
  2610. ((string= s "\t") "<text:tab/>")
  2611. (t (let ((n (length s)))
  2612. (cond
  2613. ((= n 1) " ")
  2614. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2615. (t ""))))))
  2616. line))
  2617. (defun org-odt--encode-plain-text (text &optional no-whitespace-filling)
  2618. (mapc
  2619. (lambda (pair)
  2620. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2621. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2622. (if no-whitespace-filling text
  2623. (org-odt--encode-tabs-and-spaces text)))
  2624. (defun org-odt-plain-text (text info)
  2625. "Transcode a TEXT string from Org to ODT.
  2626. TEXT is the string to transcode. INFO is a plist holding
  2627. contextual information."
  2628. (let ((output text))
  2629. ;; Protect &, < and >.
  2630. (setq output (org-odt--encode-plain-text output t))
  2631. ;; Handle smart quotes. Be sure to provide original string since
  2632. ;; OUTPUT may have been modified.
  2633. (when (plist-get info :with-smart-quotes)
  2634. (setq output (org-export-activate-smart-quotes output :utf-8 info text)))
  2635. ;; Convert special strings.
  2636. (when (plist-get info :with-special-strings)
  2637. (mapc
  2638. (lambda (pair)
  2639. (setq output
  2640. (replace-regexp-in-string (car pair) (cdr pair) output t nil)))
  2641. org-odt-special-string-regexps))
  2642. ;; Handle break preservation if required.
  2643. (when (plist-get info :preserve-breaks)
  2644. (setq output (replace-regexp-in-string
  2645. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>" output t)))
  2646. ;; Return value.
  2647. output))
  2648. ;;;; Planning
  2649. (defun org-odt-planning (planning contents info)
  2650. "Transcode a PLANNING element from Org to ODT.
  2651. CONTENTS is nil. INFO is a plist used as a communication
  2652. channel."
  2653. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2654. "OrgPlanning"
  2655. (concat
  2656. (let ((closed (org-element-property :closed planning)))
  2657. (when closed
  2658. (concat
  2659. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2660. "OrgClosedKeyword" org-closed-string)
  2661. (org-odt-timestamp closed contents info))))
  2662. (let ((deadline (org-element-property :deadline planning)))
  2663. (when deadline
  2664. (concat
  2665. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2666. "OrgDeadlineKeyword" org-deadline-string)
  2667. (org-odt-timestamp deadline contents info))))
  2668. (let ((scheduled (org-element-property :scheduled planning)))
  2669. (when scheduled
  2670. (concat
  2671. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2672. "OrgScheduledKeyword" org-deadline-string)
  2673. (org-odt-timestamp scheduled contents info)))))))
  2674. ;;;; Property Drawer
  2675. (defun org-odt-property-drawer (property-drawer contents info)
  2676. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2677. CONTENTS holds the contents of the drawer. INFO is a plist
  2678. holding contextual information."
  2679. (and (org-string-nw-p contents)
  2680. (format "<text:p text:style-name=\"OrgFixedWidthBlock\">%s</text:p>"
  2681. contents)))
  2682. ;;;; Quote Block
  2683. (defun org-odt-quote-block (quote-block contents info)
  2684. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2685. CONTENTS holds the contents of the block. INFO is a plist
  2686. holding contextual information."
  2687. contents)
  2688. ;;;; Quote Section
  2689. (defun org-odt-quote-section (quote-section contents info)
  2690. "Transcode a QUOTE-SECTION element from Org to ODT.
  2691. CONTENTS is nil. INFO is a plist holding contextual information."
  2692. (let ((value (org-remove-indentation
  2693. (org-element-property :value quote-section))))
  2694. (when value (org-odt-do-format-code value))))
  2695. ;;;; Section
  2696. (defun org-odt-format-section (text style &optional name)
  2697. (let ((default-name (car (org-odt-add-automatic-style "Section"))))
  2698. (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
  2699. style
  2700. (format "text:name=\"%s\"" (or name default-name))
  2701. text)))
  2702. (defun org-odt-section (section contents info) ; FIXME
  2703. "Transcode a SECTION element from Org to ODT.
  2704. CONTENTS holds the contents of the section. INFO is a plist
  2705. holding contextual information."
  2706. contents)
  2707. ;;;; Radio Target
  2708. (defun org-odt-radio-target (radio-target text info)
  2709. "Transcode a RADIO-TARGET object from Org to ODT.
  2710. TEXT is the text of the target. INFO is a plist holding
  2711. contextual information."
  2712. (org-odt--target
  2713. text (org-export-solidify-link-text
  2714. (org-element-property :value radio-target))))
  2715. ;;;; Special Block
  2716. (defun org-odt-special-block (special-block contents info)
  2717. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2718. CONTENTS holds the contents of the block. INFO is a plist
  2719. holding contextual information."
  2720. (let ((type (downcase (org-element-property :type special-block)))
  2721. (attributes (org-export-read-attribute :attr_odt special-block)))
  2722. (cond
  2723. ;; Annotation.
  2724. ((string= type "annotation")
  2725. (let* ((author (or (plist-get attributes :author)
  2726. (let ((author (plist-get info :author)))
  2727. (and author (org-export-data author info)))))
  2728. (date (or (plist-get attributes :date)
  2729. ;; FIXME: Is `car' right thing to do below?
  2730. (car (plist-get info :date)))))
  2731. (format "\n<text:p>%s</text:p>"
  2732. (format "<office:annotation>\n%s\n</office:annotation>"
  2733. (concat
  2734. (and author
  2735. (format "<dc:creator>%s</dc:creator>" author))
  2736. (and date
  2737. (format "<dc:date>%s</dc:date>"
  2738. (org-odt--format-timestamp date nil 'iso-date)))
  2739. contents)))))
  2740. ;; Textbox.
  2741. ((string= type "textbox")
  2742. (let ((width (plist-get attributes :width))
  2743. (height (plist-get attributes :height))
  2744. (style (plist-get attributes :style))
  2745. (extra (plist-get attributes :extra))
  2746. (anchor (plist-get attributes :anchor)))
  2747. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2748. "Text_20_body" (org-odt--textbox contents width height
  2749. style extra anchor))))
  2750. (t contents))))
  2751. ;;;; Src Block
  2752. (defun org-odt-hfy-face-to-css (fn)
  2753. "Create custom style for face FN.
  2754. When FN is the default face, use its foreground and background
  2755. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2756. use its color attribute to create a character style whose name
  2757. is obtained from FN. Currently all attributes of FN other than
  2758. color are ignored.
  2759. The style name for a face FN is derived using the following
  2760. operations on the face name in that order - de-dash, CamelCase
  2761. and prefix with \"OrgSrc\". For example,
  2762. `font-lock-function-name-face' is associated with
  2763. \"OrgSrcFontLockFunctionNameFace\"."
  2764. (let* ((css-list (hfy-face-to-style fn))
  2765. (style-name (concat "OrgSrc"
  2766. (mapconcat
  2767. 'capitalize (split-string
  2768. (hfy-face-or-def-to-name fn) "-")
  2769. "")))
  2770. (color-val (cdr (assoc "color" css-list)))
  2771. (background-color-val (cdr (assoc "background" css-list)))
  2772. (style (and org-odt-create-custom-styles-for-srcblocks
  2773. (cond
  2774. ((eq fn 'default)
  2775. (format org-odt-src-block-paragraph-format
  2776. background-color-val color-val))
  2777. (t
  2778. (format
  2779. "
  2780. <style:style style:name=\"%s\" style:family=\"text\">
  2781. <style:text-properties fo:color=\"%s\"/>
  2782. </style:style>" style-name color-val))))))
  2783. (cons style-name style)))
  2784. (defun org-odt-htmlfontify-string (line)
  2785. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2786. (hfy-html-quote-map '(("\"" "&quot;")
  2787. ("<" "&lt;")
  2788. ("&" "&amp;")
  2789. (">" "&gt;")
  2790. (" " "<text:s/>")
  2791. (" " "<text:tab/>")))
  2792. (hfy-face-to-css 'org-odt-hfy-face-to-css)
  2793. (hfy-optimisations-1 (copy-sequence hfy-optimisations))
  2794. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2795. 'body-text-only))
  2796. (hfy-begin-span-handler
  2797. (lambda (style text-block text-id text-begins-block-p)
  2798. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2799. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2800. (org-no-warnings (htmlfontify-string line))))
  2801. (defun org-odt-do-format-code
  2802. (code &optional lang refs retain-labels num-start)
  2803. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2804. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2805. (code-lines (org-split-string code "\n"))
  2806. (code-length (length code-lines))
  2807. (use-htmlfontify-p (and (functionp lang-mode)
  2808. org-odt-fontify-srcblocks
  2809. (require 'htmlfontify nil t)
  2810. (fboundp 'htmlfontify-string)))
  2811. (code (if (not use-htmlfontify-p) code
  2812. (with-temp-buffer
  2813. (insert code)
  2814. (funcall lang-mode)
  2815. (font-lock-fontify-buffer)
  2816. (buffer-string))))
  2817. (fontifier (if use-htmlfontify-p 'org-odt-htmlfontify-string
  2818. 'org-odt--encode-plain-text))
  2819. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2820. "OrgFixedWidthBlock"))
  2821. (i 0))
  2822. (assert (= code-length (length (org-split-string code "\n"))))
  2823. (setq code
  2824. (org-export-format-code
  2825. code
  2826. (lambda (loc line-num ref)
  2827. (setq par-style
  2828. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2829. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2830. (setq loc (funcall fontifier loc))
  2831. (when ref
  2832. (setq loc (org-odt--target loc (concat "coderef-" ref))))
  2833. (assert par-style)
  2834. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2835. par-style loc))
  2836. (if (not line-num) loc
  2837. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2838. num-start refs))
  2839. (cond
  2840. ((not num-start) code)
  2841. ((= num-start 0)
  2842. (format
  2843. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2844. " text:continue-numbering=\"false\"" code))
  2845. (t
  2846. (format
  2847. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2848. " text:continue-numbering=\"true\"" code)))))
  2849. (defun org-odt-format-code (element info)
  2850. (let* ((lang (org-element-property :language element))
  2851. ;; Extract code and references.
  2852. (code-info (org-export-unravel-code element))
  2853. (code (car code-info))
  2854. (refs (cdr code-info))
  2855. ;; Does the src block contain labels?
  2856. (retain-labels (org-element-property :retain-labels element))
  2857. ;; Does it have line numbers?
  2858. (num-start (case (org-element-property :number-lines element)
  2859. (continued (org-export-get-loc element info))
  2860. (new 0))))
  2861. (org-odt-do-format-code code lang refs retain-labels num-start)))
  2862. (defun org-odt-src-block (src-block contents info)
  2863. "Transcode a SRC-BLOCK element from Org to ODT.
  2864. CONTENTS holds the contents of the item. INFO is a plist holding
  2865. contextual information."
  2866. (let* ((lang (org-element-property :language src-block))
  2867. (attributes (org-export-read-attribute :attr_odt src-block))
  2868. (captions (org-odt-format-label src-block info 'definition))
  2869. (caption (car captions)) (short-caption (cdr captions)))
  2870. (concat
  2871. (and caption
  2872. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2873. "Listing" caption))
  2874. (let ((--src-block (org-odt-format-code src-block info)))
  2875. (if (not (plist-get attributes :textbox)) --src-block
  2876. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2877. "Text_20_body"
  2878. (org-odt--textbox --src-block nil nil nil)))))))
  2879. ;;;; Statistics Cookie
  2880. (defun org-odt-statistics-cookie (statistics-cookie contents info)
  2881. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2882. CONTENTS is nil. INFO is a plist holding contextual information."
  2883. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2884. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2885. "OrgCode" cookie-value)))
  2886. ;;;; Strike-Through
  2887. (defun org-odt-strike-through (strike-through contents info)
  2888. "Transcode STRIKE-THROUGH from Org to ODT.
  2889. CONTENTS is the text with strike-through markup. INFO is a plist
  2890. holding contextual information."
  2891. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2892. "Strikethrough" contents))
  2893. ;;;; Subscript
  2894. (defun org-odt-subscript (subscript contents info)
  2895. "Transcode a SUBSCRIPT object from Org to ODT.
  2896. CONTENTS is the contents of the object. INFO is a plist holding
  2897. contextual information."
  2898. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2899. "OrgSubscript" contents))
  2900. ;;;; Superscript
  2901. (defun org-odt-superscript (superscript contents info)
  2902. "Transcode a SUPERSCRIPT object from Org to ODT.
  2903. CONTENTS is the contents of the object. INFO is a plist holding
  2904. contextual information."
  2905. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2906. "OrgSuperscript" contents))
  2907. ;;;; Table Cell
  2908. (defun org-odt-table-style-spec (element info)
  2909. (let* ((table (org-export-get-parent-table element))
  2910. (table-attributes (org-export-read-attribute :attr_odt table))
  2911. (table-style (plist-get table-attributes :style)))
  2912. (assoc table-style org-odt-table-styles)))
  2913. (defun org-odt-get-table-cell-styles (table-cell info)
  2914. "Retrieve styles applicable to a table cell.
  2915. R and C are (zero-based) row and column numbers of the table
  2916. cell. STYLE-SPEC is an entry in `org-odt-table-styles'
  2917. applicable to the current table. It is `nil' if the table is not
  2918. associated with any style attributes.
  2919. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2920. When STYLE-SPEC is nil, style the table cell the conventional way
  2921. - choose cell borders based on row and column groupings and
  2922. choose paragraph alignment based on `org-col-cookies' text
  2923. property. See also
  2924. `org-odt-get-paragraph-style-cookie-for-table-cell'.
  2925. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2926. styles congruent with the ODF-1.2 specification."
  2927. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2928. (r (car table-cell-address)) (c (cdr table-cell-address))
  2929. (style-spec (org-odt-table-style-spec table-cell info))
  2930. (table-dimensions (org-export-table-dimensions
  2931. (org-export-get-parent-table table-cell)
  2932. info)))
  2933. (when style-spec
  2934. ;; LibreOffice - particularly the Writer - honors neither table
  2935. ;; templates nor custom table-cell styles. Inorder to retain
  2936. ;; inter-operability with LibreOffice, only automatic styles are
  2937. ;; used for styling of table-cells. The current implementation is
  2938. ;; congruent with ODF-1.2 specification and hence is
  2939. ;; future-compatible.
  2940. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2941. ;; which recognizes as many as 16 different cell types - is much
  2942. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2943. ;; by hand.
  2944. (let* ((template-name (nth 1 style-spec))
  2945. (cell-style-selectors (nth 2 style-spec))
  2946. (cell-type
  2947. (cond
  2948. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2949. (= c 0)) "FirstColumn")
  2950. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2951. (= (1+ c) (cdr table-dimensions)))
  2952. "LastColumn")
  2953. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2954. (= r 0)) "FirstRow")
  2955. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2956. (= (1+ r) (car table-dimensions)))
  2957. "LastRow")
  2958. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2959. (= (% r 2) 1)) "EvenRow")
  2960. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2961. (= (% r 2) 0)) "OddRow")
  2962. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2963. (= (% c 2) 1)) "EvenColumn")
  2964. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2965. (= (% c 2) 0)) "OddColumn")
  2966. (t ""))))
  2967. (concat template-name cell-type)))))
  2968. (defun org-odt-table-cell (table-cell contents info)
  2969. "Transcode a TABLE-CELL element from Org to ODT.
  2970. CONTENTS is nil. INFO is a plist used as a communication
  2971. channel."
  2972. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2973. (r (car table-cell-address))
  2974. (c (cdr table-cell-address))
  2975. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2976. (table-row (org-export-get-parent table-cell))
  2977. (custom-style-prefix (org-odt-get-table-cell-styles
  2978. table-cell info))
  2979. (paragraph-style
  2980. (or
  2981. (and custom-style-prefix
  2982. (format "%sTableParagraph" custom-style-prefix))
  2983. (concat
  2984. (cond
  2985. ((and (= 1 (org-export-table-row-group table-row info))
  2986. (org-export-table-has-header-p
  2987. (org-export-get-parent-table table-row) info))
  2988. "OrgTableHeading")
  2989. ((let* ((table (org-export-get-parent-table table-cell))
  2990. (table-attrs (org-export-read-attribute :attr_odt table))
  2991. (table-header-columns
  2992. (let ((cols (plist-get table-attrs :header-columns)))
  2993. (and cols (read cols)))))
  2994. (<= c (cond ((wholenump table-header-columns)
  2995. (- table-header-columns 1))
  2996. (table-header-columns 0)
  2997. (t -1))))
  2998. "OrgTableHeading")
  2999. (t "OrgTableContents"))
  3000. (capitalize (symbol-name (org-export-table-cell-alignment
  3001. table-cell info))))))
  3002. (cell-style-name
  3003. (or
  3004. (and custom-style-prefix (format "%sTableCell"
  3005. custom-style-prefix))
  3006. (concat
  3007. "OrgTblCell"
  3008. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  3009. (zerop r)) "T")
  3010. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  3011. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  3012. (not (zerop c)) ) "L"))))
  3013. (cell-attributes
  3014. (concat
  3015. (format " table:style-name=\"%s\"" cell-style-name)
  3016. (and (> horiz-span 0)
  3017. (format " table:number-columns-spanned=\"%d\""
  3018. (1+ horiz-span))))))
  3019. (unless contents (setq contents ""))
  3020. (concat
  3021. (assert paragraph-style)
  3022. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  3023. cell-attributes
  3024. (let ((table-cell-contents (org-element-contents table-cell)))
  3025. (if (memq (org-element-type (car table-cell-contents))
  3026. org-element-all-elements)
  3027. contents
  3028. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3029. paragraph-style contents))))
  3030. (let (s)
  3031. (dotimes (i horiz-span s)
  3032. (setq s (concat s "\n<table:covered-table-cell/>"))))
  3033. "\n")))
  3034. ;;;; Table Row
  3035. (defun org-odt-table-row (table-row contents info)
  3036. "Transcode a TABLE-ROW element from Org to ODT.
  3037. CONTENTS is the contents of the row. INFO is a plist used as a
  3038. communication channel."
  3039. ;; Rules are ignored since table separators are deduced from
  3040. ;; borders of the current row.
  3041. (when (eq (org-element-property :type table-row) 'standard)
  3042. (let* ((rowgroup-tags
  3043. (if (and (= 1 (org-export-table-row-group table-row info))
  3044. (org-export-table-has-header-p
  3045. (org-export-get-parent-table table-row) info))
  3046. ;; If the row belongs to the first rowgroup and the
  3047. ;; table has more than one row groups, then this row
  3048. ;; belongs to the header row group.
  3049. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  3050. ;; Otherwise, it belongs to non-header row group.
  3051. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  3052. (concat
  3053. ;; Does this row begin a rowgroup?
  3054. (when (org-export-table-row-starts-rowgroup-p table-row info)
  3055. (car rowgroup-tags))
  3056. ;; Actual table row
  3057. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  3058. ;; Does this row end a rowgroup?
  3059. (when (org-export-table-row-ends-rowgroup-p table-row info)
  3060. (cdr rowgroup-tags))))))
  3061. ;;;; Table
  3062. (defun org-odt-table-first-row-data-cells (table info)
  3063. (let ((table-row
  3064. (org-element-map table 'table-row
  3065. (lambda (row)
  3066. (unless (eq (org-element-property :type row) 'rule) row))
  3067. info 'first-match))
  3068. (special-column-p (org-export-table-has-special-column-p table)))
  3069. (if (not special-column-p) (org-element-contents table-row)
  3070. (cdr (org-element-contents table-row)))))
  3071. (defun org-odt--table (table contents info)
  3072. "Transcode a TABLE element from Org to ODT.
  3073. CONTENTS is the contents of the table. INFO is a plist holding
  3074. contextual information."
  3075. (case (org-element-property :type table)
  3076. ;; Case 1: table.el doesn't support export to OD format. Strip
  3077. ;; such tables from export.
  3078. (table.el
  3079. (prog1 nil
  3080. (message
  3081. (concat
  3082. "(ox-odt): Found table.el-type table in the source Org file."
  3083. " table.el doesn't support export to ODT format."
  3084. " Stripping the table from export."))))
  3085. ;; Case 2: Native Org tables.
  3086. (otherwise
  3087. (let* ((captions (org-odt-format-label table info 'definition))
  3088. (caption (car captions)) (short-caption (cdr captions))
  3089. (attributes (org-export-read-attribute :attr_odt table))
  3090. (custom-table-style (nth 1 (org-odt-table-style-spec table info)))
  3091. (table-column-specs
  3092. (function
  3093. (lambda (table info)
  3094. (let* ((table-style (or custom-table-style "OrgTable"))
  3095. (column-style (format "%sColumn" table-style)))
  3096. (mapconcat
  3097. (lambda (table-cell)
  3098. (let ((width (1+ (or (org-export-table-cell-width
  3099. table-cell info) 0)))
  3100. (s (format
  3101. "\n<table:table-column table:style-name=\"%s\"/>"
  3102. column-style))
  3103. out)
  3104. (dotimes (i width out) (setq out (concat s out)))))
  3105. (org-odt-table-first-row-data-cells table info) "\n"))))))
  3106. (concat
  3107. ;; caption.
  3108. (when caption
  3109. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3110. "Table" caption))
  3111. ;; begin table.
  3112. (let* ((automatic-name
  3113. (org-odt-add-automatic-style "Table" attributes)))
  3114. (format
  3115. "\n<table:table table:style-name=\"%s\"%s>"
  3116. (or custom-table-style (cdr automatic-name) "OrgTable")
  3117. (concat (when short-caption
  3118. (format " table:name=\"%s\"" short-caption)))))
  3119. ;; column specification.
  3120. (funcall table-column-specs table info)
  3121. ;; actual contents.
  3122. "\n" contents
  3123. ;; end table.
  3124. "</table:table>")))))
  3125. (defun org-odt-table (table contents info)
  3126. "Transcode a TABLE element from Org to ODT.
  3127. CONTENTS is the contents of the table. INFO is a plist holding
  3128. contextual information.
  3129. Use `org-odt--table' to typeset the table. Handle details
  3130. pertaining to indentation here."
  3131. (let* ((--element-preceded-by-table-p
  3132. (function
  3133. (lambda (element info)
  3134. (loop for el in (org-export-get-previous-element element info t)
  3135. thereis (eq (org-element-type el) 'table)))))
  3136. (--walk-list-genealogy-and-collect-tags
  3137. (function
  3138. (lambda (table info)
  3139. (let* ((genealogy (org-export-get-genealogy table))
  3140. (list-genealogy
  3141. (when (eq (org-element-type (car genealogy)) 'item)
  3142. (loop for el in genealogy
  3143. when (memq (org-element-type el)
  3144. '(item plain-list))
  3145. collect el)))
  3146. (llh-genealogy
  3147. (apply 'nconc
  3148. (loop for el in genealogy
  3149. when (and (eq (org-element-type el) 'headline)
  3150. (org-export-low-level-p el info))
  3151. collect
  3152. (list el
  3153. (assq 'headline
  3154. (org-element-contents
  3155. (org-export-get-parent el)))))))
  3156. parent-list)
  3157. (nconc
  3158. ;; Handle list genealogy.
  3159. (loop for el in list-genealogy collect
  3160. (case (org-element-type el)
  3161. (plain-list
  3162. (setq parent-list el)
  3163. (cons "</text:list>"
  3164. (format "\n<text:list text:style-name=\"%s\" %s>"
  3165. (case (org-element-property :type el)
  3166. (ordered "OrgNumberedList")
  3167. (unordered "OrgBulletedList")
  3168. (descriptive-1 "OrgDescriptionList")
  3169. (descriptive-2 "OrgDescriptionList"))
  3170. "text:continue-numbering=\"true\"")))
  3171. (item
  3172. (cond
  3173. ((not parent-list)
  3174. (if (funcall --element-preceded-by-table-p table info)
  3175. '("</text:list-header>" . "<text:list-header>")
  3176. '("</text:list-item>" . "<text:list-header>")))
  3177. ((funcall --element-preceded-by-table-p
  3178. parent-list info)
  3179. '("</text:list-header>" . "<text:list-header>"))
  3180. (t '("</text:list-item>" . "<text:list-item>"))))))
  3181. ;; Handle low-level headlines.
  3182. (loop for el in llh-genealogy
  3183. with step = 'item collect
  3184. (case step
  3185. (plain-list
  3186. (setq step 'item) ; Flip-flop
  3187. (setq parent-list el)
  3188. (cons "</text:list>"
  3189. (format "\n<text:list text:style-name=\"%s\" %s>"
  3190. (if (org-export-numbered-headline-p
  3191. el info)
  3192. "OrgNumberedList"
  3193. "OrgBulletedList")
  3194. "text:continue-numbering=\"true\"")))
  3195. (item
  3196. (setq step 'plain-list) ; Flip-flop
  3197. (cond
  3198. ((not parent-list)
  3199. (if (funcall --element-preceded-by-table-p table info)
  3200. '("</text:list-header>" . "<text:list-header>")
  3201. '("</text:list-item>" . "<text:list-header>")))
  3202. ((let ((section? (org-export-get-previous-element
  3203. parent-list info)))
  3204. (and section?
  3205. (eq (org-element-type section?) 'section)
  3206. (assq 'table (org-element-contents section?))))
  3207. '("</text:list-header>" . "<text:list-header>"))
  3208. (t
  3209. '("</text:list-item>" . "<text:list-item>")))))))))))
  3210. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  3211. table info)))
  3212. ;; OpenDocument schema does not permit table to occur within a
  3213. ;; list item.
  3214. ;; One solution - the easiest and lightweight, in terms of
  3215. ;; implementation - is to put the table in an indented text box
  3216. ;; and make the text box part of the list-item. Unfortunately if
  3217. ;; the table is big and spans multiple pages, the text box could
  3218. ;; overflow. In this case, the following attribute will come
  3219. ;; handy.
  3220. ;; ,---- From OpenDocument-v1.1.pdf
  3221. ;; | 15.27.28 Overflow behavior
  3222. ;; |
  3223. ;; | For text boxes contained within text document, the
  3224. ;; | style:overflow-behavior property specifies the behavior of text
  3225. ;; | boxes where the containing text does not fit into the text
  3226. ;; | box.
  3227. ;; |
  3228. ;; | If the attribute's value is clip, the text that does not fit
  3229. ;; | into the text box is not displayed.
  3230. ;; |
  3231. ;; | If the attribute value is auto-create-new-frame, a new frame
  3232. ;; | will be created on the next page, with the same position and
  3233. ;; | dimensions of the original frame.
  3234. ;; |
  3235. ;; | If the style:overflow-behavior property's value is
  3236. ;; | auto-create-new-frame and the text box has a minimum width or
  3237. ;; | height specified, then the text box will grow until the page
  3238. ;; | bounds are reached before a new frame is created.
  3239. ;; `----
  3240. ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
  3241. ;; auto-create-new-frame property and always resorts to clipping
  3242. ;; the text box. This results in table being truncated.
  3243. ;; So we solve the problem the hard (and fun) way using list
  3244. ;; continuations.
  3245. ;; The problem only becomes more interesting if you take in to
  3246. ;; account the following facts:
  3247. ;;
  3248. ;; - Description lists are simulated as plain lists.
  3249. ;; - Low-level headlines can be listified.
  3250. ;; - In Org-mode, a table can occur not only as a regular list
  3251. ;; item, but also within description lists and low-level
  3252. ;; headlines.
  3253. ;; See `org-odt-translate-description-lists' and
  3254. ;; `org-odt-translate-low-level-headlines' for how this is
  3255. ;; tackled.
  3256. (concat "\n"
  3257. ;; Discontinue the list.
  3258. (mapconcat 'car close-open-tags "\n")
  3259. ;; Put the table in an indented section.
  3260. (let* ((table (org-odt--table table contents info))
  3261. (level (/ (length (mapcar 'car close-open-tags)) 2))
  3262. (style (format "OrgIndentedSection-Level-%d" level)))
  3263. (when table (org-odt-format-section table style)))
  3264. ;; Continue the list.
  3265. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  3266. ;;;; Target
  3267. (defun org-odt-target (target contents info)
  3268. "Transcode a TARGET object from Org to ODT.
  3269. CONTENTS is nil. INFO is a plist holding contextual
  3270. information."
  3271. (let ((value (org-element-property :value target)))
  3272. (org-odt--target "" (org-export-solidify-link-text value))))
  3273. ;;;; Timestamp
  3274. (defun org-odt-timestamp (timestamp contents info)
  3275. "Transcode a TIMESTAMP object from Org to ODT.
  3276. CONTENTS is nil. INFO is a plist used as a communication
  3277. channel."
  3278. (let* ((raw-value (org-element-property :raw-value timestamp))
  3279. (type (org-element-property :type timestamp)))
  3280. (if (not org-odt-use-date-fields)
  3281. (let ((value (org-odt-plain-text
  3282. (org-timestamp-translate timestamp) info)))
  3283. (case (org-element-property :type timestamp)
  3284. ((active active-range)
  3285. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3286. "OrgActiveTimestamp" value))
  3287. ((inactive inactive-range)
  3288. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3289. "OrgInactiveTimestamp" value))
  3290. (otherwise value)))
  3291. (case type
  3292. (active
  3293. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3294. "OrgActiveTimestamp"
  3295. (format "&lt;%s&gt;" (org-odt--format-timestamp timestamp))))
  3296. (inactive
  3297. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3298. "OrgInactiveTimestamp"
  3299. (format "[%s]" (org-odt--format-timestamp timestamp))))
  3300. (active-range
  3301. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3302. "OrgActiveTimestamp"
  3303. (format "&lt;%s&gt;&#x2013;&lt;%s&gt;"
  3304. (org-odt--format-timestamp timestamp)
  3305. (org-odt--format-timestamp timestamp 'end))))
  3306. (inactive-range
  3307. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3308. "OrgInactiveTimestamp"
  3309. (format "[%s]&#x2013;[%s]"
  3310. (org-odt--format-timestamp timestamp)
  3311. (org-odt--format-timestamp timestamp 'end))))
  3312. (otherwise
  3313. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3314. "OrgDiaryTimestamp"
  3315. (org-odt-plain-text (org-timestamp-translate timestamp)
  3316. info)))))))
  3317. ;;;; Underline
  3318. (defun org-odt-underline (underline contents info)
  3319. "Transcode UNDERLINE from Org to ODT.
  3320. CONTENTS is the text with underline markup. INFO is a plist
  3321. holding contextual information."
  3322. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3323. "Underline" contents))
  3324. ;;;; Verbatim
  3325. (defun org-odt-verbatim (verbatim contents info)
  3326. "Transcode a VERBATIM object from Org to ODT.
  3327. CONTENTS is nil. INFO is a plist used as a communication
  3328. channel."
  3329. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3330. "OrgCode" (org-odt--encode-plain-text
  3331. (org-element-property :value verbatim))))
  3332. ;;;; Verse Block
  3333. (defun org-odt-verse-block (verse-block contents info)
  3334. "Transcode a VERSE-BLOCK element from Org to ODT.
  3335. CONTENTS is verse block contents. INFO is a plist holding
  3336. contextual information."
  3337. ;; Add line breaks to each line of verse.
  3338. (setq contents (replace-regexp-in-string
  3339. "\\(<text:line-break/>\\)?[ \t]*\n"
  3340. "<text:line-break/>" contents))
  3341. ;; Replace tabs and spaces.
  3342. (setq contents (org-odt--encode-tabs-and-spaces contents))
  3343. ;; Surround it in a verse environment.
  3344. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3345. "OrgVerse" contents))
  3346. ;;; Filters
  3347. ;;;; LaTeX fragments
  3348. (defun org-odt--translate-latex-fragments (tree backend info)
  3349. (let ((processing-type (plist-get info :with-latex))
  3350. (count 0))
  3351. ;; Normalize processing-type to one of dvipng, mathml or verbatim.
  3352. ;; If the desired converter is not available, force verbatim
  3353. ;; processing.
  3354. (case processing-type
  3355. ((t mathml)
  3356. (if (and (fboundp 'org-format-latex-mathml-available-p)
  3357. (org-format-latex-mathml-available-p))
  3358. (setq processing-type 'mathml)
  3359. (message "LaTeX to MathML converter not available.")
  3360. (setq processing-type 'verbatim)))
  3361. ((dvipng imagemagick)
  3362. (unless (and (org-check-external-command "latex" "" t)
  3363. (org-check-external-command
  3364. (if (eq processing-type 'dvipng) "dvipng" "convert") "" t))
  3365. (message "LaTeX to PNG converter not available.")
  3366. (setq processing-type 'verbatim)))
  3367. (otherwise
  3368. (message "Unknown LaTeX option. Forcing verbatim.")
  3369. (setq processing-type 'verbatim)))
  3370. ;; Store normalized value for later use.
  3371. (when (plist-get info :with-latex)
  3372. (plist-put info :with-latex processing-type))
  3373. (message "Formatting LaTeX using %s" processing-type)
  3374. ;; Convert `latex-fragment's and `latex-environment's.
  3375. (when (memq processing-type '(mathml dvipng imagemagick))
  3376. (org-element-map tree '(latex-fragment latex-environment)
  3377. (lambda (latex-*)
  3378. (incf count)
  3379. (let* ((latex-frag (org-element-property :value latex-*))
  3380. (input-file (plist-get info :input-file))
  3381. (cache-dir (file-name-directory input-file))
  3382. (cache-subdir (concat
  3383. (case processing-type
  3384. ((dvipng imagemagick) "ltxpng/")
  3385. (mathml "ltxmathml/"))
  3386. (file-name-sans-extension
  3387. (file-name-nondirectory input-file))))
  3388. (display-msg
  3389. (case processing-type
  3390. ((dvipng imagemagick) (format "Creating LaTeX Image %d..." count))
  3391. (mathml (format "Creating MathML snippet %d..." count))))
  3392. ;; Get an Org-style link to PNG image or the MathML
  3393. ;; file.
  3394. (org-link
  3395. (let ((link (with-temp-buffer
  3396. (insert latex-frag)
  3397. (org-format-latex cache-subdir cache-dir
  3398. nil display-msg
  3399. nil nil processing-type)
  3400. (buffer-substring-no-properties
  3401. (point-min) (point-max)))))
  3402. (if (not (string-match "file:\\([^]]*\\)" link))
  3403. (prog1 nil (message "LaTeX Conversion failed."))
  3404. link))))
  3405. (when org-link
  3406. ;; Conversion succeeded. Parse above Org-style link to a
  3407. ;; `link' object.
  3408. (let* ((link (car (org-element-map (with-temp-buffer
  3409. (org-mode)
  3410. (insert org-link)
  3411. (org-element-parse-buffer))
  3412. 'link 'identity))))
  3413. ;; Orphan the link.
  3414. (org-element-put-property link :parent nil)
  3415. (let* (
  3416. (replacement
  3417. (case (org-element-type latex-*)
  3418. ;; Case 1: LaTeX environment.
  3419. ;; Mimic a "standalone image or formula" by
  3420. ;; enclosing the `link' in a `paragraph'.
  3421. ;; Copy over original attributes, captions to
  3422. ;; the enclosing paragraph.
  3423. (latex-environment
  3424. (org-element-adopt-elements
  3425. (list 'paragraph
  3426. (list :style "OrgFormula"
  3427. :name (org-element-property :name
  3428. latex-*)
  3429. :caption (org-element-property :caption
  3430. latex-*)))
  3431. link))
  3432. ;; Case 2: LaTeX fragment.
  3433. ;; No special action.
  3434. (latex-fragment link))))
  3435. ;; Note down the object that link replaces.
  3436. (org-element-put-property replacement :replaces
  3437. (list (org-element-type latex-*)
  3438. (list :value latex-frag)))
  3439. ;; Replace now.
  3440. (org-element-set-element latex-* replacement))))))
  3441. info)))
  3442. tree)
  3443. ;;;; Description lists
  3444. ;; This translator is necessary to handle indented tables in a uniform
  3445. ;; manner. See comment in `org-odt--table'.
  3446. (defun org-odt--translate-description-lists (tree backend info)
  3447. ;; OpenDocument has no notion of a description list. So simulate it
  3448. ;; using plain lists. Description lists in the exported document
  3449. ;; are typeset in the same manner as they are in a typical HTML
  3450. ;; document.
  3451. ;;
  3452. ;; Specifically, a description list like this:
  3453. ;;
  3454. ;; ,----
  3455. ;; | - term-1 :: definition-1
  3456. ;; | - term-2 :: definition-2
  3457. ;; `----
  3458. ;;
  3459. ;; gets translated in to the following form:
  3460. ;;
  3461. ;; ,----
  3462. ;; | - term-1
  3463. ;; | - definition-1
  3464. ;; | - term-2
  3465. ;; | - definition-2
  3466. ;; `----
  3467. ;;
  3468. ;; Further effect is achieved by fixing the OD styles as below:
  3469. ;;
  3470. ;; 1. Set the :type property of the simulated lists to
  3471. ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
  3472. ;; that has *no* bullets whatsoever.
  3473. ;;
  3474. ;; 2. The paragraph containing the definition term is styled to be
  3475. ;; in bold.
  3476. ;;
  3477. (org-element-map tree 'plain-list
  3478. (lambda (el)
  3479. (when (equal (org-element-property :type el) 'descriptive)
  3480. (org-element-set-element
  3481. el
  3482. (apply 'org-element-adopt-elements
  3483. (list 'plain-list (list :type 'descriptive-1))
  3484. (mapcar
  3485. (lambda (item)
  3486. (org-element-adopt-elements
  3487. (list 'item (list :checkbox (org-element-property
  3488. :checkbox item)))
  3489. (list 'paragraph (list :style "Text_20_body_20_bold")
  3490. (or (org-element-property :tag item) "(no term)"))
  3491. (org-element-adopt-elements
  3492. (list 'plain-list (list :type 'descriptive-2))
  3493. (apply 'org-element-adopt-elements
  3494. (list 'item nil)
  3495. (org-element-contents item)))))
  3496. (org-element-contents el)))))
  3497. nil)
  3498. info)
  3499. tree)
  3500. ;;;; List tables
  3501. ;; Lists that are marked with attribute `:list-table' are called as
  3502. ;; list tables. They will be rendered as a table within the exported
  3503. ;; document.
  3504. ;; Consider an example. The following list table
  3505. ;;
  3506. ;; #+attr_odt :list-table t
  3507. ;; - Row 1
  3508. ;; - 1.1
  3509. ;; - 1.2
  3510. ;; - 1.3
  3511. ;; - Row 2
  3512. ;; - 2.1
  3513. ;; - 2.2
  3514. ;; - 2.3
  3515. ;;
  3516. ;; will be exported as though it were an Org table like the one show
  3517. ;; below.
  3518. ;;
  3519. ;; | Row 1 | 1.1 | 1.2 | 1.3 |
  3520. ;; | Row 2 | 2.1 | 2.2 | 2.3 |
  3521. ;;
  3522. ;; Note that org-tables are NOT multi-line and each line is mapped to
  3523. ;; a unique row in the exported document. So if an exported table
  3524. ;; needs to contain a single paragraph (with copious text) it needs to
  3525. ;; be typed up in a single line. Editing such long lines using the
  3526. ;; table editor will be a cumbersome task. Furthermore inclusion of
  3527. ;; multi-paragraph text in a table cell is well-nigh impossible.
  3528. ;;
  3529. ;; A LIST-TABLE circumvents above problems.
  3530. ;;
  3531. ;; Note that in the example above the list items could be paragraphs
  3532. ;; themselves and the list can be arbitrarily deep.
  3533. ;;
  3534. ;; Inspired by following thread:
  3535. ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
  3536. ;; Translate lists to tables
  3537. (defun org-odt--translate-list-tables (tree backend info)
  3538. (org-element-map tree 'plain-list
  3539. (lambda (l1-list)
  3540. (when (org-export-read-attribute :attr_odt l1-list :list-table)
  3541. ;; Replace list with table.
  3542. (org-element-set-element
  3543. l1-list
  3544. ;; Build replacement table.
  3545. (apply 'org-element-adopt-elements
  3546. (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
  3547. (org-element-map l1-list 'item
  3548. (lambda (l1-item)
  3549. (let* ((l1-item-contents (org-element-contents l1-item))
  3550. l1-item-leading-text l2-list)
  3551. ;; Remove Level-2 list from the Level-item. It
  3552. ;; will be subsequently attached as table-cells.
  3553. (let ((cur l1-item-contents) prev)
  3554. (while (and cur (not (eq (org-element-type (car cur))
  3555. 'plain-list)))
  3556. (setq prev cur)
  3557. (setq cur (cdr cur)))
  3558. (when prev
  3559. (setcdr prev nil)
  3560. (setq l2-list (car cur)))
  3561. (setq l1-item-leading-text l1-item-contents))
  3562. ;; Level-1 items start a table row.
  3563. (apply 'org-element-adopt-elements
  3564. (list 'table-row (list :type 'standard))
  3565. ;; Leading text of level-1 item define
  3566. ;; the first table-cell.
  3567. (apply 'org-element-adopt-elements
  3568. (list 'table-cell nil)
  3569. l1-item-leading-text)
  3570. ;; Level-2 items define subsequent
  3571. ;; table-cells of the row.
  3572. (org-element-map l2-list 'item
  3573. (lambda (l2-item)
  3574. (apply 'org-element-adopt-elements
  3575. (list 'table-cell nil)
  3576. (org-element-contents l2-item)))
  3577. info nil 'item))))
  3578. info nil 'item))))
  3579. nil)
  3580. info)
  3581. tree)
  3582. ;;; Interactive functions
  3583. (defun org-odt-create-manifest-file-entry (&rest args)
  3584. (push args org-odt-manifest-file-entries))
  3585. (defun org-odt-write-manifest-file ()
  3586. (make-directory (concat org-odt-zip-dir "META-INF"))
  3587. (let ((manifest-file (concat org-odt-zip-dir "META-INF/manifest.xml")))
  3588. (with-current-buffer
  3589. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3590. (find-file-noselect manifest-file t))
  3591. (insert
  3592. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  3593. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  3594. (mapc
  3595. (lambda (file-entry)
  3596. (let* ((version (nth 2 file-entry))
  3597. (extra (if (not version) ""
  3598. (format " manifest:version=\"%s\"" version))))
  3599. (insert
  3600. (format org-odt-manifest-file-entry-tag
  3601. (nth 0 file-entry) (nth 1 file-entry) extra))))
  3602. org-odt-manifest-file-entries)
  3603. (insert "\n</manifest:manifest>"))))
  3604. (defmacro org-odt--export-wrap (out-file &rest body)
  3605. `(let* ((--out-file ,out-file)
  3606. (out-file-type (file-name-extension --out-file))
  3607. (org-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  3608. "meta.xml" "styles.xml"))
  3609. ;; Initialize temporary workarea. All files that end up in
  3610. ;; the exported document get parked/created here.
  3611. (org-odt-zip-dir (file-name-as-directory
  3612. (make-temp-file (format "%s-" out-file-type) t)))
  3613. (org-odt-manifest-file-entries nil)
  3614. (--cleanup-xml-buffers
  3615. (function
  3616. (lambda nil
  3617. ;; Kill all XML buffers.
  3618. (mapc (lambda (file)
  3619. (let ((buf (find-buffer-visiting
  3620. (concat org-odt-zip-dir file))))
  3621. (when buf
  3622. (with-current-buffer buf
  3623. (set-buffer-modified-p nil)
  3624. (kill-buffer buf)))))
  3625. org-odt-xml-files)
  3626. ;; Delete temporary directory and also other embedded
  3627. ;; files that get copied there.
  3628. (delete-directory org-odt-zip-dir t)))))
  3629. (condition-case err
  3630. (progn
  3631. (unless (executable-find "zip")
  3632. ;; Not at all OSes ship with zip by default
  3633. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3634. ;; Do export. This creates a bunch of xml files ready to be
  3635. ;; saved and zipped.
  3636. (progn ,@body)
  3637. ;; Create a manifest entry for content.xml.
  3638. (org-odt-create-manifest-file-entry "text/xml" "content.xml")
  3639. ;; Write mimetype file
  3640. (let* ((mimetypes
  3641. '(("odt" . "application/vnd.oasis.opendocument.text")
  3642. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3643. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3644. (unless mimetype
  3645. (error "Unknown OpenDocument backend %S" out-file-type))
  3646. (write-region mimetype nil (concat org-odt-zip-dir "mimetype"))
  3647. (org-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3648. ;; Write out the manifest entries before zipping
  3649. (org-odt-write-manifest-file)
  3650. ;; Save all XML files.
  3651. (mapc (lambda (file)
  3652. (let ((buf (find-buffer-visiting
  3653. (concat org-odt-zip-dir file))))
  3654. (when buf
  3655. (with-current-buffer buf
  3656. ;; Prettify output if needed.
  3657. (when org-odt-prettify-xml
  3658. (indent-region (point-min) (point-max)))
  3659. (save-buffer 0)))))
  3660. org-odt-xml-files)
  3661. ;; Run zip.
  3662. (let* ((target --out-file)
  3663. (target-name (file-name-nondirectory target))
  3664. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3665. ("zip" "-rmTq" ,target-name "."))))
  3666. ;; If a file with same name as the desired output file
  3667. ;; exists, remove it.
  3668. (when (file-exists-p target)
  3669. (delete-file target))
  3670. ;; Zip up the xml files.
  3671. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3672. (message "Creating ODT file...")
  3673. ;; Switch temporarily to content.xml. This way Zip
  3674. ;; process will inherit `org-odt-zip-dir' as the current
  3675. ;; directory.
  3676. (with-current-buffer
  3677. (find-file-noselect (concat org-odt-zip-dir "content.xml") t)
  3678. (mapc
  3679. (lambda (cmd)
  3680. (message "Running %s" (mapconcat 'identity cmd " "))
  3681. (setq err-string
  3682. (with-output-to-string
  3683. (setq exitcode
  3684. (apply 'call-process (car cmd)
  3685. nil standard-output nil (cdr cmd)))))
  3686. (or (zerop exitcode)
  3687. (error (concat "Unable to create OpenDocument file."
  3688. (format " Zip failed with error (%s)"
  3689. err-string)))))
  3690. cmds)))
  3691. ;; Move the zip file from temporary work directory to
  3692. ;; user-mandated location.
  3693. (rename-file (concat org-odt-zip-dir target-name) target)
  3694. (message "Created %s" (expand-file-name target))
  3695. ;; Cleanup work directory and work files.
  3696. (funcall --cleanup-xml-buffers)
  3697. ;; Open the OpenDocument file in archive-mode for
  3698. ;; examination.
  3699. (find-file-noselect target t)
  3700. ;; Return exported file.
  3701. (cond
  3702. ;; Case 1: Conversion desired on exported file. Run the
  3703. ;; converter on the OpenDocument file. Return the
  3704. ;; converted file.
  3705. (org-odt-preferred-output-format
  3706. (or (org-odt-convert target org-odt-preferred-output-format)
  3707. target))
  3708. ;; Case 2: No further conversion. Return exported
  3709. ;; OpenDocument file.
  3710. (t target))))
  3711. (error
  3712. ;; Cleanup work directory and work files.
  3713. (funcall --cleanup-xml-buffers)
  3714. (message "OpenDocument export failed: %s"
  3715. (error-message-string err))))))
  3716. ;;;; Export to OpenDocument formula
  3717. ;;;###autoload
  3718. (defun org-odt-export-as-odf (latex-frag &optional odf-file)
  3719. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3720. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3721. MathML. When invoked as an interactive command, use
  3722. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3723. region. If no LaTeX fragments are found, prompt for it. Push
  3724. MathML source to kill ring depending on the value of
  3725. `org-export-copy-to-kill-ring'."
  3726. (interactive
  3727. `(,(let (frag)
  3728. (setq frag (and (setq frag (and (region-active-p)
  3729. (buffer-substring (region-beginning)
  3730. (region-end))))
  3731. (loop for e in org-latex-regexps
  3732. thereis (when (string-match (nth 1 e) frag)
  3733. (match-string (nth 2 e) frag)))))
  3734. (read-string "LaTeX Fragment: " frag nil frag))
  3735. ,(let ((odf-filename (expand-file-name
  3736. (concat
  3737. (file-name-sans-extension
  3738. (or (file-name-nondirectory buffer-file-name)))
  3739. "." "odf")
  3740. (file-name-directory buffer-file-name))))
  3741. (read-file-name "ODF filename: " nil odf-filename nil
  3742. (file-name-nondirectory odf-filename)))))
  3743. (let ((filename (or odf-file
  3744. (expand-file-name
  3745. (concat
  3746. (file-name-sans-extension
  3747. (or (file-name-nondirectory buffer-file-name)))
  3748. "." "odf")
  3749. (file-name-directory buffer-file-name)))))
  3750. (org-odt--export-wrap
  3751. filename
  3752. (let* ((buffer (progn
  3753. (require 'nxml-mode)
  3754. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3755. (find-file-noselect (concat org-odt-zip-dir
  3756. "content.xml") t))))
  3757. (coding-system-for-write 'utf-8)
  3758. (save-buffer-coding-system 'utf-8))
  3759. (set-buffer buffer)
  3760. (set-buffer-file-coding-system coding-system-for-write)
  3761. (let ((mathml (org-create-math-formula latex-frag)))
  3762. (unless mathml (error "No Math formula created"))
  3763. (insert mathml)
  3764. ;; Add MathML to kill ring, if needed.
  3765. (when (org-export--copy-to-kill-ring-p)
  3766. (org-kill-new (buffer-string))))))))
  3767. ;;;###autoload
  3768. (defun org-odt-export-as-odf-and-open ()
  3769. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3770. Use `org-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3771. formula file."
  3772. (interactive)
  3773. (org-open-file (call-interactively 'org-odt-export-as-odf) 'system))
  3774. ;;;; Export to OpenDocument Text
  3775. ;;;###autoload
  3776. (defun org-odt-export-to-odt (&optional async subtreep visible-only ext-plist)
  3777. "Export current buffer to a ODT file.
  3778. If narrowing is active in the current buffer, only export its
  3779. narrowed part.
  3780. If a region is active, export that region.
  3781. A non-nil optional argument ASYNC means the process should happen
  3782. asynchronously. The resulting file should be accessible through
  3783. the `org-export-stack' interface.
  3784. When optional argument SUBTREEP is non-nil, export the sub-tree
  3785. at point, extracting information from the headline properties
  3786. first.
  3787. When optional argument VISIBLE-ONLY is non-nil, don't export
  3788. contents of hidden elements.
  3789. EXT-PLIST, when provided, is a property list with external
  3790. parameters overriding Org default settings, but still inferior to
  3791. file-local settings.
  3792. Return output file's name."
  3793. (interactive)
  3794. (let ((outfile (org-export-output-file-name ".odt" subtreep)))
  3795. (if async
  3796. (org-export-async-start (lambda (f) (org-export-add-to-stack f 'odt))
  3797. `(expand-file-name
  3798. (org-odt--export-wrap
  3799. ,outfile
  3800. (let* ((org-odt-embedded-images-count 0)
  3801. (org-odt-embedded-formulas-count 0)
  3802. (org-odt-automatic-styles nil)
  3803. (org-odt-object-counters nil)
  3804. ;; Let `htmlfontify' know that we are interested in
  3805. ;; collecting styles.
  3806. (hfy-user-sheet-assoc nil))
  3807. ;; Initialize content.xml and kick-off the export
  3808. ;; process.
  3809. (let ((out-buf
  3810. (progn
  3811. (require 'nxml-mode)
  3812. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3813. (find-file-noselect
  3814. (concat org-odt-zip-dir "content.xml") t))))
  3815. (output (org-export-as
  3816. 'odt ,subtreep ,visible-only nil ,ext-plist)))
  3817. (with-current-buffer out-buf
  3818. (erase-buffer)
  3819. (insert output)))))))
  3820. (org-odt--export-wrap
  3821. outfile
  3822. (let* ((org-odt-embedded-images-count 0)
  3823. (org-odt-embedded-formulas-count 0)
  3824. (org-odt-automatic-styles nil)
  3825. (org-odt-object-counters nil)
  3826. ;; Let `htmlfontify' know that we are interested in collecting
  3827. ;; styles.
  3828. (hfy-user-sheet-assoc nil))
  3829. ;; Initialize content.xml and kick-off the export process.
  3830. (let ((output (org-export-as 'odt subtreep visible-only nil ext-plist))
  3831. (out-buf (progn
  3832. (require 'nxml-mode)
  3833. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3834. (find-file-noselect
  3835. (concat org-odt-zip-dir "content.xml") t)))))
  3836. (with-current-buffer out-buf (erase-buffer) (insert output))))))))
  3837. ;;;; Convert between OpenDocument and other formats
  3838. (defun org-odt-reachable-p (in-fmt out-fmt)
  3839. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3840. (catch 'done
  3841. (let ((reachable-formats (org-odt-do-reachable-formats in-fmt)))
  3842. (dolist (e reachable-formats)
  3843. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3844. (when out-fmt-spec
  3845. (throw 'done (cons (car e) out-fmt-spec))))))))
  3846. (defun org-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3847. "Workhorse routine for `org-odt-convert'."
  3848. (require 'browse-url)
  3849. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3850. (dummy (or (file-readable-p in-file)
  3851. (error "Cannot read %s" in-file)))
  3852. (in-fmt (file-name-extension in-file))
  3853. (out-fmt (or out-fmt (error "Output format unspecified")))
  3854. (how (or (org-odt-reachable-p in-fmt out-fmt)
  3855. (error "Cannot convert from %s format to %s format?"
  3856. in-fmt out-fmt)))
  3857. (convert-process (car how))
  3858. (out-file (concat (file-name-sans-extension in-file) "."
  3859. (nth 1 (or (cdr how) out-fmt))))
  3860. (extra-options (or (nth 2 (cdr how)) ""))
  3861. (out-dir (file-name-directory in-file))
  3862. (cmd (format-spec convert-process
  3863. `((?i . ,(shell-quote-argument in-file))
  3864. (?I . ,(browse-url-file-url in-file))
  3865. (?f . ,out-fmt)
  3866. (?o . ,out-file)
  3867. (?O . ,(browse-url-file-url out-file))
  3868. (?d . , (shell-quote-argument out-dir))
  3869. (?D . ,(browse-url-file-url out-dir))
  3870. (?x . ,extra-options)))))
  3871. (when (file-exists-p out-file)
  3872. (delete-file out-file))
  3873. (message "Executing %s" cmd)
  3874. (let ((cmd-output (shell-command-to-string cmd)))
  3875. (message "%s" cmd-output))
  3876. (cond
  3877. ((file-exists-p out-file)
  3878. (message "Exported to %s" out-file)
  3879. (when prefix-arg
  3880. (message "Opening %s..." out-file)
  3881. (org-open-file out-file 'system))
  3882. out-file)
  3883. (t
  3884. (message "Export to %s failed" out-file)
  3885. nil))))
  3886. (defun org-odt-do-reachable-formats (in-fmt)
  3887. "Return verbose info about formats to which IN-FMT can be converted.
  3888. Return a list where each element is of the
  3889. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3890. `org-odt-convert-processes' for CONVERTER-PROCESS and see
  3891. `org-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3892. (let* ((converter
  3893. (and org-odt-convert-process
  3894. (cadr (assoc-string org-odt-convert-process
  3895. org-odt-convert-processes t))))
  3896. (capabilities
  3897. (and org-odt-convert-process
  3898. (cadr (assoc-string org-odt-convert-process
  3899. org-odt-convert-processes t))
  3900. org-odt-convert-capabilities))
  3901. reachable-formats)
  3902. (when converter
  3903. (dolist (c capabilities)
  3904. (when (member in-fmt (nth 1 c))
  3905. (push (cons converter (nth 2 c)) reachable-formats))))
  3906. reachable-formats))
  3907. (defun org-odt-reachable-formats (in-fmt)
  3908. "Return list of formats to which IN-FMT can be converted.
  3909. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3910. (let (l)
  3911. (mapc (lambda (e) (add-to-list 'l e))
  3912. (apply 'append (mapcar
  3913. (lambda (e) (mapcar 'car (cdr e)))
  3914. (org-odt-do-reachable-formats in-fmt))))
  3915. l))
  3916. (defun org-odt-convert-read-params ()
  3917. "Return IN-FILE and OUT-FMT params for `org-odt-do-convert'.
  3918. This is a helper routine for interactive use."
  3919. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3920. (in-file (read-file-name "File to be converted: "
  3921. nil buffer-file-name t))
  3922. (in-fmt (file-name-extension in-file))
  3923. (out-fmt-choices (org-odt-reachable-formats in-fmt))
  3924. (out-fmt
  3925. (or (and out-fmt-choices
  3926. (funcall input "Output format: "
  3927. out-fmt-choices nil nil nil))
  3928. (error
  3929. "No known converter or no known output formats for %s files"
  3930. in-fmt))))
  3931. (list in-file out-fmt)))
  3932. ;;;###autoload
  3933. (defun org-odt-convert (&optional in-file out-fmt prefix-arg)
  3934. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3935. IN-FILE is the file to be converted. If unspecified, it defaults
  3936. to variable `buffer-file-name'. OUT-FMT is the desired output
  3937. format. Use `org-odt-convert-process' as the converter.
  3938. If PREFIX-ARG is non-nil then the newly converted file is opened
  3939. using `org-open-file'."
  3940. (interactive
  3941. (append (org-odt-convert-read-params) current-prefix-arg))
  3942. (org-odt-do-convert in-file out-fmt prefix-arg))
  3943. ;;; Library Initializations
  3944. (mapc
  3945. (lambda (desc)
  3946. ;; Let Emacs open all OpenDocument files in archive mode
  3947. (add-to-list 'auto-mode-alist
  3948. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3949. org-odt-file-extensions)
  3950. (provide 'ox-odt)
  3951. ;; Local variables:
  3952. ;; generated-autoload-file: "org-loaddefs.el"
  3953. ;; End:
  3954. ;;; ox-odt.el ends here