summaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
authorryanss2015-01-19 23:59:00 -0500
committerryanss2015-01-19 23:59:00 -0500
commit44349f72a3546e61b5669ac368512302c5205bec (patch)
treed5bcf95767156d0b9a14b871ed412ad565b95e4c /plugin
parent9b52648a347427cfb9b27232a12634a073865458 (diff)
downloadvim-hn-44349f72a3546e61b5669ac368512302c5205bec.tar.gz
Add syntax hightlighting to code blocks
Diffstat (limited to 'plugin')
-rw-r--r--plugin/hackernews.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/plugin/hackernews.py b/plugin/hackernews.py
index 76870f5..e46d2a9 100644
--- a/plugin/hackernews.py
+++ b/plugin/hackernews.py
@@ -10,12 +10,16 @@ API_URL = "http://node-hnapi.herokuapp.com"
def bwrite(s):
- s = s.encode('utf-8')
b = vim.current.buffer
+ # Never write more than two blank lines in a row
if not s.strip() and not b[-1].strip() and not b[-2].strip():
- # Never write more than two blank lines in a row
return
- elif not b[0]:
+ # Code block markers for syntax highlighting
+ if s and s[-1] == unichr(160):
+ b[-1] = s
+ return
+ s = s.encode('utf-8')
+ if not b[0]:
b[0] = s
else:
b.append(s)
@@ -106,6 +110,7 @@ def print_comments(comments):
continue
p = html.unescape(p)
+ # Extract code block before textwrap to conserve whitespace
code = None
if p.find("<code>") >= 0:
m = re.search("<pre><code>([\S\s]*?)</code></pre>\n", p)
@@ -132,8 +137,10 @@ def print_comments(comments):
subsequent_indent=" "*4*level)
for line in contents:
if line.find("!CODE!") >= 0:
+ bwrite(" "*4*level + unichr(160))
for c in code.split("\n"):
bwrite(" "*4*level + c)
+ bwrite(" "*4*level + unichr(160))
line = " "*4*level + line.replace("!CODE!", "").strip()
if line.strip():
bwrite(line)