[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/toc-org 114dcc9813 053/128: make GitHub hrefs unique (fixe
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/toc-org 114dcc9813 053/128: make GitHub hrefs unique (fixes #16) |
Date: |
Sun, 2 Jan 2022 09:59:09 -0500 (EST) |
branch: elpa/toc-org
commit 114dcc9813e2d8784b8c21165c95408c1b26d86e
Author: Sergei Nosov <sergei.nosov@gmail.com>
Commit: Sergei Nosov <sergei.nosov@gmail.com>
make GitHub hrefs unique (fixes #16)
---
README.org | 10 +++++++---
toc-org.el | 35 +++++++++++++++++++++++++++--------
2 files changed, 34 insertions(+), 11 deletions(-)
diff --git a/README.org b/README.org
index 9989e17b7d..4778f45e44 100644
--- a/README.org
+++ b/README.org
@@ -96,13 +96,17 @@ Now =C-c C-q T RET= and you are done putting the =:TOC:=
entry.
Currently, only 2 href styles are supported: =gh= and =org=. You can easily
define your own styles. If you use the tag =:TOC_2_STYLE:= (=STYLE= being a
style name), then the package will look for a function named
-=toc-org-hrefify-STYLE=, which accepts a heading string and returns a href
-corresponding to that heading.
+=toc-org-hrefify-STYLE=.
+
+It should accept a heading string and a hash table of previously generated
+hrefs. The table can be used to maintain href uniqueness (see
+=toc-org-hrefify-gh=, for example). Return value should be a href corresponding
+to that heading.
E.g. for =org= style it simply returns input as is:
#+BEGIN_SRC emacs-lisp
- (defun toc-org-hrefify-org (str)
+ (defun toc-org-hrefify-org (str &optional hash)
"Given a heading, transform it into a href using the org-mode
rules."
str)
diff --git a/toc-org.el b/toc-org.el
index f092954418..6ea2a78dd3 100644
--- a/toc-org.el
+++ b/toc-org.el
@@ -160,21 +160,38 @@ auxiliary text."
"* About\n:TOC:\n drawer\n:END:\n\ntoc-org is a utility to have an
up-to-date table of contents in the\norg files without exporting (useful
primarily for readme files on\nGitHub).\n\nIt is similar to the
[[https://github.com/ardumont/markdown-toc][markdown-toc]] package, but works
for org files.\n:TOC:\n drawer\n:END:\n\n* Table of Contents
:TOC:\n - [[#about][About]]\n -
[[#use][Use]]\n - [[#different-href-styles][Different href st [...]
"* About\n* Installation\n** via package.el\n** Manual\n* Use\n* Different
href styles\n* Example\n"))
-(defun toc-org-hrefify-gh (str)
+(defun toc-org-hrefify-gh (str &optional hash)
"Given a heading, transform it into a href using the GitHub
rules."
(let* ((spc-fix (replace-regexp-in-string " " "-" str))
(upcase-fix (replace-regexp-in-string "[A-Z]" 'downcase spc-fix t))
- (special-chars-fix (replace-regexp-in-string
toc-org-special-chars-regexp "" upcase-fix t)))
- (concat "#" special-chars-fix)))
+ (special-chars-fix (replace-regexp-in-string
toc-org-special-chars-regexp "" upcase-fix t))
+ (hrefified-base (concat "#" special-chars-fix))
+ (hrefified hrefified-base)
+ (idx 0))
+ ;; try appending -1, -2, -3, etc. until unique href is found
+ (when hash
+ (while (gethash hrefified hash)
+ (setq hrefified
+ (concat hrefified-base "-" (number-to-string (setq idx (1+
idx)))))))
+ hrefified))
(ert-deftest test-toc-org-hrefify-gh ()
"Test the `toc-org-hrefify-gh' function"
(should (equal (toc-org-hrefify-gh "About") "#about"))
(should (equal (toc-org-hrefify-gh "!h@#$%^&*(){}|][:;\"'/?.>,<`~") "#h"))
- (should (equal (toc-org-hrefify-gh "!h@#$% ^&*(S){}|][:;\"'/?.>,<`~")
"#h-s")))
+ (should (equal (toc-org-hrefify-gh "!h@#$% ^&*(S){}|][:;\"'/?.>,<`~")
"#h-s"))
-(defun toc-org-hrefify-org (str)
+ (let ((hash (make-hash-table :test 'equal)))
+ (should (equal (toc-org-hrefify-gh "About" hash) "#about"))
+ (puthash "#about" "About" hash)
+ (should (equal (toc-org-hrefify-gh "About" hash) "#about-1"))
+ (puthash "#about-1" "About" hash)
+ (should (equal (toc-org-hrefify-gh "About" hash) "#about-2"))
+ (puthash "#about-2" "About" hash)
+ (should (equal (toc-org-hrefify-gh "About" hash) "#about-3"))))
+
+(defun toc-org-hrefify-org (str &optional hash)
"Given a heading, transform it into a href using the org-mode
rules."
str)
@@ -227,7 +244,7 @@ each heading into a link."
(end (line-end-position))
(heading (buffer-substring-no-properties
beg end))
- (hrefified (funcall hrefify heading)))
+ (hrefified (funcall hrefify heading hash)))
(insert "[[")
(insert hrefified)
(insert "][")
@@ -244,11 +261,13 @@ each heading into a link."
(ert-deftest test-toc-org-hrefify-toc ()
(let ((hash (make-hash-table :test 'equal)))
- (should (equal (toc-org-hrefify-toc "* About\n" 'upcase hash)
+ (should (equal (toc-org-hrefify-toc "* About\n"
+ (lambda (str &optional hash) (upcase
str))
+ hash)
" - [[ABOUT][About]]\n"))
(should (equal (gethash "ABOUT" hash) "About")))
(let ((hash (make-hash-table :test 'equal)))
- (should (equal (toc-org-hrefify-toc "* About\n* Installation\n** via
package.el\n** Manual\n* Use\n* Different href styles\n* Example\n" 'upcase
hash)
+ (should (equal (toc-org-hrefify-toc "* About\n* Installation\n** via
package.el\n** Manual\n* Use\n* Different href styles\n* Example\n" (lambda
(str &optional hash) (upcase str)) hash)
" - [[ABOUT][About]]\n - [[INSTALLATION][Installation]]\n
- [[VIA PACKAGE.EL][via package.el]]\n - [[MANUAL][Manual]]\n -
[[USE][Use]]\n - [[DIFFERENT HREF STYLES][Different href styles]]\n -
[[EXAMPLE][Example]]\n"))
(should (equal (gethash "ABOUT" hash) "About"))
(should (equal (gethash "INSTALLATION" hash) "Installation"))
- [nongnu] elpa/toc-org ebff38bfa4 094/128: make toc-org a minor mode (#61), (continued)
- [nongnu] elpa/toc-org ebff38bfa4 094/128: make toc-org a minor mode (#61), ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org aa9aea6d69 102/128: Supress warnings, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 215d3e41e3 114/128: fix C-c C-o for links with cookies, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org a2cb4c254f 128/128: Merge pull request #84 from skangas/nongnu-elpa, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org d66a41f6b7 010/128: strip tags, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org cac667fabd 018/128: Add a small note about global tag setup, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 752fe14c5b 042/128: fix "_1_org"-style suffixes, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 815c6d8c1b 044/128: flatten links when creating raw toc, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org e0cef2c6a1 027/128: print emacs version in travis log, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 8cf236450b 045/128: don't modify and save file on opening, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 114dcc9813 053/128: make GitHub hrefs unique (fixes #16),
ELPA Syncer <=
- [nongnu] elpa/toc-org 72883a08b0 049/128: update the TOC only if it's actually different from the current one, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 4e4b4e6484 057/128: generate correct toc when it's the last heading, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 4380d99113 065/128: add unit test for #31, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 970560a874 069/128: fix travis build, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 39d983c381 074/128: add noexport support, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 663e5ec281 116/128: Correct sample configuration to valid code, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org c83c19ddb4 093/128: Merge pull request #59 from snosov1/add-quote, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 35781c6313 126/128: Merge pull request #82 from skangas/add-license, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 08ed3f0883 061/128: Merge pull request #30 from snosov1/fix-28, ELPA Syncer, 2022/01/02
- [nongnu] elpa/toc-org 56b48ec37b 033/128: rename to toc-org, ELPA Syncer, 2022/01/02