Skip to content

Commit

Permalink
Latest changes and fixes for libraries
Browse files Browse the repository at this point in the history
- superfences, inlinehilite, and betterem fixes/changes
- Update rgba
- Sublime highlighter needs to use 'text' if  `not` lang instead of just
lang is `None`
  • Loading branch information
facelessuser committed Dec 8, 2015
1 parent 35e533d commit 0aaac6d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 50 deletions.
15 changes: 4 additions & 11 deletions st3/mdpopups/mdx/betterem.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
# Smart rules for when "smart underscore" is enabled
# SMART: ___strong,em___
SMART_UNDER_STRONG_EM = r'(?<!\w)(_{3})(?![\s_])%s(?<!\s)\2(?!\w)' % SMART_UNDER_CONTENT
# ___strong,em_strong__
# ___strong,em_ strong__
SMART_UNDER_STRONG_EM2 = \
r'(?<!\w)(_{3})(?![\s_])%s(?<!\s)_(?!\w)%s(?<!\s)_{2}(?!\w)' % (SMART_UNDER_MIXED_CONTENT, SMART_UNDER_CONTENT)
# ___em,strong__em_
# ___em,strong__ em_
SMART_UNDER_EM_STRONG = \
r'(?<!\w)(_{3})(?![\s_])%s(?<!\s)_{2}(?!\w)%s(?<!\s)_(?!\w)' % (SMART_UNDER_MIXED_CONTENT, SMART_UNDER_CONTENT)
# __strong__
Expand All @@ -74,12 +74,12 @@
# Smart rules for when "smart asterisk" is enabled
# SMART: ***strong,em***
SMART_STAR_STRONG_EM = r'(?:(?<=_)|(?<![\w\*]))(\*{3})(?![\s\*])%s(?<!\s)\2(?:(?=_)|(?![\w\*]))' % SMART_STAR_CONTENT
# ***strong,em*strong**
# ***strong,em* strong**
SMART_STAR_STRONG_EM2 = \
r'(?:(?<=_)|(?<![\w\*]))(\*{3})(?![\s\*])%s(?<!\s)\*(?:(?=_)|(?![\w\*]))%s(?<!\s)\*{2}(?:(?=_)|(?![\w\*]))' % (
SMART_STAR_MIXED_CONTENT, SMART_STAR_CONTENT
)
# ***em,strong**em*
# ***em,strong** em*
SMART_STAR_EM_STRONG = \
r'(?:(?<=_)|(?<![\w\*]))(\*{3})(?![\s\*])%s(?<!\s)\*{2}(?:(?=_)|(?![\w\*]))%s(?<!\s)\*(?:(?=_)|(?![\w\*]))' % (
SMART_STAR_MIXED_CONTENT, SMART_STAR_CONTENT
Expand All @@ -89,10 +89,6 @@
# SMART *em*
SMART_STAR_EM = r'(?:(?<=_)|(?<![\w\*]))(\*)(?![\s\*])%s(?<!\s)\2(?:(?=_)|(?![\w\*]))' % SMART_STAR_CONTENT

smart_enable_keys = [
"all", "asterisk", "underscore", "none"
]


class BetterEmExtension(Extension):
"""Add extension to Markdown class."""
Expand All @@ -104,9 +100,6 @@ def __init__(self, *args, **kwargs):
'smart_enable': ["underscore", "Treat connected words intelligently - Default: underscore"]
}

if "smart_enable" in kwargs and kwargs["smart_enable"] not in smart_enable_keys:
del kwargs["smart_enable"]

super(BetterEmExtension, self).__init__(*args, **kwargs)

def extendMarkdown(self, md, md_globals):
Expand Down
13 changes: 6 additions & 7 deletions st3/mdpopups/mdx/inlinehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
from __future__ import unicode_literals
from markdown import Extension
from markdown.inlinepatterns import Pattern
try:
from markdown.extensions import codehilite
except:
codehilite = None
from markdown.extensions import codehilite
# import traceback
try:
from pygments import highlight
Expand Down Expand Up @@ -155,7 +152,7 @@ def get_settings(self):
self.use_pygments = self.config['use_pygments']
self.use_codehilite_settings = self.config['use_codehilite_settings']
self.style_plain_text = self.config['style_plain_text']
if codehilite and self.use_codehilite_settings:
if self.use_codehilite_settings:
for ext in self.markdown.registeredExtensions:
if isinstance(ext, codehilite.CodeHiliteExtension):
self.guess_lang = ext.config['guess_lang'][0]
Expand All @@ -169,7 +166,9 @@ def get_settings(self):
def codehilite(self, lang, src):
"""Syntax highlite the inline code block."""

