org-e-odt.el 158 KB

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