Skip to content

Commit

Permalink
Merge pull request #17 from ardumont/0.0.9
Browse files Browse the repository at this point in the history
0.0.9
  • Loading branch information
ardumont committed Dec 23, 2015
2 parents 44c79a3 + a16e981 commit c2ac578
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 103 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.cask/
/test.md
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: emacs-lisp
sudo: false
env:
global:
- PATH=$HOME/.cask/bin:$HOME/.evm/bin:$PATH
Expand All @@ -10,9 +11,8 @@ env:
- EVM_EMACS=emacs-24.5-bin
before_install:
# evm install
- curl -fsSkL https://gist.github.com/rejeep/7736123/raw > travis.sh && source ./travis.sh
- curl -fsSkL https://gist.github.com/rejeep/ebcd57c3af83b049833b/raw > x.sh && source ./x.sh
# install the matrix's emacs version
- evm install $EVM_EMACS --use --skip
- cask --version
- emacs --version
# install deps for cask
Expand Down
67 changes: 61 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ markdown-toc

<!-- markdown-toc end -->

A simple mode to create TOC in a markdown file.
A simple mode to create TOC in a well-formed markdown file.

Limitations:

The TOC is well-formed if the markdown is. (cf. #15)

# Use

Expand Down Expand Up @@ -55,15 +59,63 @@ Here is one possible output:
- [Inspiration](#inspiration)
```

## User toc manipulation

If the user would want to enhance the generated toc, (s)he could use
the following function markdown-toc-user-toc-structure-manipulation-fn:

It expects as argument the toc-structure markdown-toc uses to generate
the toc. The remaining code expects a similar structure.

Example:

``` lisp
'((0 . \"some markdown page title\")
(0 . \"main title\")
(1 . \"Sources\")
(2 . \"Marmalade (recommended)\")
(2 . \"Melpa-stable\")
(2 . \"Melpa (~snapshot)\")
(1 . \"Install\")
(2 . \"Load org-trello\")
(2 . \"Alternative\")
(3 . \"Git\")
(3 . \"Tar\")
(0 . \"another title\")
(1 . \"with\")
(1 . \"some\")
(1 . \"heading\"))
```

So for example, as asked in #16, one could drop the first element:

``` lisp
(custom-set-variables '(markdown-toc-user-toc-structure-manipulation-fn 'cdr))
```

Or drop all h1 titles... or whatever:

``` lisp
(require 'dash)
(custom-set-variables '(markdown-toc-user-toc-structure-manipulation-fn
(lambda (toc-structure)
(-filter (lambda (l) (let ((index (car l)))
(<= 1 index)))
toc-structure)))
```

## Update

To update the existing TOC, simply execute again: <kbd>C-u M-x markdown-toc-generate-toc</kbd>
To update the existing TOC, simply execute again:
<kbd>C-u M-x markdown-toc-generate-toc</kbd>

This will update the current TOC.

## Create elsewhere

To create another updated TOC elsewhere, execute again <kbd>M-x markdown-toc-generate-toc</kbd>, this will remove the old TOC and insert the updated one from where you stand.
To create another updated TOC elsewhere, execute again <kbd>M-x
markdown-toc-generate-toc</kbd>, this will remove the old TOC and
insert the updated one from where you stand.

# Install

Expand All @@ -77,7 +129,8 @@ You need to add melpa or melpa-stable package repository before installing it.

``` lisp
(require 'package)
(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/"))
(add-to-list 'package-archives '("melpa-stable" .
"http://melpa-stable.milkbox.net/packages/"))
(package-initialize)
```

Expand All @@ -87,7 +140,8 @@ Then hit <kbd>M-x eval-buffer</kbd> to evaluate the buffer's contents.

``` lisp
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("melpa" .
"http://melpa.milkbox.net/packages/"))
(package-initialize)
```

Expand All @@ -97,7 +151,8 @@ Then hit <kbd>M-x eval-buffer</kbd> to evaluate the buffer's contents.

``` lisp
(require 'package)
(add-to-list 'package-archives '("marmalade" . "http://marmalade-repo.org/packages/"))
(add-to-list 'package-archives '("marmalade" .
"http://marmalade-repo.org/packages/"))
(package-initialize)
```

Expand Down
2 changes: 1 addition & 1 deletion markdown-toc-pkg.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(define-package "markdown-toc" "0.0.8" "A simple TOC generator for markdown file"
(define-package "markdown-toc" "0.0.9" "A simple TOC generator for markdown file"
'((s "1.9.0")
(dash "2.11.0")
(markdown-mode "2.0")))
90 changes: 62 additions & 28 deletions markdown-toc.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; Maintainer: Antoine R. Dumont
;; URL: http://github.com/ardumont/markdown-toc
;; Created: 24th May 2014
;; Version: 0.0.8
;; Version: 0.0.9
;; Keywords: markdown, toc, tools,
;; Package-Requires: ((markdown-mode "2.0") (dash "2.11.0") (s "1.9.0"))

Expand Down Expand Up @@ -62,7 +62,7 @@
(require 'dash)
(require 'markdown-mode)

(defconst markdown--toc-version "0.0.8" "Current version installed.")
(defconst markdown--toc-version "0.0.9" "Current version installed.")

;;;###autoload
(defun markdown-toc-version ()
Expand All @@ -79,7 +79,9 @@
(tail (cdr menu-index))
(ttail (if (integerp tail) nil (cdr tail))))
(cons `(,level . ,fst)
(--mapcat (markdown-toc--compute-toc-structure-from-level (+ 1 level) it) ttail)))))
(--mapcat
(markdown-toc--compute-toc-structure-from-level (+ 1 level) it)
ttail)))))

(defun markdown-toc--compute-toc-structure (imenu-index)
"Given a IMENU-INDEX, compute the TOC structure."
Expand All @@ -88,29 +90,32 @@
(defun markdown-toc--symbol (sym n)
"Compute the repetition of a symbol SYM N times as a string."
(--> n
(-repeat it sym)
(s-join "" it)))
(-repeat it sym)
(s-join "" it)))

(defun markdown-toc--to-link (title)
"Given a TITLE, return the markdown link associated."
(format "[%s](#%s)" title
(->>
title
downcase
(replace-regexp-in-string "[^a-z0-9 -]" "")
(s-replace " " "-"))))
(->> title
downcase
(replace-regexp-in-string "[^a-z0-9 -]" "")
(s-replace " " "-"))))

(defun markdown-toc--to-markdown-toc (level-title-toc-list)
"Given LEVEL-TITLE-TOC-LIST, a list of pair level, title, return a TOC string."
(->> level-title-toc-list
(--map (let ((nb-spaces (* 4 (car it)))
(title (cdr it)))
(format "%s- %s" (markdown-toc--symbol " " nb-spaces) (markdown-toc--to-link title))))
(s-join "\n")))

(defconst markdown-toc--header-toc-start "<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->")
(defconst markdown-toc--header-toc-title "**Table of Contents**")
(defconst markdown-toc--header-toc-end "<!-- markdown-toc end -->")
(--map (let ((nb-spaces (* 4 (car it)))
(title (cdr it)))
(format "%s- %s" (markdown-toc--symbol " " nb-spaces)
(markdown-toc--to-link title))))
(s-join "\n")))

(defconst markdown-toc--header-toc-start
"<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->")
(defconst markdown-toc--header-toc-title
"**Table of Contents**")
(defconst markdown-toc--header-toc-end
"<!-- markdown-toc end -->")

(defun markdown-toc--toc-already-present-p ()
"Determine if a TOC has already been generated.
Expand All @@ -131,10 +136,9 @@ Return the end position if it exists, nil otherwise."
(goto-char (point-min))
(re-search-forward markdown-toc--header-toc-end nil t)))

(defun markdown-toc--generate-toc (toc-index)
"Given a TOC-INDEX, compute a new toc."
(-> toc-index
markdown-toc--compute-toc-structure
(defun markdown-toc--generate-toc (toc-structure)
"Given a TOC-STRUCTURE, compute a new toc."
(-> toc-structure
markdown-toc--to-markdown-toc
markdown-toc--compute-full-toc))

Expand All @@ -146,7 +150,36 @@ Return the end position if it exists, nil otherwise."
toc
markdown-toc--header-toc-end))

(defcustom markdown-toc-user-toc-structure-manipulation-fn
(lambda (toc-structure) toc-structure)
"User crafted function to manipulate toc-structure as user sees fit.
The toc-structure has the following form:
'((0 . \"some markdown page title\")
(0 . \"main title\")
(1 . \"Sources\")
(2 . \"Marmalade (recommended)\")
(2 . \"Melpa-stable\")
(2 . \"Melpa (~snapshot)\")
(1 . \"Install\")
(2 . \"Load org-trello\")
(2 . \"Alternative\")
(3 . \"Git\")
(3 . \"Tar\")
(0 . \"another title\")
(1 . \"with\")
(1 . \"some\")
(1 . \"heading\"))
If the user wanted to remove the first element, it could for
example define the following function:
(custom-set-variables
'(markdown-toc-user-toc-structure-manipulation-fn 'cdr))
Default to identity function (do nothing).")

;;;###autoload

(defun markdown-toc-generate-toc (&optional replace-toc-p)
"Generate a TOC for markdown file at current point.
Deletes any previous TOC.
Expand All @@ -158,12 +191,13 @@ If called interactively with prefix arg REPLACE-TOC-P, replaces previous TOC."
(let ((region-start (markdown-toc--toc-start))
(region-end (markdown-toc--toc-end)))
(delete-region region-start (1+ region-end))
(if replace-toc-p
(goto-char region-start))))
;; generate the toc
(-> (markdown-imenu-create-index)
markdown-toc--generate-toc
insert)))
(when replace-toc-p
(goto-char region-start))))
(->> (markdown-imenu-create-index)
markdown-toc--compute-toc-structure
(funcall markdown-toc-user-toc-structure-manipulation-fn)
markdown-toc--generate-toc
insert)))

(defalias 'markdown-toc/generate-toc 'markdown-toc-generate-toc)

Expand Down
8 changes: 8 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 0.0.9

- [X] Permit user-custom function to manipulate toc some more - #16
- [X] Improve testing
- [X] Clean some code
- [X] Update version
- [X] Clean up tests

# 0.0.8

- [X] Add test coverage
Expand Down
27 changes: 0 additions & 27 deletions test/load-markdown-toc-tests.el

This file was deleted.

Loading

0 comments on commit c2ac578

Please sign in to comment.