process_text = self.style_plain_text or lang != 'text'
process_text = self.style_plain_text or lang or self.guess_lang
if not lang and self.style_plain_text and not self.guess_lang:
lang = 'text'
sublime_hl_enabled, sublime_hl = self.config.get("sublime_hl", None)
if sublime_hl_enabled:
code = sublime_hl.syntax_highlight(src, lang, inline=True)
Expand Down Expand Up @@ -213,7 +212,7 @@ def codehilite(self, lang, src):
def handleMatch(self, m):
"""Handle the pattern match."""

lang = m.group('lang') if m.group('lang') else 'text'
lang = m.group('lang') if m.group('lang') else ''
src = m.group('code').strip()
self.get_settings()
return self.codehilite(lang, src)
Expand Down
20 changes: 8 additions & 12 deletions st3/mdpopups/mdx/superfences.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
from markdown.blockprocessors import CodeBlockProcessor
try:
from markdown.extensions.codehilite import CodeHilite, CodeHiliteExtension, parse_hl_lines
except Exception:
CodeHilite = None
from markdown.extensions.codehilite import CodeHilite, CodeHiliteExtension, parse_hl_lines
try:
from pygments import highlight
from pygments.lexers import get_lexer_by_name, guess_lexer
Expand Down Expand Up @@ -281,12 +278,12 @@ def extendMarkdown(self, md, md_globals):
if not hasattr(md, "superfences"):
md.superfences = []
md.superfences.insert(0, sf_entry)
if config.get("uml_flow", False): # Disable for Sublime Text
if config.get("uml_flow", True):
extend_super_fences(
md, "flow", "flow",
lambda s, l, c="uml-flowchart": uml_format(s, l, c)
)
if config.get("uml_sequence", False): # Disable for Sublime Text
if config.get("uml_sequence", True):
extend_super_fences(
md, "sequence", "sequence",
lambda s, l, c="uml-sequence-diagram": uml_format(s, l, c)
Expand Down Expand Up @@ -358,11 +355,10 @@ def check_codehilite(self):
"""Check for code hilite extension to get its config."""

if not self.checked_for_codehilite:
if CodeHilite:
for ext in self.markdown.registeredExtensions:
if isinstance(ext, CodeHiliteExtension):
self.codehilite_conf = ext.config
break
for ext in self.markdown.registeredExtensions:
if isinstance(ext, CodeHiliteExtension):
self.codehilite_conf = ext.config
break
self.checked_for_codehilite = True

def clear(self):
Expand All @@ -389,7 +385,7 @@ def eval(self, m, start, end):
elif len(m.group(1)) != self.ws_len and m.group(2) != '':
# Not indented enough
self.clear()
elif self.fence_end.match(m.group(0)) is not None:
elif self.fence_end.match(m.group(0)) is not None and not m.group(2).startswith(' '):
# End of fence
self.process_nested_block(m, start, end)
else:
Expand Down
71 changes: 52 additions & 19 deletions st3/mdpopups/rgba.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import re
from colorsys import rgb_to_hls, hls_to_rgb, rgb_to_hsv, hsv_to_rgb
import decimal

RGB_CHANNEL_SCALE = 1.0 / 255.0
HUE_SCALE = 1.0 / 360.0
Expand All @@ -17,6 +18,12 @@ def clamp(value, mn, mx):
return max(min(value, mx), mn)


def round_int(dec):
"""Round float to nearest int using expected rounding."""

return int(decimal.Decimal(dec).quantize(decimal.Decimal('0'), decimal.ROUND_HALF_UP))


class RGBA(object):
"""RGBA object for converting between color formats or applying filters to the color."""

Expand Down Expand Up @@ -68,7 +75,7 @@ def apply_alpha(self, background="#000000FF"):
def tx_alpha(cf, af, cb, ab):
"""Translate the color channel with the alpha channel and background channel color."""

return int(
return round_int(
abs(
cf * (af * RGB_CHANNEL_SCALE) + cb * (ab * RGB_CHANNEL_SCALE) * (1 - (af * RGB_CHANNEL_SCALE))
)
Expand All @@ -86,33 +93,33 @@ def tx_alpha(cf, af, cb, ab):
def get_luminance(self):
"""Get percieved luminance."""

return clamp(int(round(0.299 * self.r + 0.587 * self.g + 0.114 * self.b)), 0, 255)
return clamp(round_int(0.299 * self.r + 0.587 * self.g + 0.114 * self.b), 0, 255)

def get_true_luminance(self):
""""Get true liminance."""

l = self.tohls()[1]
return clamp(int(l * 255.0), 0, 255)
return clamp(round_int(l * 255.0), 0, 255)

def alpha(self, factor):
"""Adjust alpha."""

self.a = int(clamp(self.a + (255.0 * factor) - 255.0, 0.0, 255.0))
self.a = round_int(clamp(self.a + (255.0 * factor) - 255.0, 0.0, 255.0))

def red(self, factor):
"""Adjust red."""

self.r = int(clamp(self.r + (255.0 * factor) - 255.0, 0.0, 255.0))
self.r = round_int(clamp(self.r + (255.0 * factor) - 255.0, 0.0, 255.0))

def green(self, factor):
"""Adjust green."""

self.g = int(clamp(self.g + (255.0 * factor) - 255.0, 0.0, 255.0))
self.g = round_int(clamp(self.g + (255.0 * factor) - 255.0, 0.0, 255.0))

def blue(self, factor):
"""Adjust blue."""

self.b = int(clamp(self.b + (255.0 * factor) - 255.0, 0.0, 255.0))
self.b = round_int(clamp(self.b + (255.0 * factor) - 255.0, 0.0, 255.0))

def luminance(self, factor):
"""True luminance."""
Expand All @@ -130,9 +137,9 @@ def fromhsv(self, h, s, v):
"""Convert to RGB from HSV."""

r, g, b = hsv_to_rgb(h, s, v)
self.r = int(round(r * 255)) & 0xFF
self.g = int(round(g * 255)) & 0xFF
self.b = int(round(b * 255)) & 0xFF
self.r = round_int(r * 255.0) & 0xFF
self.g = round_int(g * 255.0) & 0xFF
self.b = round_int(b * 255.0) & 0xFF

def tohls(self):
"""Convert to HLS color format."""
Expand All @@ -143,9 +150,35 @@ def fromhls(self, h, l, s):
"""Convert to RGB from HSL."""

r, g, b = hls_to_rgb(h, l, s)
self.r = int(round(r * 255)) & 0xFF
self.g = int(round(g * 255)) & 0xFF
self.b = int(round(b * 255)) & 0xFF
self.r = round_int(r * 255.0) & 0xFF
self.g = round_int(g * 255.0) & 0xFF
self.b = round_int(b * 255.0) & 0xFF

def tohwb(self):
"""Convert to HWB from RGB."""

h, s, v = self.tohsv()
w = (1.0 - s) * v
b = 1.0 - v
return h, w, b

def fromhwb(self, h, w, b):
"""Convert to RGB from HWB."""

# Normalize white and black
# w + b <= 1.0
if w + b > 1.0:
norm_factor = 1.0 / (w + b)
w *= norm_factor
b *= norm_factor

# Convert to HSV and then to RGB
s = 1.0 - (w / (1.0 - b))
v = 1.0 - b
r, g, b = hsv_to_rgb(h, s, v)
self.r = round_int(r * 255.0) & 0xFF
self.g = round_int(g * 255.0) & 0xFF
self.b = round_int(b * 255.0) & 0xFF

def colorize(self, deg):
"""Colorize the color with the given hue."""
Expand Down Expand Up @@ -191,9 +224,9 @@ def grayscale(self):
def sepia(self):
"""Apply a sepia filter to the color."""

r = clamp(int((self.r * .393) + (self.g * .769) + (self.b * .189)), 0, 255) & 0xFF
g = clamp(int((self.r * .349) + (self.g * .686) + (self.b * .168)), 0, 255) & 0xFF
b = clamp(int((self.r * .272) + (self.g * .534) + (self.b * .131)), 0, 255) & 0xFF
r = clamp(round_int((self.r * .393) + (self.g * .769) + (self.b * .189)), 0, 255) & 0xFF
g = clamp(round_int((self.r * .349) + (self.g * .686) + (self.b * .168)), 0, 255) & 0xFF
b = clamp(round_int((self.r * .272) + (self.g * .534) + (self.b * .131)), 0, 255) & 0xFF
self.r, self.g, self.b = r, g, b

def _get_overage(self, c):
Expand Down Expand Up @@ -259,6 +292,6 @@ def brightness(self, factor):
components = list(self._distribute_overage(components, overage, slots))
count += 1

self.r = clamp(int(round(components[0])), 0, 255) & 0xFF
self.g = clamp(int(round(components[1])), 0, 255) & 0xFF
self.b = clamp(int(round(components[2])), 0, 255) & 0xFF
self.r = clamp(round_int(components[0]), 0, 255) & 0xFF
self.g = clamp(round_int(components[1]), 0, 255) & 0xFF
self.b = clamp(round_int(components[2]), 0, 255) & 0xFF
2 changes: 1 addition & 1 deletion st3/mdpopups/st_code_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def set_view(self, src, lang):
def syntax_highlight(self, src, lang, hl_lines=[], inline=False):
"""Syntax Highlight."""

self.set_view(src, 'text' if lang is None else lang)
self.set_view(src, 'text' if not lang else lang)
self.inline = inline
self.hl_lines = hl_lines
self.setup()
Expand Down

0 comments on commit 0aaac6d

Please sign in to comment.