emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Use headings in sitemap
@ 2016-09-29  4:09 Thibault Marin
  2016-10-11 16:34 ` Nicolas Goaziou
  0 siblings, 1 reply; 12+ messages in thread
From: Thibault Marin @ 2016-09-29  4:09 UTC (permalink / raw)
  To: org mode

[-- Attachment #1: Type: text/plain, Size: 1307 bytes --]


Hi list,

I would like to generate a sitemap for a published website and use it to extract
the last few entries in a specific folder to put on the main page.

The site structure looks like:
.
├── index.org
├── posts
│   ├── A.org
│   ├── B.org
│   └── C.org
├── misc
│   ├── page.org
│   └── other-page.org
└── sitemap.org

In index.org, I would have:
#+begin_src org
#+INCLUDE: sitemap.org::*posts :lines "-10" :only-contents t
#+end_src
to include links to the 10 most recent pages in =posts= (I use
:sitemap-sort-files anti-chronologically in the project setup).  If I am not
missing anything, this requires the sitemap.org file to have a =posts= heading,
but the `org-publish-org-sitemap' function only produces a list of pages.

If there is no better way to get this to work, I would like to propose a patch
to `org-publish-org-sitemap' to produce headings in the sitemap file when a new
parameter is passed and non-nil.  The attached patch is my first attempt at it,
it works for my tests.

I would be interested to hear people's opinion on this:
- Is there a better way to achieve what I want?
- Is the proposed patch acceptable?  Any comments would be appreciated.

Thanks in advance.
thibault


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-publish.el-Option-to-use-headings-instead-of-list.patch --]
[-- Type: text/x-diff, Size: 4393 bytes --]

From a9ae0ecc623d2794475f3481765c637447f1ab24 Mon Sep 17 00:00:00 2001
From: thibault <thibault.marin@gmx.com>
Date: Wed, 28 Sep 2016 22:47:48 -0500
Subject: [PATCH] ox-publish.el: Option to use headings instead of list in
 sitemap

* list/ox-publish.el (org-publish-org-sitemap): Add an optional
parameter allowing generation of a sitemap composed of headings
instead of list items.
---
 lisp/ox-publish.el | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/lisp/ox-publish.el b/lisp/ox-publish.el
index 14c93b2..ceb0673 100644
--- a/lisp/ox-publish.el
+++ b/lisp/ox-publish.el
@@ -716,15 +716,17 @@ If `:auto-sitemap' is set, publish the sitemap too.  If
 	      ((functionp fun) (funcall fun project-plist))))
       (org-publish-write-cache-file))))
 
-(defun org-publish-org-sitemap (project &optional sitemap-filename)
+(defun org-publish-org-sitemap
+    (project &optional sitemap-filename use-headings)
   "Create a sitemap of pages in set defined by PROJECT.
 Optionally set the filename of the sitemap with SITEMAP-FILENAME.
-Default for SITEMAP-FILENAME is `sitemap.org'."
+Default for SITEMAP-FILENAME is `sitemap.org'.  When USE-HEADINGS
+is nil (default), the sitemap produced is a list, otherwise it
+uses headings."
   (let* ((project-plist (cdr project))
 	 (dir (file-name-as-directory
 	       (plist-get project-plist :base-directory)))
 	 (localdir (file-name-directory dir))
-	 (indent-str (make-string 2 ?\ ))
 	 (exclude-regexp (plist-get project-plist :exclude))
 	 (files (nreverse
 		 (org-publish-get-base-files project exclude-regexp)))
@@ -736,7 +738,18 @@ Default for SITEMAP-FILENAME is `sitemap.org'."
 	 (sitemap-sans-extension
 	  (plist-get project-plist :sitemap-sans-extension))
 	 (visiting (find-buffer-visiting sitemap-filename))
-	 file sitemap-buffer)
+	 file sitemap-buffer
+	 (entry-string
+	  (if use-headings
+	      `((indent . ,(make-string 1 ?*))
+		(char-space . " "))
+	      `((indent . ,(make-string 2 ?\ ))
+		(char-space . " + "))))
+	 (entry-string-indent (cdr (assq 'indent entry-string)))
+	 (entry-string-char (string-to-char
+			     (substring entry-string-indent 0 1)))
+	 (entry-string-indent-char (cdr (assq 'char-space entry-string)))
+	 (indent-str entry-string-indent))
     (with-current-buffer
 	(let ((org-inhibit-startup t))
 	  (setq sitemap-buffer
@@ -758,7 +771,7 @@ Default for SITEMAP-FILENAME is `sitemap.org'."
 				     (file-name-directory link)))
 	      (unless (string= localdir oldlocal)
 		(if (string= localdir dir)
-		    (setq indent-str (make-string 2 ?\ ))
+		    (setq indent-str entry-string-indent)
 		  (let ((subdirs
 			 (split-string
 			  (directory-file-name
@@ -767,16 +780,18 @@ Default for SITEMAP-FILENAME is `sitemap.org'."
 			(subdir "")
 			(old-subdirs (split-string
 				      (file-relative-name oldlocal dir) "/")))
-		    (setq indent-str (make-string 2 ?\ ))
+		    (setq indent-str entry-string-indent)
 		    (while (string= (car old-subdirs) (car subdirs))
-		      (setq indent-str (concat indent-str (make-string 2 ?\ )))
+		      (setq indent-str (concat indent-str entry-string-indent))
 		      (pop old-subdirs)
 		      (pop subdirs))
 		    (dolist (d subdirs)
 		      (setq subdir (concat subdir d "/"))
-		      (insert (concat indent-str " + " d "\n"))
+		      (insert (concat indent-str entry-string-indent-char d "\n"))
 		      (setq indent-str (make-string
-					(+ (length indent-str) 2) ?\ )))))))
+					(+ (length indent-str)
+					   (length entry-string-indent))
+					entry-string-char)))))))
 	    ;; This is common to 'flat and 'tree
 	    (let ((entry
 		   (org-publish-format-file-entry
@@ -784,12 +799,14 @@ Default for SITEMAP-FILENAME is `sitemap.org'."
 		  (regexp "\\(.*\\)\\[\\([^][]+\\)\\]\\(.*\\)"))
 	      (cond ((string-match-p regexp entry)
 		     (string-match regexp entry)
-		     (insert (concat indent-str " + " (match-string 1 entry)
+		     (insert (concat indent-str entry-string-indent-char
+				     (match-string 1 entry)
 				     "[[file:" link "]["
 				     (match-string 2 entry)
 				     "]]" (match-string 3 entry) "\n")))
 		    (t
-		     (insert (concat indent-str " + [[file:" link "]["
+		     (insert (concat indent-str entry-string-indent-char
+				     "[[file:" link "]["
 				     entry
 				     "]]\n"))))))))
       (save-buffer))
-- 
2.9.3


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2016-11-02 10:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-29  4:09 Use headings in sitemap Thibault Marin
2016-10-11 16:34 ` Nicolas Goaziou
2016-10-12  4:52   ` Thibault Marin
2016-10-12  9:12   ` Rasmus Pank Roulund
2016-10-12 10:20     ` Nicolas Goaziou
2016-10-30 17:38       ` Nicolas Goaziou
2016-10-31  3:38         ` Thibault Marin
2016-10-31  8:04           ` Nicolas Goaziou
2016-10-31 12:57             ` Thibault Marin
2016-10-31 15:21               ` Nicolas Goaziou
2016-11-01  2:36                 ` Thibault Marin
2016-11-02 10:08                   ` Nicolas Goaziou

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).