Skip to content

Commit

Permalink
Merge pull request #12 from ardumont/0.0.8
Browse files Browse the repository at this point in the history
0.0.8
  • Loading branch information
ardumont committed Jul 15, 2015
2 parents 1265e1e + 67b5403 commit 30b9cb4
Show file tree
Hide file tree
Showing 12 changed files with 183 additions and 144 deletions.
1 change: 1 addition & 0 deletions .ert-runner
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-L .
39 changes: 18 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
language: emacs-lisp
env:
global:
- PATH=$HOME/.cask/bin:$HOME/.evm/bin:$PATH
matrix:
# - EVM_EMACS=emacs-24.1-bin
# - EVM_EMACS=emacs-24.2-bin
- EVM_EMACS=emacs-24.3-bin
- EVM_EMACS=emacs-24.4-bin
- EVM_EMACS=emacs-24.5-bin
before_install:
- mv Cask Cask.origin
- grep -v "marmalade" Cask.origin > Cask
- if [ "$EMACS" = 'emacs-snapshot' ]; then
sudo add-apt-repository -y ppa:cassou/emacs &&
sudo apt-get update -qq &&
sudo apt-get install -qq
emacs-snapshot-el emacs-snapshot-gtk emacs-snapshot;
fi
- if [ "$EMACS" = 'emacs24' ]; then
sudo add-apt-repository -y ppa:cassou/emacs &&
sudo apt-get update -qq &&
sudo apt-get install -qq
emacs24 emacs24-el emacs24-common-non-dfsg;
fi
- curl -fsSkL https://raw.github.com/cask/cask/master/go | python
- export PATH="/home/travis/.cask/bin:$PATH"
# evm install
- curl -fsSkL https://gist.github.com/rejeep/7736123/raw > travis.sh && source ./travis.sh
# install the matrix's emacs version
- evm install $EVM_EMACS --use --skip
- cask --version
- emacs --version
# install deps for cask
- cask
env:
#- EMACS=emacs
- EMACS=emacs24
# - EMACS=emacs-snapshot
script:
./run-travis-ci.sh
- pwd
- make install test
2 changes: 2 additions & 0 deletions Cask
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
(package-file "markdown-toc.el")

(development
(depends-on "undercover")
(depends-on "ert-runner")
(depends-on "ert")
(depends-on "ert-expectations")
(depends-on "el-mock"))
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ clean: clean-dist
install:
cask install

test-init:
cask exec ert-runner init

test: clean
cask exec $(EMACS) --batch \
-l ert \
-l ./test/load-markdown-toc-tests.el \
-f ert-run-tests-batch-and-exit
cask exec ert-runner

