org-e-odt.el 146 KB

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