33; ; Author: Bob Weiner
44; ;
55; ; Orig-Date: 21-Apr-24 at 22:41:13
6- ; ; Last-Mod: 26-Oct -25 at 11:48:45 by Bob Weiner
6+ ; ; Last-Mod: 2-Nov -25 at 17:09:25 by Mats Lidell
77; ;
88; ; SPDX-License-Identifier: GPL-3.0-or-later
99; ;
@@ -2845,11 +2845,29 @@ If not found, set it up and return the new project properties."
28452845 (concat hywiki-org-link-type " :" ))
28462846 (hywiki-word-read)))
28472847
2848+ (defun hywiki--org-link-html-format (path-stem suffix desc info )
2849+ " Format an html link using Org ids."
2850+ (let* ((heading (and suffix (not (string-empty-p suffix)) (substring suffix 1 )))
2851+ (link-obj (org-element-create
2852+ 'link
2853+ (list
2854+ :type " file"
2855+ :path (concat path-stem " .org" )
2856+ :search-option (and heading (concat " *" heading))
2857+ :format 'bracket )))
2858+ ; ; Export as HTML
2859+ (exported (org-export-data link-obj info))
2860+ ; ; Extract the href
2861+ (href (if (string-match " href=\" \\ ([^\" ]+\\ )\" " exported)
2862+ (match-string 1 exported)
2863+ exported)))
2864+ (format " <a href=\" %s \" >%s </a> " href desc)))
2865+
28482866; ;; Next two functions derived from the denote package.
28492867;;;### autoload
2850- (defun hywiki-org-link-export (link description format )
2868+ (defun hywiki-org-link-export (link description format info )
28512869 " Export a HyWikiWord Org-format `hy:' link to various formats.
2852- The LINK, DESCRIPTION, and FORMAT are provided by the export
2870+ The LINK, DESCRIPTION, FORMAT and INFO are provided by the export
28532871backend."
28542872 (let* ((path-word-suffix (hywiki-reference-to-referent link :full-data ))
28552873 (path (when path-word-suffix
@@ -2866,11 +2884,7 @@ backend."
28662884 (if path
28672885 (pcase format
28682886 (`ascii (format " [%s ] <%s :%s > " hywiki-org-link-type desc path))
2869- (`html (format " <a href=\" %s .html%s \" >%s </a> "
2870- path-stem
2871- (hpath:spaces-to-dashes-markup-anchor
2872- (or suffix " " ))
2873- desc))
2887+ (`html (hywiki--org-link-html-format path-stem suffix desc info))
28742888 (`latex (format " \\ href{%s .latex}{%s }" (replace-regexp-in-string " [\\ {}$%&_#~^]" " \\\\\\ &" path-stem) desc))
28752889 (`md (format " [%s ](%s .md%s ) " desc path-stem
28762890 (hpath:spaces-to-dashes-markup-anchor
@@ -2983,12 +2997,7 @@ Customize this directory with:
29832997 ; ; Export Org to html with useful link ids.
29842998 ; ; Instead of random ids like "orga1b2c3", use heading titles with
29852999 ; ; spaces replaced with dashes, made unique when necessary.
2986- (unwind-protect
2987- (progn
2988- ; ; (advice-add #'org-export-get-reference :override #'hywiki--org-export-get-reference)
2989- (org-publish-project " hywiki" all-pages-flag))
2990- ; ; (advice-remove #'org-export-get-reference #'hywiki--org-export-get-reference)
2991- nil ))
3000+ (org-publish-project " hywiki" all-pages-flag))
29923001
29933002(defun hywiki-referent-exists-p (&optional word start end )
29943003 " Return the HyWikiWord at point or optional HyWiki WORD, if has a referent.
@@ -3798,121 +3807,6 @@ This must be called within a `save-excursion' or it may move point."
37983807 (goto-char (1- (point ))))
37993808 (hywiki-maybe-highlight-between-page-names))
38003809
3801- ; ;; ************************************************************************
3802- ; ;; Private Org export override functions
3803- ; ;; ************************************************************************
3804-
3805- ; ; Thanks to alphapapa for the GPLed code upon which these hywiki--org
3806- ; ; functions are based. These change the html ids that Org export
3807- ; ; generates to use the text of headings rather than randomly
3808- ; ; generated ids.
3809-
3810- (require 'cl-extra ) ; ; for `cl-some'
3811- (require 'ox ) ; ; for `org-export-get-reference'
3812- (require 'url-util ) ; ; for `url-hexify-string'
3813-
3814- (defun hywiki--org-export-get-reference (datum info )
3815- " Return a unique reference for DATUM, as a string.
3816- Like `org-export-get-reference' but uses modified heading strings as
3817- link ids rather than generated ids. To form an id, spaces in headings
3818- are replaced with dashes and to make each id unique, heading parent
3819- ids are prepended separated by '--'.
3820-
3821- DATUM is either an element or an object. INFO is the current
3822- export state, as a plist.
3823-
3824- References for the current document are stored in the
3825- `:internal-references' property. Its value is an alist with
3826- associations of the following types:
3827-
3828- (REFERENCE . DATUM) and (SEARCH-CELL . ID)
3829-
3830- REFERENCE is the reference string to be used for object or
3831- element DATUM. SEARCH-CELL is a search cell, as returned by
3832- `org-export-search-cells' . ID is a number or a string uniquely
3833- identifying DATUM within the document.
3834-
3835- This function also checks `:crossrefs' property for search cells
3836- matching DATUM before creating a new reference."
3837- (let ((cache (plist-get info :internal-references )))
3838- (or (car (rassq datum cache))
3839- (let* ((crossrefs (plist-get info :crossrefs ))
3840- (cells (org-export-search-cells datum))
3841- ; ; Preserve any pre-existing association between
3842- ; ; a search cell and a reference, i.e., when some
3843- ; ; previously published document referenced a location
3844- ; ; within current file (see
3845- ; ; `org-publish-resolve-external-link' ).
3846- ; ;
3847- ; ; However, there is no guarantee that search cells are
3848- ; ; unique, e.g., there might be duplicate custom ID or
3849- ; ; two headings with the same title in the file.
3850- ; ;
3851- ; ; As a consequence, before reusing any reference to
3852- ; ; an element or object, we check that it doesn't refer
3853- ; ; to a previous element or object.
3854- (new (or (when (org-element-property :raw-value datum)
3855- ; ; Heading with a title
3856- (hywiki--org-export-new-title-reference datum cache))
3857- (cl-some
3858- (lambda (cell )
3859- (let ((stored (cdr (assoc cell crossrefs))))
3860- (when stored
3861- (let ((old (org-export-format-reference stored)))
3862- (and (not (assoc old cache)) stored)))))
3863- cells)
3864- (org-export-format-reference
3865- (org-export-new-reference cache))))
3866- (reference-string new))
3867- ; ; Cache contains both data already associated to
3868- ; ; a reference and in-use internal references, so as to make
3869- ; ; unique references.
3870- (dolist (cell cells) (push (cons cell new) cache))
3871- ; ; Retain a direct association between reference string and
3872- ; ; DATUM since (1) not every object or element can be given
3873- ; ; a search cell (2) it permits quick lookup.
3874- (push (cons reference-string datum) cache)
3875- (plist-put info :internal-references cache)
3876- reference-string))))
3877-
3878- (defun hywiki--org-export-new-title-reference (datum cache )
3879- " Return new heading title reference for DATUM that is unique in CACHE."
3880- (let* ((title (org-element-property :raw-value datum))
3881- (ref (hywiki--org-format-reference title))
3882- (parent (org-element-property :parent datum))
3883- raw-parent)
3884- (while (cl-some (lambda (elt ) (equal ref (car elt)))
3885- cache)
3886- ; ; Title not unique: make it so.
3887- (if parent
3888- ; ; Append ancestor title.
3889- (setq raw-parent (org-element-property :raw-value parent)
3890- title (if (and (stringp raw-parent) (not (string-empty-p raw-parent)))
3891- (concat raw-parent " --" title)
3892- title)
3893- ref (hywiki--org-format-reference title)
3894- parent (org-element-property :parent parent))
3895- ; ; No more ancestors: add and increment a number.
3896- (when (string-match " \\ `\\ ([[:unibyte:]]\\ )+?\\ (--\\ ([0-9]+\\ )\\ )?\\ '"
3897- ref)
3898- (let ((num (match-string 3 ref)))
3899- (setq parent (match-string 1 ref)
3900- parent (if (stringp parent) (concat parent " --" ) " " )
3901- num (if num
3902- (string-to-number num)
3903- 0 )
3904- num (1+ num)
3905- ref (format " %s%s " parent num))))))
3906- ref))
3907-
3908- (defun hywiki--org-format-reference (title )
3909- " Format TITLE string as an html id."
3910- (url-hexify-string
3911- (replace-regexp-in-string " \\ [\\ [\\ ([a-z]+:\\ )?\\ |\\ ]\\ [\\ |\\ ]\\ ]" " "
3912- (subst-char-in-string
3913- ?\ ?-
3914- (substring-no-properties title)))))
3915-
39163810; ;; ************************************************************************
39173811; ;; Private initializations
39183812; ;; ************************************************************************
0 commit comments