Skip to content

Commit

Permalink
Command to convert SETEXT headers to ATX
Browse files Browse the repository at this point in the history
  • Loading branch information
bordaigorl committed Mar 13, 2014
1 parent a63a149 commit 48a5a30
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"caption": "MarkdownEditing: Fix Underlined Headers",
"command": "fix_all_underlined_headers"
},
{
"caption": "MarkdownEditing: Convert Underlined Headers to ATX",
"command": "convert_to_atx"
},
{
"caption": "MarkdownEditing: Add Missing Link Labels",
"command": "gather_missing_link_markers"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Strikethrough is supported:

Adjusts every setext-style header to add or remove `=` or `-` characters as needed to match the lengths of their header text.

### Convert Underlined Headers to ATX

Converts every setext-style header into an ATX style header. If something is selected only the headers in the selections will be converted, otherwise the conversion will be applied to the whole view.

### Add Missing Link Labels

Scans document for referenced link usages (`[some link][some_ref]` and `[some link][]`) and checks if they are all defined. If there are undefined link references, command will automatically create their definition snippet at the bottom of the file.
Expand Down
4 changes: 4 additions & 0 deletions messages/2.0.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ Your _MarkdownEditing_ plugin is updated. Enjoy new version. For any type of fee

* Fenced code blocks now supports Lisp. Fixes #156

## New Features

* Added command to convert underlined (SETEXT) headers to hashed (ATX) headers

[issues]: https://github.com/SublimeText-Markdown/MarkdownEditing/issues
29 changes: 29 additions & 0 deletions underlined_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
$ # Must fill the while line. Don't match "- list items"
''', re.X )

SETEXT_HEADER_RE = re.compile( r'''
^(.+)\n
( =+ | -+ ) # A run of ---- or ==== underline characters.
[ \t]* # Optional trailing whitespace.
$ # Must fill the while line. Don't match "- list items"
''', re.X | re.M )

def fix_dashes(view, edit, text_region, dash_region):
"""Replaces the underlined "dash" region of a setext header with a run of
dashes or equal-signs that match the length of the header text."""
Expand Down Expand Up @@ -105,3 +112,25 @@ def run(self, edit):

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

class ConvertToAtxCommand(sublime_plugin.TextCommand):

def run(self, edit, closed=False):
regions = list(self.view.sel())
if len(regions) == 1 and regions[0].size() == 0:
regions = [sublime.Region(0, self.view.size())]
regions.reverse()
for region in regions:
txt = self.view.substr(region)
matches = list(SETEXT_HEADER_RE.finditer(txt))
matches.reverse()
for m in matches:
mreg = sublime.Region(region.begin()+m.start(), region.begin()+m.end())
atx = "# "
if '-' in m.group(2):
atx = "#" + atx
closing = atx[::-1] if closed else ""
self.view.replace(edit, mreg, atx + m.group(1) + closing)

def is_enabled(self):
return bool(self.view.score_selector(self.view.sel()[0].a, "text.html.markdown"))

0 comments on commit 48a5a30

Please sign in to comment.