org-e-odt.el 157 KB

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