Skip to content

Commit

Permalink
Merge branch 'features/discrete-headings'
Browse files Browse the repository at this point in the history
* features/discrete-headings: (27 commits)
  Rewrite according to suggestions
  Remove requirements.txt as it's not related to the feature itself
  Cleanup: Removing extra white space
  Delete wrong line
  Fix sentence
  Add message for 4.1.0
  Update TOC
  Add 'Excluded heading' into README
  Fix unit test
  Fix unit tests
  pip install black
  pip install pycodestyle
  Fix Python version
  Suppress appveyor error?
  rename discrete to exclude, fix unittests
  unit tests fixed
  quote alignment
  quotes
  rename: discrete → exclude
  Cleanup
  ...
  • Loading branch information
naokazuterada committed Jun 7, 2020
2 parents b20063f + 47ee29c commit 2b4f9f1
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.7.1
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Sublime Text 3 plugin for generating a Table of Contents (TOC) in a Markdown doc
- [Supported file extensions](#supported-file-extensions)
- [Customizing generation of TOC using attributes](#customizing-generation-of-toc-using-attributes)
- [Auto anchoring when heading has anchor defined](#auto-anchoring-when-heading-has-anchor-defined)
- [Auto linking for _clickable_ TOC](#auto-linking-for-clickable-toc)
- [Auto linking for _clickable_ TOC](#auto-linking-for-_clickable_-toc)
- [Lowercasing in ids](#lowercasing-in-ids)
- [Preserve case](#preserve-case)
- [Lowercase all characters](#lowercase-all-characters)
Expand All @@ -41,10 +41,12 @@ Sublime Text 3 plugin for generating a Table of Contents (TOC) in a Markdown doc
- [Customizable list bullets in TOC](#customizable-list-bullets-in-toc)
- [Specify custom indentation prefix](#specify-custom-indentation-prefix)
- [Preserve images in headings](#preserve-images-in-headings)
- [Excluded headings](#excluded-headings)
- [Usage](#usage)
- [Tips](#tips)
- [How to remove anchors added by MarkdownTOC](#how-to-remove-anchors-added-by-markdowntoc)
- [Addressing issues with Github Pages](#addressing-issues-with-github-pages)
- [Using MarkdownTOC with Markdownlint](#using-markdowntoc-with-markdownlint)
- [Limitations](#limitations)
- [Headings in lists are not included in the auto-generated table of contents](#headings-in-lists-are-not-included-in-the-auto-generated-table-of-contents)
- [Attributes](#attributes)
Expand Down Expand Up @@ -98,6 +100,7 @@ The **MarkdownTOC** plugin is rich on features and customization, useful for bot
- [Customizable list bullets in TOC](#customizable-list-bullets-in-toc)
- [Specify custom indentation prefix](#specify-custom-indentation-prefix)
- [Preserve images in headings](#preserve-images-in-headings)
- [Excluded headings](#excluded-heading)

### Insertion of TOC based on headings in Markdown document

Expand Down Expand Up @@ -722,6 +725,15 @@ Please note that the default for the [attribute](#attributes) is: `false`.

You can change your default setting in your [configuration](#configuration) with the key `defaults.remove_image`.

### Excluded headings

You can exclude certain headings in the TOC by adding a special comment to the line above the line with the heading, as shown below.

```markdown
<!-- MarkdownTOC:excluded -->
## This heading will be excluded
```

## Usage

1. Open your [Markdown] file
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_script:
# run tests with test coverage report
- ps: .\appveyor.ps1 "run_tests" -coverage -verbose

after_test:
on_finish:
- "SET PYTHON=C:\\Python33"
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
- pip install codecov
Expand Down
21 changes: 17 additions & 4 deletions markdowntoc/markdowntoc_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
PT_ANCHOR = re.compile(r'<a\s+id="[^"]+"\s*>\s*</a>')



# <!-- MarkdownTOC:excluded -->
PT_EXCLUDE = re.compile(
r'^<!--.*(MarkdownTOC:excluded).*-->', re.IGNORECASE
)


class MarkdowntocInsert(sublime_plugin.TextCommand, Base):

def run(self, edit):
Expand Down Expand Up @@ -138,6 +145,7 @@ def replace_brackets(m):
return _open + m.group(1) + _close
else:
return m.group(0)

return re.sub(_pattern, replace_brackets, _text)

_text = do_escape(_text, re.compile(
Expand Down Expand Up @@ -165,10 +173,15 @@ def get_toc(self, attrs, begin, edit):
for heading in headings:
if begin < heading.end():
lines = self.view.lines(heading)
previous_line = self.view.substr(
self.view.line(lines[0].a - 1))
if PT_EXCLUDE.match(previous_line):
continue

if len(lines) == 1:
# handle hash headings, ### chapter 1
r = sublime.Region(
heading.end() - 1, self.view.line(heading).end())
r = sublime.Region(heading.end() - 1,
self.view.line(heading).end())
text = self.view.substr(r).strip().rstrip('#')
indent = heading.size() - 1
items.append([indent, text, heading.begin()])
Expand All @@ -180,8 +193,8 @@ def get_toc(self, attrs, begin, edit):
# ----
text = self.view.substr(lines[0])
if text.strip():
indent = 1 if (
self.view.substr(lines[1])[0] == '=') else 2
heading_type = self.view.substr(lines[1])[0]
indent = 1 if heading_type == '=' else 2
items.append([indent, text, heading.begin()])

if len(items) < 1:
Expand Down
6 changes: 6 additions & 0 deletions messages/4.1.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# MarkdownTOC - 4.1.0

## Changes

- Add Excluded heading feature Ref: [#140](https://github.com/naokazuterada/MarkdownTOC/issues/140), [#149](https://github.com/naokazuterada/MarkdownTOC/pull/149)
- Update "Coding Style" in CONTRIBUTING.md
61 changes: 61 additions & 0 deletions tests/exclude_heading.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# coding:utf-8
from base import TestBase

class TestExcludeHeading(TestBase):
'''Ignore exclude heading'''

# for debug
# def tearDown(self):
# pass

text_1 = '''
<!-- MarkdownTOC -->
<!-- /MarkdownTOC -->
# heading 1
<!-- MarkdownTOC:excluded -->
# heading 2
# heading 3
'''

def test_exclude_heading(self):
'''Default is no limit'''
toc = self.init_update(self.text_1)['toc']
self.assert_In('- heading 1', toc)
self.assert_NotIn('- heading 2', toc)
self.assert_In('- heading 3', toc)

text_2 = '''
<!-- MarkdownTOC -->
<!-- /MarkdownTOC -->
# level 1
<!-- MarkdownTOC:excluded -->
## level 2
### level 3
<!-- MarkdownTOC:excluded -->
#### level 4
<!-- MarkdownTOC:excluded -->
##### level 5
###### level 6
'''

def test_exclude_heading_level(self):
'''Existence and level is correct'''
toc = self.init_update(self.text_2)['toc']
# existence
self.assert_NotIn('- level 2', toc)
self.assert_NotIn('- level 4', toc)
self.assert_NotIn('- level 5', toc)
# level
self.assert_In('''- level 1
- level 3
- level 6''', toc)
2 changes: 1 addition & 1 deletion tests/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ def test_levels_specific_levels(self):
self.assert_NotIn('- heading 1', toc)
self.assert_In('- heading 2', toc)
self.assert_NotIn('- heading 5', toc)
self.assert_In('- heading 6', toc)
self.assert_In('- heading 6', toc)

0 comments on commit 2b4f9f1

Please sign in to comment.