pkg-file:
cask pkg-file
Expand Down
42 changes: 19 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
markdown-toc [![Build Status](https://travis-ci.org/ardumont/markdown-toc.png?branch=master)](https://travis-ci.org/ardumont/markdown-toc) [![MELPA Stable](http://stable.melpa.org/packages/markdown-toc-badge.svg)](http://stable.melpa.org/#/markdown-toc) [![MELPA](http://melpa.org/packages/markdown-toc-badge.svg)](http://melpa.org/#/markdown-toc)
markdown-toc [![Build Status](https://travis-ci.org/ardumont/markdown-toc.png?branch=master)](https://travis-ci.org/ardumont/markdown-toc) [![Coverage Status](https://coveralls.io/repos/ardumont/markdown-toc/badge.svg?branch=master&service=github)](https://coveralls.io/github/ardumont/markdown-toc?branch=master) [![MELPA Stable](http://stable.melpa.org/packages/markdown-toc-badge.svg)](http://stable.melpa.org/#/markdown-toc) [![MELPA](http://melpa.org/packages/markdown-toc-badge.svg)](http://melpa.org/#/markdown-toc)
============

<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc/generate-toc again -->
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
**Table of Contents**

- [Use](#use)
Expand All @@ -28,44 +28,40 @@ A simple mode to create TOC in a markdown file.

Inside a markdown file, the first time, place yourself where you want to insert the TOC:

<kbd>M-x markdown-toc/generate-toc</kbd>
<kbd>M-x markdown-toc-generate-toc</kbd>

This will compute the TOC and insert it at current position.

Here is one possible output:

```markdown
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc/generate-toc again -->
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
**Table of Contents**

- [some markdown page title](#some-markdown-page-title)
- [main title](#main-title)
- [Sources](#sources)
- [Marmalade (recommended)](#marmalade-recommended)
- [Melpa-stable](#melpa-stable)
- [Melpa (~snapshot)](#melpa-~snapshot)
- [Install](#install)
- [Load org-trello](#load-org-trello)
- [Alternative](#alternative)
- [Git](#git)
- [Tar](#tar)
- [another title](#another-title)
- [with](#with)
- [some](#some)
- [heading](#heading)

<!-- markdown-toc end -->
- [Use](#use)
- [Create](#create)
- [Update](#update)
- [Create elsewhere](#create-elsewhere)
- [Install](#install)
- [emacs package repository](#emacs-package-repository)
- [Setup](#setup)
- [melpa stable](#melpa-stable)
- [melpa](#melpa)
- [marmalade](#marmalade)
- [Install](#install)
- [emacs-lisp file](#emacs-lisp-file)
- [Inspiration](#inspiration)
```

## 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 Down
6 changes: 3 additions & 3 deletions markdown-toc-pkg.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(define-package "markdown-toc" "0.0.7" "A simple TOC generator for markdown file"
'((s "1.7.0")
(dash "2.5.0")
(define-package "markdown-toc" "0.0.8" "A simple TOC generator for markdown file"
'((s "1.9.0")
(dash "2.11.0")
(markdown-mode "2.0")))
82 changes: 43 additions & 39 deletions markdown-toc.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
;; Maintainer: Antoine R. Dumont
;; URL: http://github.com/ardumont/markdown-toc
;; Created: 24th May 2014
;; Version: 0.0.7
;; Version: 0.0.8
;; Keywords: markdown, toc, tools,
;; Package-Requires: ((markdown-mode "2.0") (dash "2.5.0") (s "1.7.0"))
;; Package-Requires: ((markdown-mode "2.0") (dash "2.11.0") (s "1.9.0"))

;; This file is NOT part of GNU Emacs.

Expand All @@ -28,12 +28,12 @@

;;; Commentary:

;; Generate a TOC from a markdown file: M-x markdown-toc/generate-toc
;; Generate a TOC from a markdown file: M-x markdown-toc-generate-toc
;; This will compute the TOC at insert it at current position.
;; Update existing TOC: C-u M-x markdown-toc/generate-toc
;; Update existing TOC: C-u M-x markdown-toc-generate-toc

;; Here is a possible output:
;; <!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc/generate-toc again -->
;; <!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-generate-toc again -->
;; **Table of Contents**

;; - [some markdown page title](#some-markdown-page-title)
Expand Down Expand Up @@ -62,34 +62,36 @@
(require 'dash)
(require 'markdown-mode)

(defconst *MARKDOWN-TOC/VERSION* "0.0.7" "Current version installed.")
(defconst markdown--toc-version "0.0.8" "Current version installed.")

;;;###autoload
(defun markdown-toc/version ()
(defun markdown-toc-version ()
"Markdown-toc version."
(interactive)
(message "markdown-toc version: %s" *MARKDOWN-TOC/VERSION*))
(message "markdown-toc version: %s" markdown--toc-version))

(defun markdown-toc/--compute-toc-structure-from-level (level menu-index)
(defalias 'markdown-toc/version 'markdown-toc-version)

(defun markdown-toc--compute-toc-structure-from-level (level menu-index)
"Given a LEVEL and a MENU-INDEX, compute the toc structure."
(when menu-index
(let* ((fst (car menu-index))
(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)
(defun markdown-toc--compute-toc-structure (imenu-index)
"Given a IMENU-INDEX, compute the TOC structure."
(--mapcat (markdown-toc/--compute-toc-structure-from-level 0 it) imenu-index))
(--mapcat (markdown-toc--compute-toc-structure-from-level 0 it) imenu-index))

(defun markdown-toc/--symbol (sym n)
(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)))

(defun markdown-toc/--to-link (title)
(defun markdown-toc--to-link (title)
"Given a TITLE, return the markdown link associated."
(format "[%s](#%s)" title
(->>
Expand All @@ -98,70 +100,72 @@
(replace-regexp-in-string "[^a-z0-9 -]" "")
(s-replace " " "-"))))

(defun markdown-toc/--to-markdown-toc (level-title-toc-list)
(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))))
(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 -->")
(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! ()
(defun markdown-toc--toc-already-present-p ()
"Determine if a TOC has already been generated.
Return the end position if it exists, nil otherwise."
(save-excursion
(goto-char (point-min))
(re-search-forward *markdown-toc/header-toc-start* nil t)))
(re-search-forward markdown-toc--header-toc-start nil t)))

(defun markdown-toc/--toc-start! ()
(defun markdown-toc--toc-start ()
"Compute the toc's starting point."
(save-excursion
(goto-char (markdown-toc/--toc-already-present-p!))
(goto-char (markdown-toc--toc-already-present-p))
(point-at-bol)))

(defun markdown-toc/--toc-end! ()
(defun markdown-toc--toc-end ()
"Compute the toc's end point."
(save-excursion
(goto-char (point-min))
(re-search-forward *markdown-toc/header-toc-end* nil t)))
(re-search-forward markdown-toc--header-toc-end nil t)))

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

(defun markdown-toc/--compute-full-toc (toc)
(defun markdown-toc--compute-full-toc (toc)
"Given the TOC's content, compute the full toc with comments and title."
(format "%s\n%s\n\n%s\n\n%s\n"
*markdown-toc/header-toc-start*
*markdown-toc/header-toc-title*
markdown-toc--header-toc-start
markdown-toc--header-toc-title
toc
*markdown-toc/header-toc-end*))
markdown-toc--header-toc-end))

;;;###autoload
(defun markdown-toc/generate-toc (&optional replace-toc-p)
(defun markdown-toc-generate-toc (&optional replace-toc-p)
"Generate a TOC for markdown file at current point.
Deletes any previous TOC.
If called interactively with prefix arg REPLACE-TOC-P, replaces previous TOC."
(interactive "P")
(save-excursion
(when (markdown-toc/--toc-already-present-p!)
(when (markdown-toc--toc-already-present-p)
;; when toc already present, remove it
(let ((region-start (markdown-toc/--toc-start!))
(region-end (markdown-toc/--toc-end!)))
(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)))
markdown-toc--generate-toc
insert)))

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

(provide 'markdown-toc)
;;; markdown-toc.el ends here
9 changes: 9 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 0.0.8

- [X] Add test coverage
- [X] Improve CI code
- [X] Respect elisp conventions
- [X] Update version
- [X] Update version dependencies
- [X] Release notes

# 0.0.7

- [X] Add prefix argument to `markdown-toc/generate-toc' to replace existing TOC at the same location #10
Expand Down
13 changes: 0 additions & 13 deletions run-travis-ci.sh

This file was deleted.

Loading

0 comments on commit 30b9cb4

Please sign in to comment.