org-e-odt.el 161 KB

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