Skip to content

Commit d02f28c

Browse files
committed
make the python code up to date
1 parent e0aac83 commit d02f28c

File tree

1 file changed

+11
-46
lines changed

1 file changed

+11
-46
lines changed

python3/commonmark.py

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import sys
22
import os.path
3-
import re
4-
from enum import Enum, auto
53
from collections import namedtuple
64
import pynvim
75
sys.path.append(os.path.dirname(__file__))
@@ -12,40 +10,18 @@ def q(string):
1210
return '"' + string + '"'
1311

1412

15-
Range = namedtuple('Range', ['start', 'end'])
16-
1713
Pos = namedtuple('Pos', ['line', 'col'])
1814

1915
PosPair = namedtuple('PosPair', ['startpos', 'endpos'])
2016

2117

22-
class Tag(Enum):
23-
Paragraph = auto()
24-
Heading = auto()
25-
BlockQuote = auto()
26-
CodeBlock = auto()
27-
List = auto()
28-
Item = auto()
29-
FootnoteDefinition = auto()
30-
Table = auto()
31-
TableHead = auto()
32-
TableRow = auto()
33-
TableCell = auto()
34-
Emphasis = auto()
35-
Strong = auto()
36-
Strikethrough = auto()
37-
Link = auto()
38-
Image = auto()
39-
40-
4118
@pynvim.plugin
4219
class CommonMark(object):
4320
def __init__(self, vim):
4421
self.vim = vim
4522
self.namespace = self.vim.new_highlight_source()
4623
self.vim.command('hi cmarkEmphasis gui=italic')
4724
self.vim.command('hi cmarkStrong gui=bold')
48-
self.vim.command('hi link cmarkHeading Directory')
4925

5026
def echo(self, string):
5127
self.vim.command('echom ' + q(string))
@@ -90,39 +66,28 @@ def to_line_highlights(self, startpos, endpos):
9066
tail = PosPair(Pos(endpos.line, 0), Pos(endpos.line, endpos.col))
9167
return [head, *body, tail]
9268

93-
def build_hl(self, group, lnum, start_col=0, end_col=-1):
94-
return [group, lnum, start_col, end_col]
95-
9669
def highlight(self):
97-
if self.vim.current.buffer.options['filetype'] != 'commonmark':
70+
if self.vim.current.buffer.options['filetype'] != 'pandoc':
9871
return
9972
buf_str = "\n".join(self.vim.current.buffer)
100-
offsets = libpulldowncmark.get_offsets(buf_str)
73+
offsets = libvim_commonmark.get_offsets(buf_str)
10174
hls = []
10275
for i in offsets:
103-
ranges, typ = offsets[i]
104-
105-
# internal vim byte counts start on 1,
106-
# not 0 as in pulldowm-cmark
107-
rng = Range(*map(lambda x: int(x) + 1, ranges.split('..')))
76+
data = offsets[i]
77+
typ = data['group']
10878

10979
# canonical positions of the offsets
110-
startpos = self.offset2pos(rng.start)
111-
endpos = self.offset2pos(rng.end)
80+
startpos = self.offset2pos(data['start'])
81+
endpos = self.offset2pos(data['end'])
11282

11383
# self.echo(self.vim.funcs.fnameescape(type))
114-
if typ in [Tag.Emphasis.name, Tag.Strong.name]:
115-
line_highlights = self.to_line_highlights(startpos, endpos)
116-
for lh in line_highlights:
117-
hls.append(self.build_hl('cmark' + typ,
118-
lh.startpos.line - 1,
119-
lh.startpos.col,
120-
lh.endpos.col))
121-
elif re.match(Tag.Heading.name, typ):
84+
if typ in ('cmarkEmphasis', 'cmarkStrong'):
12285
line_highlights = self.to_line_highlights(startpos, endpos)
12386
for lh in line_highlights:
124-
hls.append(('cmarkHeading',
125-
lh.startpos.line - 1))
87+
hls.append([typ,
88+
lh.startpos.line - 1,
89+
lh.startpos.col,
90+
lh.endpos.col])
12691

12792
if len(hls) > 0:
12893
self.vim.current.buffer.update_highlights(self.namespace,

0 commit comments

Comments
 (0)