ox-publish.el 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. ;;; ox-publish.el --- Publish Related Org Mode Files as a Website
  2. ;; Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. ;; Author: David O'Toole <dto@gnu.org>
  4. ;; Maintainer: Carsten Dominik <carsten DOT dominik AT gmail DOT com>
  5. ;; Keywords: hypermedia, outlines, wp
  6. ;; This file is part of GNU Emacs.
  7. ;;
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This program allow configurable publishing of related sets of
  20. ;; Org mode files as a complete website.
  21. ;;
  22. ;; ox-publish.el can do the following:
  23. ;;
  24. ;; + Publish all one's Org files to a given export back-end
  25. ;; + Upload HTML, images, attachments and other files to a web server
  26. ;; + Exclude selected private pages from publishing
  27. ;; + Publish a clickable sitemap of pages
  28. ;; + Manage local timestamps for publishing only changed files
  29. ;; + Accept plugin functions to extend range of publishable content
  30. ;;
  31. ;; Documentation for publishing is in the manual.
  32. ;;; Code:
  33. (eval-when-compile (require 'cl))
  34. (require 'format-spec)
  35. (require 'ox)
  36. ;;; Variables
  37. (defvar org-publish-temp-files nil
  38. "Temporary list of files to be published.")
  39. ;; Here, so you find the variable right before it's used the first time:
  40. (defvar org-publish-cache nil
  41. "This will cache timestamps and titles for files in publishing projects.
  42. Blocks could hash sha1 values here.")
  43. (defvar org-publish-after-publishing-hook nil
  44. "Hook run each time a file is published.
  45. Every function in this hook will be called with two arguments:
  46. the name of the original file and the name of the file
  47. produced.")
  48. (defgroup org-publish nil
  49. "Options for publishing a set of Org-mode and related files."
  50. :tag "Org Publishing"
  51. :group 'org)
  52. (defcustom org-publish-project-alist nil
  53. "Association list to control publishing behavior.
  54. Each element of the alist is a publishing project. The CAR of
  55. each element is a string, uniquely identifying the project. The
  56. CDR of each element is in one of the following forms:
  57. 1. A well-formed property list with an even number of elements,
  58. alternating keys and values, specifying parameters for the
  59. publishing process.
  60. (:property value :property value ... )
  61. 2. A meta-project definition, specifying of a list of
  62. sub-projects:
  63. (:components (\"project-1\" \"project-2\" ...))
  64. When the CDR of an element of org-publish-project-alist is in
  65. this second form, the elements of the list after `:components'
  66. are taken to be components of the project, which group together
  67. files requiring different publishing options. When you publish
  68. such a project with \\[org-publish], the components all publish.
  69. When a property is given a value in `org-publish-project-alist',
  70. its setting overrides the value of the corresponding user
  71. variable (if any) during publishing. However, options set within
  72. a file override everything.
  73. Most properties are optional, but some should always be set:
  74. `:base-directory'
  75. Directory containing publishing source files.
  76. `:base-extension'
  77. Extension (without the dot!) of source files. This can be
  78. a regular expression. If not given, \"org\" will be used as
  79. default extension.
  80. `:publishing-directory'
  81. Directory (possibly remote) where output files will be
  82. published.
  83. The `:exclude' property may be used to prevent certain files from
  84. being published. Its value may be a string or regexp matching
  85. file names you don't want to be published.
  86. The `:include' property may be used to include extra files. Its
  87. value may be a list of filenames to include. The filenames are
  88. considered relative to the base directory.
  89. When both `:include' and `:exclude' properties are given values,
  90. the exclusion step happens first.
  91. One special property controls which back-end function to use for
  92. publishing files in the project. This can be used to extend the
  93. set of file types publishable by `org-publish', as well as the
  94. set of output formats.
  95. `:publishing-function'
  96. Function to publish file. Each back-end may define its
  97. own (i.e. `org-latex-publish-to-pdf',
  98. `org-html-publish-to-html'). May be a list of functions, in
  99. which case each function in the list is invoked in turn.
  100. Another property allows you to insert code that prepares
  101. a project for publishing. For example, you could call GNU Make
  102. on a certain makefile, to ensure published files are built up to
  103. date.
  104. `:preparation-function'
  105. Function to be called before publishing this project. This
  106. may also be a list of functions.
  107. `:completion-function'
  108. Function to be called after publishing this project. This
  109. may also be a list of functions.
  110. Some properties control details of the Org publishing process,
  111. and are equivalent to the corresponding user variables listed in
  112. the right column. Back-end specific properties may also be
  113. included. See the back-end documentation for more information.
  114. :author `user-full-name'
  115. :creator `org-export-creator-string'
  116. :email `user-mail-address'
  117. :exclude-tags `org-export-exclude-tags'
  118. :headline-levels `org-export-headline-levels'
  119. :language `org-export-default-language'
  120. :preserve-breaks `org-export-preserve-breaks'
  121. :section-numbers `org-export-with-section-numbers'
  122. :select-tags `org-export-select-tags'
  123. :time-stamp-file `org-export-time-stamp-file'
  124. :with-archived-trees `org-export-with-archived-trees'
  125. :with-author `org-export-with-author'
  126. :with-creator `org-export-with-creator'
  127. :with-date `org-export-with-date'
  128. :with-drawers `org-export-with-drawers'
  129. :with-email `org-export-with-email'
  130. :with-emphasize `org-export-with-emphasize'
  131. :with-entities `org-export-with-entities'
  132. :with-fixed-width `org-export-with-fixed-width'
  133. :with-footnotes `org-export-with-footnotes'
  134. :with-inlinetasks `org-export-with-inlinetasks'
  135. :with-latex `org-export-with-latex'
  136. :with-planning `org-export-with-planning'
  137. :with-priority `org-export-with-priority'
  138. :with-properties `org-export-with-properties'
  139. :with-smart-quotes `org-export-with-smart-quotes'
  140. :with-special-strings `org-export-with-special-strings'
  141. :with-statistics-cookies' `org-export-with-statistics-cookies'
  142. :with-sub-superscript `org-export-with-sub-superscripts'
  143. :with-toc `org-export-with-toc'
  144. :with-tables `org-export-with-tables'
  145. :with-tags `org-export-with-tags'
  146. :with-tasks `org-export-with-tasks'
  147. :with-timestamps `org-export-with-timestamps'
  148. :with-title `org-export-with-title'
  149. :with-todo-keywords `org-export-with-todo-keywords'
  150. The following properties may be used to control publishing of
  151. a site-map of files or summary page for a given project.
  152. `:auto-sitemap'
  153. Whether to publish a site-map during
  154. `org-publish-current-project' or `org-publish-all'.
  155. `:sitemap-filename'
  156. Filename for output of sitemap. Defaults to \"sitemap.org\".
  157. `:sitemap-title'
  158. Title of site-map page. Defaults to name of file.
  159. `:sitemap-function'
  160. Plugin function to use for generation of site-map. Defaults
  161. to `org-publish-org-sitemap', which generates a plain list of
  162. links to all files in the project.
  163. `:sitemap-style'
  164. Can be `list' (site-map is just an itemized list of the
  165. titles of the files involved) or `tree' (the directory
  166. structure of the source files is reflected in the site-map).
  167. Defaults to `tree'.
  168. `:sitemap-sans-extension'
  169. Remove extension from site-map's file-names. Useful to have
  170. cool URIs (see http://www.w3.org/Provider/Style/URI).
  171. Defaults to nil.
  172. If you create a site-map file, adjust the sorting like this:
  173. `:sitemap-sort-folders'
  174. Where folders should appear in the site-map. Set this to
  175. `first' (default) or `last' to display folders first or last,
  176. respectively. Any other value will mix files and folders.
  177. `:sitemap-sort-files'
  178. The site map is normally sorted alphabetically. You can
  179. change this behavior setting this to `anti-chronologically',
  180. `chronologically', or nil.
  181. `:sitemap-ignore-case'
  182. Should sorting be case-sensitive? Default nil.
  183. The following property control the creation of a concept index.
  184. `:makeindex'
  185. Create a concept index. The file containing the index has to
  186. be called \"theindex.org\". If it doesn't exist in the
  187. project, it will be generated. Contents of the index are
  188. stored in the file \"theindex.inc\", which can be included in
  189. \"theindex.org\".
  190. Other properties affecting publication.
  191. `:body-only'
  192. Set this to t to publish only the body of the documents."
  193. :group 'org-export-publish
  194. :type 'alist)
  195. (defcustom org-publish-use-timestamps-flag t
  196. "Non-nil means use timestamp checking to publish only changed files.
  197. When nil, do no timestamp checking and always publish all files."
  198. :group 'org-export-publish
  199. :type 'boolean)
  200. (defcustom org-publish-timestamp-directory
  201. (convert-standard-filename "~/.org-timestamps/")
  202. "Name of directory in which to store publishing timestamps."
  203. :group 'org-export-publish
  204. :type 'directory)
  205. (defcustom org-publish-list-skipped-files t
  206. "Non-nil means show message about files *not* published."
  207. :group 'org-export-publish
  208. :type 'boolean)
  209. (defcustom org-publish-sitemap-sort-files 'alphabetically
  210. "Method to sort files in site-maps.
  211. Possible values are `alphabetically', `chronologically',
  212. `anti-chronologically' and nil.
  213. If `alphabetically', files will be sorted alphabetically. If
  214. `chronologically', files will be sorted with older modification
  215. time first. If `anti-chronologically', files will be sorted with
  216. newer modification time first. nil won't sort files.
  217. You can overwrite this default per project in your
  218. `org-publish-project-alist', using `:sitemap-sort-files'."
  219. :group 'org-export-publish
  220. :type 'symbol)
  221. (defcustom org-publish-sitemap-sort-folders 'first
  222. "A symbol, denoting if folders are sorted first in sitemaps.
  223. Possible values are `first', `last', and nil.
  224. If `first', folders will be sorted before files.
  225. If `last', folders are sorted to the end after the files.
  226. Any other value will not mix files and folders.
  227. You can overwrite this default per project in your
  228. `org-publish-project-alist', using `:sitemap-sort-folders'."
  229. :group 'org-export-publish
  230. :type 'symbol)
  231. (defcustom org-publish-sitemap-sort-ignore-case nil
  232. "Non-nil when site-map sorting should ignore case.
  233. You can overwrite this default per project in your
  234. `org-publish-project-alist', using `:sitemap-ignore-case'."
  235. :group 'org-export-publish
  236. :type 'boolean)
  237. (defcustom org-publish-sitemap-date-format "%Y-%m-%d"
  238. "Format for printing a date in the sitemap.
  239. See `format-time-string' for allowed formatters."
  240. :group 'org-export-publish
  241. :type 'string)
  242. (defcustom org-publish-sitemap-file-entry-format "%t"
  243. "Format string for site-map file entry.
  244. You could use brackets to delimit on what part the link will be.
  245. %t is the title.
  246. %a is the author.
  247. %d is the date formatted using `org-publish-sitemap-date-format'."
  248. :group 'org-export-publish
  249. :type 'string)
  250. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  251. ;;; Timestamp-related functions
  252. (defun org-publish-timestamp-filename (filename &optional pub-dir pub-func)
  253. "Return path to timestamp file for filename FILENAME."
  254. (setq filename (concat filename "::" (or pub-dir "") "::"
  255. (format "%s" (or pub-func ""))))
  256. (concat "X" (if (fboundp 'sha1) (sha1 filename) (md5 filename))))
  257. (defun org-publish-needed-p
  258. (filename &optional pub-dir pub-func true-pub-dir base-dir)
  259. "Non-nil if FILENAME should be published in PUB-DIR using PUB-FUNC.
  260. TRUE-PUB-DIR is where the file will truly end up. Currently we
  261. are not using this - maybe it can eventually be used to check if
  262. the file is present at the target location, and how old it is.
  263. Right now we cannot do this, because we do not know under what
  264. file name the file will be stored - the publishing function can
  265. still decide about that independently."
  266. (let ((rtn (if (not org-publish-use-timestamps-flag) t
  267. (org-publish-cache-file-needs-publishing
  268. filename pub-dir pub-func base-dir))))
  269. (if rtn (message "Publishing file %s using `%s'" filename pub-func)
  270. (when org-publish-list-skipped-files
  271. (message "Skipping unmodified file %s" filename)))
  272. rtn))
  273. (defun org-publish-update-timestamp
  274. (filename &optional pub-dir pub-func base-dir)
  275. "Update publishing timestamp for file FILENAME.
  276. If there is no timestamp, create one."
  277. (let ((key (org-publish-timestamp-filename filename pub-dir pub-func))
  278. (stamp (org-publish-cache-ctime-of-src filename)))
  279. (org-publish-cache-set key stamp)))
  280. (defun org-publish-remove-all-timestamps ()
  281. "Remove all files in the timestamp directory."
  282. (let ((dir org-publish-timestamp-directory)
  283. files)
  284. (when (and (file-exists-p dir) (file-directory-p dir))
  285. (mapc 'delete-file (directory-files dir 'full "[^.]\\'"))
  286. (org-publish-reset-cache))))
  287. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  288. ;;; Getting project information out of `org-publish-project-alist'
  289. (defun org-publish-expand-projects (projects-alist)
  290. "Expand projects in PROJECTS-ALIST.
  291. This splices all the components into the list."
  292. (let ((rest projects-alist) rtn p components)
  293. (while (setq p (pop rest))
  294. (if (setq components (plist-get (cdr p) :components))
  295. (setq rest (append
  296. (mapcar (lambda (x) (assoc x org-publish-project-alist))
  297. components)
  298. rest))
  299. (push p rtn)))
  300. (nreverse (delete-dups (delq nil rtn)))))
  301. (defvar org-publish-sitemap-sort-files)
  302. (defvar org-publish-sitemap-sort-folders)
  303. (defvar org-publish-sitemap-ignore-case)
  304. (defvar org-publish-sitemap-requested)
  305. (defvar org-publish-sitemap-date-format)
  306. (defvar org-publish-sitemap-file-entry-format)
  307. (defun org-publish-compare-directory-files (a b)
  308. "Predicate for `sort', that sorts folders and files for sitemap."
  309. (let ((retval t))
  310. (when (or org-publish-sitemap-sort-files org-publish-sitemap-sort-folders)
  311. ;; First we sort files:
  312. (when org-publish-sitemap-sort-files
  313. (case org-publish-sitemap-sort-files
  314. (alphabetically
  315. (let* ((adir (file-directory-p a))
  316. (aorg (and (string-match "\\.org$" a) (not adir)))
  317. (bdir (file-directory-p b))
  318. (borg (and (string-match "\\.org$" b) (not bdir)))
  319. (A (if aorg (concat (file-name-directory a)
  320. (org-publish-find-title a)) a))
  321. (B (if borg (concat (file-name-directory b)
  322. (org-publish-find-title b)) b)))
  323. (setq retval (if org-publish-sitemap-ignore-case
  324. (not (string-lessp (upcase B) (upcase A)))
  325. (not (string-lessp B A))))))
  326. ((anti-chronologically chronologically)
  327. (let* ((adate (org-publish-find-date a))
  328. (bdate (org-publish-find-date b))
  329. (A (+ (lsh (car adate) 16) (cadr adate)))
  330. (B (+ (lsh (car bdate) 16) (cadr bdate))))
  331. (setq retval
  332. (if (eq org-publish-sitemap-sort-files 'chronologically) (<= A B)
  333. (>= A B)))))))
  334. ;; Directory-wise wins:
  335. (when org-publish-sitemap-sort-folders
  336. ;; a is directory, b not:
  337. (cond
  338. ((and (file-directory-p a) (not (file-directory-p b)))
  339. (setq retval (equal org-publish-sitemap-sort-folders 'first)))
  340. ;; a is not a directory, but b is:
  341. ((and (not (file-directory-p a)) (file-directory-p b))
  342. (setq retval (equal org-publish-sitemap-sort-folders 'last))))))
  343. retval))
  344. (defun org-publish-get-base-files-1
  345. (base-dir &optional recurse match skip-file skip-dir)
  346. "Set `org-publish-temp-files' with files from BASE-DIR directory.
  347. If RECURSE is non-nil, check BASE-DIR recursively. If MATCH is
  348. non-nil, restrict this list to the files matching the regexp
  349. MATCH. If SKIP-FILE is non-nil, skip file matching the regexp
  350. SKIP-FILE. If SKIP-DIR is non-nil, don't check directories
  351. matching the regexp SKIP-DIR when recursing through BASE-DIR."
  352. (mapc (lambda (f)
  353. (let ((fd-p (file-directory-p f))
  354. (fnd (file-name-nondirectory f)))
  355. (if (and fd-p recurse
  356. (not (string-match "^\\.+$" fnd))
  357. (if skip-dir (not (string-match skip-dir fnd)) t))
  358. (org-publish-get-base-files-1
  359. f recurse match skip-file skip-dir)
  360. (unless (or fd-p ;; this is a directory
  361. (and skip-file (string-match skip-file fnd))
  362. (not (file-exists-p (file-truename f)))
  363. (not (string-match match fnd)))
  364. (pushnew f org-publish-temp-files)))))
  365. (let ((all-files (if (not recurse) (directory-files base-dir t match)
  366. ;; If RECURSE is non-nil, we want all files
  367. ;; matching MATCH and sub-directories.
  368. (org-remove-if-not
  369. (lambda (file)
  370. (or (file-directory-p file)
  371. (and match (string-match match file))))
  372. (directory-files base-dir t)))))
  373. (if (not org-publish-sitemap-requested) all-files
  374. (sort all-files 'org-publish-compare-directory-files)))))
  375. (defun org-publish-get-base-files (project &optional exclude-regexp)
  376. "Return a list of all files in PROJECT.
  377. If EXCLUDE-REGEXP is set, this will be used to filter out
  378. matching filenames."
  379. (let* ((project-plist (cdr project))
  380. (base-dir (file-name-as-directory
  381. (plist-get project-plist :base-directory)))
  382. (include-list (plist-get project-plist :include))
  383. (recurse (plist-get project-plist :recursive))
  384. (extension (or (plist-get project-plist :base-extension) "org"))
  385. ;; sitemap-... variables are dynamically scoped for
  386. ;; org-publish-compare-directory-files:
  387. (org-publish-sitemap-requested
  388. (plist-get project-plist :auto-sitemap))
  389. (sitemap-filename
  390. (or (plist-get project-plist :sitemap-filename) "sitemap.org"))
  391. (org-publish-sitemap-sort-folders
  392. (if (plist-member project-plist :sitemap-sort-folders)
  393. (plist-get project-plist :sitemap-sort-folders)
  394. org-publish-sitemap-sort-folders))
  395. (org-publish-sitemap-sort-files
  396. (cond ((plist-member project-plist :sitemap-sort-files)
  397. (plist-get project-plist :sitemap-sort-files))
  398. ;; For backward compatibility:
  399. ((plist-member project-plist :sitemap-alphabetically)
  400. (if (plist-get project-plist :sitemap-alphabetically)
  401. 'alphabetically nil))
  402. (t org-publish-sitemap-sort-files)))
  403. (org-publish-sitemap-ignore-case
  404. (if (plist-member project-plist :sitemap-ignore-case)
  405. (plist-get project-plist :sitemap-ignore-case)
  406. org-publish-sitemap-sort-ignore-case))
  407. (match (if (eq extension 'any) "^[^\\.]"
  408. (concat "^[^\\.].*\\.\\(" extension "\\)$"))))
  409. ;; Make sure `org-publish-sitemap-sort-folders' has an accepted
  410. ;; value.
  411. (unless (memq org-publish-sitemap-sort-folders '(first last))
  412. (setq org-publish-sitemap-sort-folders nil))
  413. (setq org-publish-temp-files nil)
  414. (if org-publish-sitemap-requested
  415. (pushnew (expand-file-name (concat base-dir sitemap-filename))
  416. org-publish-temp-files))
  417. (org-publish-get-base-files-1 base-dir recurse match
  418. ;; FIXME distinguish exclude regexp
  419. ;; for skip-file and skip-dir?
  420. exclude-regexp exclude-regexp)
  421. (mapc (lambda (f)
  422. (pushnew
  423. (expand-file-name (concat base-dir f))
  424. org-publish-temp-files))
  425. include-list)
  426. org-publish-temp-files))
  427. (defun org-publish-get-project-from-filename (filename &optional up)
  428. "Return the project that FILENAME belongs to."
  429. (let* ((filename (expand-file-name filename))
  430. project-name)
  431. (catch 'p-found
  432. (dolist (prj org-publish-project-alist)
  433. (unless (plist-get (cdr prj) :components)
  434. ;; [[info:org:Selecting%20files]] shows how this is supposed to work:
  435. (let* ((r (plist-get (cdr prj) :recursive))
  436. (b (expand-file-name (file-name-as-directory
  437. (plist-get (cdr prj) :base-directory))))
  438. (x (or (plist-get (cdr prj) :base-extension) "org"))
  439. (e (plist-get (cdr prj) :exclude))
  440. (i (plist-get (cdr prj) :include))
  441. (xm (concat "^" b (if r ".+" "[^/]+") "\\.\\(" x "\\)$")))
  442. (when
  443. (or (and i
  444. (member filename
  445. (mapcar (lambda (file)
  446. (expand-file-name file b))
  447. i)))
  448. (and (not (and e (string-match e filename)))
  449. (string-match xm filename)))
  450. (setq project-name (car prj))
  451. (throw 'p-found project-name))))))
  452. (when up
  453. (dolist (prj org-publish-project-alist)
  454. (if (member project-name (plist-get (cdr prj) :components))
  455. (setq project-name (car prj)))))
  456. (assoc project-name org-publish-project-alist)))
  457. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  458. ;;; Tools for publishing functions in back-ends
  459. (defun org-publish-org-to (backend filename extension plist &optional pub-dir)
  460. "Publish an Org file to a specified back-end.
  461. BACKEND is a symbol representing the back-end used for
  462. transcoding. FILENAME is the filename of the Org file to be
  463. published. EXTENSION is the extension used for the output
  464. string, with the leading dot. PLIST is the property list for the
  465. given project.
  466. Optional argument PUB-DIR, when non-nil is the publishing
  467. directory.
  468. Return output file name."
  469. (unless (or (not pub-dir) (file-exists-p pub-dir)) (make-directory pub-dir t))
  470. ;; Check if a buffer visiting FILENAME is already open.
  471. (let* ((org-inhibit-startup t)
  472. (visitingp (find-buffer-visiting filename))
  473. (work-buffer (or visitingp (find-file-noselect filename))))
  474. (prog1 (with-current-buffer work-buffer
  475. (let ((output-file
  476. (org-export-output-file-name extension nil pub-dir))
  477. (body-p (plist-get plist :body-only)))
  478. (org-export-to-file backend output-file
  479. nil nil nil body-p
  480. ;; Add `org-publish--collect-references' and
  481. ;; `org-publish-collect-index' to final output
  482. ;; filters. The latter isn't dependent on
  483. ;; `:makeindex', since we want to keep it up-to-date
  484. ;; in cache anyway.
  485. (org-combine-plists
  486. plist
  487. `(:filter-final-output
  488. ,(cons 'org-publish--collect-references
  489. (cons 'org-publish-collect-index
  490. (plist-get plist :filter-final-output))))))))
  491. ;; Remove opened buffer in the process.
  492. (unless visitingp (kill-buffer work-buffer)))))
  493. (defun org-publish-attachment (plist filename pub-dir)
  494. "Publish a file with no transformation of any kind.
  495. FILENAME is the filename of the Org file to be published. PLIST
  496. is the property list for the given project. PUB-DIR is the
  497. publishing directory.
  498. Return output file name."
  499. (unless (file-directory-p pub-dir)
  500. (make-directory pub-dir t))
  501. (let ((output (expand-file-name (file-name-nondirectory filename) pub-dir)))
  502. (or (equal (expand-file-name (file-name-directory filename))
  503. (file-name-as-directory (expand-file-name pub-dir)))
  504. (copy-file filename output t))
  505. ;; Return file name.
  506. output))
  507. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  508. ;;; Publishing files, sets of files, and indices
  509. (defun org-publish-file (filename &optional project no-cache)
  510. "Publish file FILENAME from PROJECT.
  511. If NO-CACHE is not nil, do not initialize org-publish-cache and
  512. write it to disk. This is needed, since this function is used to
  513. publish single files, when entire projects are published.
  514. See `org-publish-projects'."
  515. (let* ((project
  516. (or project
  517. (or (org-publish-get-project-from-filename filename)
  518. (error "File %s not part of any known project"
  519. (abbreviate-file-name filename)))))
  520. (project-plist (cdr project))
  521. (ftname (expand-file-name filename))
  522. (publishing-function
  523. (let ((fun (plist-get project-plist :publishing-function)))
  524. (cond ((null fun) (error "No publishing function chosen"))
  525. ((listp fun) fun)
  526. (t (list fun)))))
  527. (base-dir
  528. (file-name-as-directory
  529. (expand-file-name
  530. (or (plist-get project-plist :base-directory)
  531. (error "Project %s does not have :base-directory defined"
  532. (car project))))))
  533. (pub-dir
  534. (file-name-as-directory
  535. (file-truename
  536. (or (eval (plist-get project-plist :publishing-directory))
  537. (error "Project %s does not have :publishing-directory defined"
  538. (car project))))))
  539. tmp-pub-dir)
  540. (unless no-cache (org-publish-initialize-cache (car project)))
  541. (setq tmp-pub-dir
  542. (file-name-directory
  543. (concat pub-dir
  544. (and (string-match (regexp-quote base-dir) ftname)
  545. (substring ftname (match-end 0))))))
  546. ;; Allow chain of publishing functions.
  547. (dolist (f publishing-function)
  548. (when (org-publish-needed-p filename pub-dir f tmp-pub-dir base-dir)
  549. (let ((output (funcall f project-plist filename tmp-pub-dir)))
  550. (org-publish-update-timestamp filename pub-dir f base-dir)
  551. (run-hook-with-args 'org-publish-after-publishing-hook
  552. filename
  553. output))))
  554. (unless no-cache (org-publish-write-cache-file))))
  555. (defun org-publish-projects (projects)
  556. "Publish all files belonging to the PROJECTS alist.
  557. If `:auto-sitemap' is set, publish the sitemap too. If
  558. `:makeindex' is set, also produce a file theindex.org."
  559. (mapc
  560. (lambda (project)
  561. ;; Each project uses its own cache file:
  562. (org-publish-initialize-cache (car project))
  563. (let* ((project-plist (cdr project))
  564. (exclude-regexp (plist-get project-plist :exclude))
  565. (sitemap-p (plist-get project-plist :auto-sitemap))
  566. (sitemap-filename (or (plist-get project-plist :sitemap-filename)
  567. "sitemap.org"))
  568. (sitemap-function (or (plist-get project-plist :sitemap-function)
  569. 'org-publish-org-sitemap))
  570. (org-publish-sitemap-date-format
  571. (or (plist-get project-plist :sitemap-date-format)
  572. org-publish-sitemap-date-format))
  573. (org-publish-sitemap-file-entry-format
  574. (or (plist-get project-plist :sitemap-file-entry-format)
  575. org-publish-sitemap-file-entry-format))
  576. (preparation-function
  577. (plist-get project-plist :preparation-function))
  578. (completion-function (plist-get project-plist :completion-function))
  579. (files (org-publish-get-base-files project exclude-regexp))
  580. (theindex
  581. (expand-file-name "theindex.org"
  582. (plist-get project-plist :base-directory))))
  583. (when preparation-function (run-hooks 'preparation-function))
  584. (if sitemap-p (funcall sitemap-function project sitemap-filename))
  585. ;; Publish all files from PROJECT excepted "theindex.org". Its
  586. ;; publishing will be deferred until "theindex.inc" is
  587. ;; populated.
  588. (dolist (file files)
  589. (unless (equal file theindex)
  590. (org-publish-file file project t)))
  591. ;; Populate "theindex.inc", if needed, and publish
  592. ;; "theindex.org".
  593. (when (plist-get project-plist :makeindex)
  594. (org-publish-index-generate-theindex
  595. project (plist-get project-plist :base-directory))
  596. (org-publish-file theindex project t))
  597. (when completion-function (run-hooks 'completion-function))
  598. (org-publish-write-cache-file)))
  599. (org-publish-expand-projects projects)))
  600. (defun org-publish-org-sitemap (project &optional sitemap-filename)
  601. "Create a sitemap of pages in set defined by PROJECT.
  602. Optionally set the filename of the sitemap with SITEMAP-FILENAME.
  603. Default for SITEMAP-FILENAME is `sitemap.org'."
  604. (let* ((project-plist (cdr project))
  605. (dir (file-name-as-directory
  606. (plist-get project-plist :base-directory)))
  607. (localdir (file-name-directory dir))
  608. (indent-str (make-string 2 ?\ ))
  609. (exclude-regexp (plist-get project-plist :exclude))
  610. (files (nreverse
  611. (org-publish-get-base-files project exclude-regexp)))
  612. (sitemap-filename (concat dir (or sitemap-filename "sitemap.org")))
  613. (sitemap-title (or (plist-get project-plist :sitemap-title)
  614. (concat "Sitemap for project " (car project))))
  615. (sitemap-style (or (plist-get project-plist :sitemap-style)
  616. 'tree))
  617. (sitemap-sans-extension
  618. (plist-get project-plist :sitemap-sans-extension))
  619. (visiting (find-buffer-visiting sitemap-filename))
  620. (ifn (file-name-nondirectory sitemap-filename))
  621. file sitemap-buffer)
  622. (with-current-buffer
  623. (let ((org-inhibit-startup t))
  624. (setq sitemap-buffer
  625. (or visiting (find-file sitemap-filename))))
  626. (erase-buffer)
  627. (insert (concat "#+TITLE: " sitemap-title "\n\n"))
  628. (while (setq file (pop files))
  629. (let ((fn (file-name-nondirectory file))
  630. (link (file-relative-name file dir))
  631. (oldlocal localdir))
  632. (when sitemap-sans-extension
  633. (setq link (file-name-sans-extension link)))
  634. ;; sitemap shouldn't list itself
  635. (unless (equal (file-truename sitemap-filename)
  636. (file-truename file))
  637. (if (eq sitemap-style 'list)
  638. (message "Generating list-style sitemap for %s" sitemap-title)
  639. (message "Generating tree-style sitemap for %s" sitemap-title)
  640. (setq localdir (concat (file-name-as-directory dir)
  641. (file-name-directory link)))
  642. (unless (string= localdir oldlocal)
  643. (if (string= localdir dir)
  644. (setq indent-str (make-string 2 ?\ ))
  645. (let ((subdirs
  646. (split-string
  647. (directory-file-name
  648. (file-name-directory
  649. (file-relative-name localdir dir))) "/"))
  650. (subdir "")
  651. (old-subdirs (split-string
  652. (file-relative-name oldlocal dir) "/")))
  653. (setq indent-str (make-string 2 ?\ ))
  654. (while (string= (car old-subdirs) (car subdirs))
  655. (setq indent-str (concat indent-str (make-string 2 ?\ )))
  656. (pop old-subdirs)
  657. (pop subdirs))
  658. (dolist (d subdirs)
  659. (setq subdir (concat subdir d "/"))
  660. (insert (concat indent-str " + " d "\n"))
  661. (setq indent-str (make-string
  662. (+ (length indent-str) 2) ?\ )))))))
  663. ;; This is common to 'flat and 'tree
  664. (let ((entry
  665. (org-publish-format-file-entry
  666. org-publish-sitemap-file-entry-format file project-plist))
  667. (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
  668. (cond ((string-match-p regexp entry)
  669. (string-match regexp entry)
  670. (insert (concat indent-str " + " (match-string 1 entry)
  671. "[[file:" link "]["
  672. (match-string 2 entry)
  673. "]]" (match-string 3 entry) "\n")))
  674. (t
  675. (insert (concat indent-str " + [[file:" link "]["
  676. entry
  677. "]]\n"))))))))
  678. (save-buffer))
  679. (or visiting (kill-buffer sitemap-buffer))))
  680. (defun org-publish-format-file-entry (fmt file project-plist)
  681. (format-spec
  682. fmt
  683. `((?t . ,(org-publish-find-title file t))
  684. (?d . ,(format-time-string org-publish-sitemap-date-format
  685. (org-publish-find-date file)))
  686. (?a . ,(or (plist-get project-plist :author) user-full-name)))))
  687. (defun org-publish-find-title (file &optional reset)
  688. "Find the title of FILE in project."
  689. (or
  690. (and (not reset) (org-publish-cache-get-file-property file :title nil t))
  691. (let* ((org-inhibit-startup t)
  692. (visiting (find-buffer-visiting file))
  693. (buffer (or visiting (find-file-noselect file))))
  694. (with-current-buffer buffer
  695. (let ((title
  696. (let ((property
  697. (plist-get
  698. ;; protect local variables in open buffers
  699. (if visiting
  700. (org-export-with-buffer-copy (org-export-get-environment))
  701. (org-export-get-environment))
  702. :title)))
  703. (if property
  704. (org-no-properties (org-element-interpret-data property))
  705. (file-name-nondirectory (file-name-sans-extension file))))))
  706. (unless visiting (kill-buffer buffer))
  707. (org-publish-cache-set-file-property file :title title)
  708. title)))))
  709. (defun org-publish-find-date (file)
  710. "Find the date of FILE in project.
  711. This function assumes FILE is either a directory or an Org file.
  712. If FILE is an Org file and provides a DATE keyword use it. In
  713. any other case use the file system's modification time. Return
  714. time in `current-time' format."
  715. (if (file-directory-p file) (nth 5 (file-attributes file))
  716. (let* ((org-inhibit-startup t)
  717. (visiting (find-buffer-visiting file))
  718. (file-buf (or visiting (find-file-noselect file nil)))
  719. (date (plist-get
  720. (with-current-buffer file-buf
  721. (if visiting
  722. (org-export-with-buffer-copy
  723. (org-export-get-environment))
  724. (org-export-get-environment)))
  725. :date)))
  726. (unless visiting (kill-buffer file-buf))
  727. ;; DATE is a secondary string. If it contains a timestamp,
  728. ;; convert it to internal format. Otherwise, use FILE
  729. ;; modification time.
  730. (cond ((let ((ts (and (consp date) (assq 'timestamp date))))
  731. (and ts
  732. (let ((value (org-element-interpret-data ts)))
  733. (and (org-string-nw-p value)
  734. (org-time-string-to-time value))))))
  735. ((file-exists-p file) (nth 5 (file-attributes file)))
  736. (t (error "No such file: \"%s\"" file))))))
  737. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  738. ;;; Interactive publishing functions
  739. ;;;###autoload
  740. (defalias 'org-publish-project 'org-publish)
  741. ;;;###autoload
  742. (defun org-publish (project &optional force async)
  743. "Publish PROJECT.
  744. PROJECT is either a project name, as a string, or a project
  745. alist (see `org-publish-project-alist' variable).
  746. When optional argument FORCE is non-nil, force publishing all
  747. files in PROJECT. With a non-nil optional argument ASYNC,
  748. publishing will be done asynchronously, in another process."
  749. (interactive
  750. (list (assoc (org-icompleting-read "Publish project: "
  751. org-publish-project-alist nil t)
  752. org-publish-project-alist)
  753. current-prefix-arg))
  754. (let ((project (if (not (stringp project)) project
  755. ;; If this function is called in batch mode,
  756. ;; PROJECT is still a string here.
  757. (assoc project org-publish-project-alist))))
  758. (cond
  759. ((not project))
  760. (async
  761. (org-export-async-start (lambda (results) nil)
  762. `(let ((org-publish-use-timestamps-flag
  763. ,(and (not force) org-publish-use-timestamps-flag)))
  764. ;; Expand components right now as external process may not
  765. ;; be aware of complete `org-publish-project-alist'.
  766. (org-publish-projects
  767. ',(org-publish-expand-projects (list project))))))
  768. (t (save-window-excursion
  769. (let ((org-publish-use-timestamps-flag
  770. (and (not force) org-publish-use-timestamps-flag)))
  771. (org-publish-projects (list project))))))))
  772. ;;;###autoload
  773. (defun org-publish-all (&optional force async)
  774. "Publish all projects.
  775. With prefix argument FORCE, remove all files in the timestamp
  776. directory and force publishing all projects. With a non-nil
  777. optional argument ASYNC, publishing will be done asynchronously,
  778. in another process."
  779. (interactive "P")
  780. (if async
  781. (org-export-async-start (lambda (results) nil)
  782. `(progn
  783. (when ',force (org-publish-remove-all-timestamps))
  784. (let ((org-publish-use-timestamps-flag
  785. (if ',force nil ,org-publish-use-timestamps-flag)))
  786. (org-publish-projects ',org-publish-project-alist))))
  787. (when force (org-publish-remove-all-timestamps))
  788. (save-window-excursion
  789. (let ((org-publish-use-timestamps-flag
  790. (if force nil org-publish-use-timestamps-flag)))
  791. (org-publish-projects org-publish-project-alist)))))
  792. ;;;###autoload
  793. (defun org-publish-current-file (&optional force async)
  794. "Publish the current file.
  795. With prefix argument FORCE, force publish the file. When
  796. optional argument ASYNC is non-nil, publishing will be done
  797. asynchronously, in another process."
  798. (interactive "P")
  799. (let ((file (buffer-file-name (buffer-base-buffer))))
  800. (if async
  801. (org-export-async-start (lambda (results) nil)
  802. `(let ((org-publish-use-timestamps-flag
  803. (if ',force nil ,org-publish-use-timestamps-flag)))
  804. (org-publish-file ,file)))
  805. (save-window-excursion
  806. (let ((org-publish-use-timestamps-flag
  807. (if force nil org-publish-use-timestamps-flag)))
  808. (org-publish-file file))))))
  809. ;;;###autoload
  810. (defun org-publish-current-project (&optional force async)
  811. "Publish the project associated with the current file.
  812. With a prefix argument, force publishing of all files in
  813. the project."
  814. (interactive "P")
  815. (save-window-excursion
  816. (let ((project (org-publish-get-project-from-filename
  817. (buffer-file-name (buffer-base-buffer)) 'up)))
  818. (if project (org-publish project force async)
  819. (error "File %s is not part of any known project"
  820. (buffer-file-name (buffer-base-buffer)))))))
  821. ;;; Index generation
  822. (defun org-publish-collect-index (output backend info)
  823. "Update index for a file in cache.
  824. OUTPUT is the output from transcoding current file. BACKEND is
  825. the back-end that was used for transcoding. INFO is a plist
  826. containing publishing and export options.
  827. The index relative to current file is stored as an alist. An
  828. association has the following shape: (TERM FILE-NAME PARENT),
  829. where TERM is the indexed term, as a string, FILE-NAME is the
  830. original full path of the file where the term in encountered, and
  831. PARENT is a reference to the headline, if any, containing the
  832. original index keyword. When non-nil, this reference is a cons
  833. cell. Its CAR is a symbol among `id', `custom-id' and `name' and
  834. its CDR is a string."
  835. (let ((file (plist-get info :input-file)))
  836. (org-publish-cache-set-file-property
  837. file :index
  838. (delete-dups
  839. (org-element-map (plist-get info :parse-tree) 'keyword
  840. (lambda (k)
  841. (when (equal (org-element-property :key k) "INDEX")
  842. (let ((parent (org-export-get-parent-headline k)))
  843. (list (org-element-property :value k)
  844. file
  845. (cond
  846. ((not parent) nil)
  847. ((let ((id (org-element-property :ID parent)))
  848. (and id (cons 'id id))))
  849. ((let ((id (org-element-property :CUSTOM_ID parent)))
  850. (and id (cons 'custom-id id))))
  851. (t (cons 'name
  852. ;; Remove statistics cookie.
  853. (replace-regexp-in-string
  854. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  855. (org-element-property :raw-value parent)))))))))
  856. info))))
  857. ;; Return output unchanged.
  858. output)
  859. (defun org-publish-index-generate-theindex (project directory)
  860. "Retrieve full index from cache and build \"theindex.org\".
  861. PROJECT is the project the index relates to. DIRECTORY is the
  862. publishing directory."
  863. (let ((all-files (org-publish-get-base-files
  864. project (plist-get (cdr project) :exclude)))
  865. full-index)
  866. ;; Compile full index and sort it alphabetically.
  867. (dolist (file all-files
  868. (setq full-index
  869. (sort (nreverse full-index)
  870. (lambda (a b) (string< (downcase (car a))
  871. (downcase (car b)))))))
  872. (let ((index (org-publish-cache-get-file-property file :index)))
  873. (dolist (term index)
  874. (unless (member term full-index) (push term full-index)))))
  875. ;; Write "theindex.inc" in DIRECTORY.
  876. (with-temp-file (expand-file-name "theindex.inc" directory)
  877. (let ((current-letter nil) (last-entry nil))
  878. (dolist (idx full-index)
  879. (let* ((entry (org-split-string (car idx) "!"))
  880. (letter (upcase (substring (car entry) 0 1)))
  881. ;; Transform file into a path relative to publishing
  882. ;; directory.
  883. (file (file-relative-name
  884. (nth 1 idx)
  885. (plist-get (cdr project) :base-directory))))
  886. ;; Check if another letter has to be inserted.
  887. (unless (string= letter current-letter)
  888. (insert (format "* %s\n" letter)))
  889. ;; Compute the first difference between last entry and
  890. ;; current one: it tells the level at which new items
  891. ;; should be added.
  892. (let* ((rank (if (equal entry last-entry) (1- (length entry))
  893. (loop for n from 0 to (length entry)
  894. unless (equal (nth n entry) (nth n last-entry))
  895. return n)))
  896. (len (length (nthcdr rank entry))))
  897. ;; For each term after the first difference, create
  898. ;; a new sub-list with the term as body. Moreover,
  899. ;; linkify the last term.
  900. (dotimes (n len)
  901. (insert
  902. (concat
  903. (make-string (* (+ rank n) 2) ? ) " - "
  904. (if (not (= (1- len) n)) (nth (+ rank n) entry)
  905. ;; Last term: Link it to TARGET, if possible.
  906. (let ((target (nth 2 idx)))
  907. (format
  908. "[[%s][%s]]"
  909. ;; Destination.
  910. (case (car target)
  911. ('nil (format "file:%s" file))
  912. (id (format "id:%s" (cdr target)))
  913. (custom-id (format "file:%s::#%s" file (cdr target)))
  914. (otherwise (format "file:%s::*%s" file (cdr target))))
  915. ;; Description.
  916. (car (last entry)))))
  917. "\n"))))
  918. (setq current-letter letter last-entry entry))))
  919. ;; Create "theindex.org", if it doesn't exist yet, and provide
  920. ;; a default index file.
  921. (let ((index.org (expand-file-name "theindex.org" directory)))
  922. (unless (file-exists-p index.org)
  923. (with-temp-file index.org
  924. (insert "#+TITLE: Index\n\n#+INCLUDE: \"theindex.inc\"\n\n")))))))
  925. ;;; External Fuzzy Links Resolution
  926. ;;
  927. ;; This part implements tools to resolve [[file.org::*Some headline]]
  928. ;; links, where "file.org" belongs to the current project.
  929. (defun org-publish--collect-references (output backend info)
  930. "Store headlines references for current published file.
  931. OUPUT is the produced output, as a string. BACKEND is the export
  932. back-end used, as a symbol. INFO is the final export state, as
  933. a plist.
  934. References are stored as an alist ((TYPE SEARCH) . VALUE) where
  935. TYPE is a symbol among `headline', `custom-id', `target' and
  936. `other'.
  937. SEARCH is the string a link is expected to match. It is
  938. - headline's title, as a string, with all whitespace
  939. characters and statistics cookies removed, if TYPE is
  940. `headline'.
  941. - CUSTOM_ID value if TYPE is `custom-id'.
  942. - target's or radio-target's name if TYPE is `target'.
  943. - NAME affiliated keyword is TYPE is `other'.
  944. VALUE is an internal reference used in the document, as
  945. a string.
  946. This function is meant to be used as a final out filter. See
  947. `org-publish-org-to'."
  948. (org-publish-cache-set-file-property
  949. (plist-get info :input-file) :references
  950. (let (refs)
  951. (when (hash-table-p (plist-get info :internal-references))
  952. (maphash
  953. (lambda (k v)
  954. (case (org-element-type k)
  955. ((headline inlinetask)
  956. (push (cons
  957. (cons 'headline
  958. (org-split-string
  959. (replace-regexp-in-string
  960. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  961. (org-element-property :raw-value k))))
  962. v)
  963. refs)
  964. (let ((custom-id (org-element-property :CUSTOM_ID k)))
  965. (when custom-id
  966. (push (cons (cons 'custom-id custom-id) v) refs))))
  967. ((radio-target target)
  968. (push
  969. (cons (cons 'target
  970. (org-split-string (org-element-property :value k)))
  971. v)
  972. refs))
  973. ((org-element-property :name k)
  974. (push
  975. (cons
  976. (cons 'other (org-split-string (org-element-property :name k)))
  977. v)
  978. refs)))
  979. refs)
  980. (plist-get info :internal-references)))
  981. refs))
  982. ;; Return output unchanged.
  983. output)
  984. (defun org-publish-resolve-external-link (search file)
  985. "Return reference for elements or objects matching SEARCH in FILE.
  986. Return value is an internal reference, as a string.
  987. This function allows to resolve external links like:
  988. [[file.org::*fuzzy][description]]
  989. [[file.org::#custom-id][description]]
  990. [[file.org::fuzzy][description]]"
  991. (if (not org-publish-cache)
  992. (progn
  993. (message "Reference \"%s\" in file \"%s\" cannot be resolved without \
  994. publishing"
  995. search
  996. file)
  997. "MissingReference")
  998. (let ((references (org-publish-cache-get-file-property
  999. (expand-file-name file) :references nil t)))
  1000. (cond
  1001. ((cdr (case (aref search 0)
  1002. (?* (assoc (cons 'headline (org-split-string (substring search 1)))
  1003. references))
  1004. (?# (assoc (cons 'custom-id (substring search 1)) references))
  1005. (t
  1006. (let ((s (org-split-string search)))
  1007. (or (assoc (cons 'target s) references)
  1008. (assoc (cons 'other s) references)
  1009. (assoc (cons 'headline s) references)))))))
  1010. (t (message "Unknown cross-reference \"%s\" in file \"%s\"" search file)
  1011. "MissingReference")))))
  1012. ;;; Caching functions
  1013. (defun org-publish-write-cache-file (&optional free-cache)
  1014. "Write `org-publish-cache' to file.
  1015. If FREE-CACHE, empty the cache."
  1016. (unless org-publish-cache
  1017. (error "`org-publish-write-cache-file' called, but no cache present"))
  1018. (let ((cache-file (org-publish-cache-get ":cache-file:")))
  1019. (unless cache-file
  1020. (error "Cannot find cache-file name in `org-publish-write-cache-file'"))
  1021. (with-temp-file cache-file
  1022. (let (print-level print-length)
  1023. (insert "(setq org-publish-cache (make-hash-table :test 'equal :weakness nil :size 100))\n")
  1024. (maphash (lambda (k v)
  1025. (insert
  1026. (format (concat "(puthash %S "
  1027. (if (or (listp v) (symbolp v))
  1028. "'" "")
  1029. "%S org-publish-cache)\n") k v)))
  1030. org-publish-cache)))
  1031. (when free-cache (org-publish-reset-cache))))
  1032. (defun org-publish-initialize-cache (project-name)
  1033. "Initialize the projects cache if not initialized yet and return it."
  1034. (unless project-name
  1035. (error "Cannot initialize `org-publish-cache' without projects name in `org-publish-initialize-cache'"))
  1036. (unless (file-exists-p org-publish-timestamp-directory)
  1037. (make-directory org-publish-timestamp-directory t))
  1038. (unless (file-directory-p org-publish-timestamp-directory)
  1039. (error "Org publish timestamp: %s is not a directory"
  1040. org-publish-timestamp-directory))
  1041. (unless (and org-publish-cache
  1042. (string= (org-publish-cache-get ":project:") project-name))
  1043. (let* ((cache-file
  1044. (concat
  1045. (expand-file-name org-publish-timestamp-directory)
  1046. project-name ".cache"))
  1047. (cexists (file-exists-p cache-file)))
  1048. (when org-publish-cache (org-publish-reset-cache))
  1049. (if cexists (load-file cache-file)
  1050. (setq org-publish-cache
  1051. (make-hash-table :test 'equal :weakness nil :size 100))
  1052. (org-publish-cache-set ":project:" project-name)
  1053. (org-publish-cache-set ":cache-file:" cache-file))
  1054. (unless cexists (org-publish-write-cache-file nil))))
  1055. org-publish-cache)
  1056. (defun org-publish-reset-cache ()
  1057. "Empty org-publish-cache and reset it nil."
  1058. (message "%s" "Resetting org-publish-cache")
  1059. (when (hash-table-p org-publish-cache)
  1060. (clrhash org-publish-cache))
  1061. (setq org-publish-cache nil))
  1062. (defun org-publish-cache-file-needs-publishing
  1063. (filename &optional pub-dir pub-func base-dir)
  1064. "Check the timestamp of the last publishing of FILENAME.
  1065. Return non-nil if the file needs publishing. Also check if
  1066. any included files have been more recently published, so that
  1067. the file including them will be republished as well."
  1068. (unless org-publish-cache
  1069. (error
  1070. "`org-publish-cache-file-needs-publishing' called, but no cache present"))
  1071. (let* ((case-fold-search t)
  1072. (key (org-publish-timestamp-filename filename pub-dir pub-func))
  1073. (pstamp (org-publish-cache-get key))
  1074. (org-inhibit-startup t)
  1075. (visiting (find-buffer-visiting filename))
  1076. included-files-ctime buf)
  1077. (when (equal (file-name-extension filename) "org")
  1078. (setq buf (find-file (expand-file-name filename)))
  1079. (with-current-buffer buf
  1080. (goto-char (point-min))
  1081. (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
  1082. (let* ((element (org-element-at-point))
  1083. (included-file
  1084. (and (eq (org-element-type element) 'keyword)
  1085. (let ((value (org-element-property :value element)))
  1086. (and value
  1087. (string-match "^\\(\".+?\"\\|\\S-+\\)" value)
  1088. ;; Ignore search suffix.
  1089. (car (split-string
  1090. (org-remove-double-quotes
  1091. (match-string 1 value)))))))))
  1092. (when included-file
  1093. (add-to-list 'included-files-ctime
  1094. (org-publish-cache-ctime-of-src
  1095. (expand-file-name included-file))
  1096. t)))))
  1097. (unless visiting (kill-buffer buf)))
  1098. (if (null pstamp) t
  1099. (let ((ctime (org-publish-cache-ctime-of-src filename)))
  1100. (or (< pstamp ctime)
  1101. (when included-files-ctime
  1102. (not (null (delq nil (mapcar (lambda (ct) (< ctime ct))
  1103. included-files-ctime))))))))))
  1104. (defun org-publish-cache-set-file-property
  1105. (filename property value &optional project-name)
  1106. "Set the VALUE for a PROPERTY of file FILENAME in publishing cache to VALUE.
  1107. Use cache file of PROJECT-NAME. If the entry does not exist, it
  1108. will be created. Return VALUE."
  1109. ;; Evtl. load the requested cache file:
  1110. (if project-name (org-publish-initialize-cache project-name))
  1111. (let ((pl (org-publish-cache-get filename)))
  1112. (if pl (progn (plist-put pl property value) value)
  1113. (org-publish-cache-get-file-property
  1114. filename property value nil project-name))))
  1115. (defun org-publish-cache-get-file-property
  1116. (filename property &optional default no-create project-name)
  1117. "Return the value for a PROPERTY of file FILENAME in publishing cache.
  1118. Use cache file of PROJECT-NAME. Return the value of that PROPERTY
  1119. or DEFAULT, if the value does not yet exist. If the entry will
  1120. be created, unless NO-CREATE is not nil."
  1121. ;; Evtl. load the requested cache file:
  1122. (if project-name (org-publish-initialize-cache project-name))
  1123. (let ((pl (org-publish-cache-get filename)) retval)
  1124. (if pl
  1125. (if (plist-member pl property)
  1126. (setq retval (plist-get pl property))
  1127. (setq retval default))
  1128. ;; no pl yet:
  1129. (unless no-create
  1130. (org-publish-cache-set filename (list property default)))
  1131. (setq retval default))
  1132. retval))
  1133. (defun org-publish-cache-get (key)
  1134. "Return the value stored in `org-publish-cache' for key KEY.
  1135. Returns nil, if no value or nil is found, or the cache does not
  1136. exist."
  1137. (unless org-publish-cache
  1138. (error "`org-publish-cache-get' called, but no cache present"))
  1139. (gethash key org-publish-cache))
  1140. (defun org-publish-cache-set (key value)
  1141. "Store KEY VALUE pair in `org-publish-cache'.
  1142. Returns value on success, else nil."
  1143. (unless org-publish-cache
  1144. (error "`org-publish-cache-set' called, but no cache present"))
  1145. (puthash key value org-publish-cache))
  1146. (defun org-publish-cache-ctime-of-src (file)
  1147. "Get the ctime of FILE as an integer."
  1148. (let ((attr (file-attributes
  1149. (expand-file-name (or (file-symlink-p file) file)
  1150. (file-name-directory file)))))
  1151. (if (not attr) (error "No such file: \"%s\"" file)
  1152. (+ (lsh (car (nth 5 attr)) 16)
  1153. (cadr (nth 5 attr))))))
  1154. (provide 'ox-publish)
  1155. ;; Local variables:
  1156. ;; generated-autoload-file: "org-loaddefs.el"
  1157. ;; End:
  1158. ;;; ox-publish.el ends here