org-e-odt.el 154 KB

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