Skip to content

Commit 0d4c848

Browse files
authored
Merge pull request #15 from alerque/python
2 parents 666318f + d02f28c commit 0d4c848

File tree

2 files changed

+22
-49
lines changed

2 files changed

+22
-49
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,

src/python.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use pyo3::prelude::*;
2+
use pyo3::types::PyDict;
23
use pyo3::wrap_pyfunction;
34

45
#[pyfunction]
@@ -7,12 +8,19 @@ fn to_html(_py: Python, buffer: String) -> PyResult<String> {
78
}
89

910
#[pyfunction]
10-
fn get_offsets(_py: Python, buffer: String) -> PyResult<()> {
11+
fn get_offsets(_py: Python, buffer: String) -> PyResult<&PyDict> {
1112
let events = super::get_offsets(buffer).unwrap();
13+
let pyevents = PyDict::new(_py);
14+
let mut i: u32 = 1;
1215
for event in events.iter() {
13-
eprintln!("DEBUG={:#?}", event);
16+
let event_dict = PyDict::new(_py);
17+
event_dict.set_item("group", event.group.as_str()).unwrap();
18+
event_dict.set_item("start", event.first).unwrap();
19+
event_dict.set_item("end", event.last).unwrap();
20+
pyevents.set_item(i, event_dict)?;
21+
i += 1;
1422
}
15-
Ok(())
23+
Ok(pyevents)
1624
}
1725

1826
#[pymodule]

0 commit comments

Comments
 (0)