Skip to content

Commit

Permalink
Chore/pc4 (#89)
Browse files Browse the repository at this point in the history
* Add support for PC4 (changes needed to support latest mdpopups)

* Reduce dependencies

* Bump version

* spelling
  • Loading branch information
facelessuser authored Sep 5, 2021
1 parent 39733e1 commit 56763e9
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 49 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ExportHtml

## 2.17.3

- **FIX**: Fix a scheme loading related issue.
- **FIX**: Ensure ExportHTML will work with Package Control 4.0 (compatibility issues with latest `mdpopups` needed for
Package Control 4.0).

## 2.17.2

- **FIX**: Fix a regression where custom defined schemes are not used.
Expand Down
2 changes: 1 addition & 1 deletion ExportHtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def setup(self, **kwargs):
self.switch = False
self.save_to_view = False
self.view_scheme = view_scheme
if alt_scheme != view_scheme:
if isinstance(alt_scheme, str) and alt_scheme != view_scheme:
switch = True
if view_scheme != default.get('color_scheme'):
self.save_to_view = True
Expand Down
8 changes: 1 addition & 7 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
"python-jinja2"
],
">=3124": [
"pygments",
"python-markdown",
"mdpopups",
"python-jinja2",
"markupsafe",
"pymdownx",
"pyyaml"
"mdpopups"
]
}
}
2 changes: 1 addition & 1 deletion docs/src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mkdocs_pymdownx_material_extras>=1.2.2
mkdocs_pymdownx_material_extras>=1.4
mkdocs-git-revision-date-localized-plugin
mkdocs-minify-plugin
pyspelling
6 changes: 3 additions & 3 deletions lib/color_scheme_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def process_color_gradient(self, colors, simple_strip=False, bground=None):
if not simple_strip:
if bground is None:
bground = self.special_colors['background']['color_simulated']
rgb.overlay(Color(bground if bground != "" else "#FFFFFF"))
rgb.compose(Color(bground if bground != "" else "#FFFFFF"))

gradient.append((color, rgb.to_string(**HEX)))
if gradient:
Expand Down Expand Up @@ -446,7 +446,7 @@ def process_color(self, color, simple_strip=False, bground=None):
if not simple_strip:
if bground is None:
bground = self.special_colors['background']['color_simulated']
rgb.overlay(Color(bground if bground != "" else "#FFFFFF"))
rgb.compose(Color(bground if bground != "" else "#FFFFFF"))

return color, rgb.to_string(**HEX)

Expand Down Expand Up @@ -481,7 +481,7 @@ def guess_color(self, scope_key, selected=False, explicit_background=False, no_b
This was originally introduced for mdpopups so that it would
know when a background was not needed. This allowed mdpopups
to generate syntax highlighted code that could be overlayed on
to generate syntax highlighted code that could be overlaid on
block elements with different background colors and allow that
background would show through.
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/color_scheme_tweaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def _filter_colors(self, *args, **kwargs):
(bg is None or bg.strip() == "" or bg == "none")
):
rgba = Color(rgba_fg)
rgba.overlay(self.bground if self.bground != "" else "#FFFFFF", in_place=True)
rgba.compose(self.bground if self.bground != "" else "#FFFFFF", in_place=True)
bg = rgba.to_string(hex=True, alpha=False) + ("%02X" % int((255.0 * value)))
try:
rgba_bg = Color(bg)
Expand Down Expand Up @@ -431,7 +431,7 @@ def _filter_colors(self, *args, **kwargs):
(bg is None or bg.strip() == "" or bg == "none")
):
rgba = Color(rgba_fg)
rgba.overlay(self.bground if self.bground != "" else "#FFFFFF", in_place=True)
rgba.compose(self.bground if self.bground != "" else "#FFFFFF", in_place=True)
bg = rgba.to_string(hex=True, alpha=False) + ("%02X" % int((255.0 * value)))
try:
rgba_bg = Color(bg)
Expand Down
57 changes: 32 additions & 25 deletions lib/st_colormod.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Color-mod."""
import re
from mdpopups.coloraide.css import Color as ColorCSS
from mdpopups.coloraide.colors import ColorMatch
from mdpopups.coloraide.colors import _parse as parse
from mdpopups.coloraide import Color as ColorCSS
from mdpopups.coloraide import ColorMatch
from mdpopups.coloraide.spaces import _parse
from mdpopups.coloraide import util
import functools
import math
Expand All @@ -21,30 +21,32 @@
\#(?:{hex}{{6}}(?:{hex}{{2}})?|{hex}{{3}}(?:{hex})?) |
[\w][\w\d]*
)
""".format(**parse.COLOR_PARTS)
""".format(**_parse.COLOR_PARTS)
),
"functions": re.compile(r'(?i)[\w][\w\d]*\('),
"separators": re.compile(r'(?:{comma}|{space}|{slash})'.format(**parse.COLOR_PARTS))
"separators": re.compile(r'(?:{comma}|{space}|{slash})'.format(**_parse.COLOR_PARTS))
}

RE_ADJUSTERS = {
"alpha": re.compile(
r'(?i)\s+a(?:lpha)?\(\s*(?:(\+\s+|\-\s+)?({percent}|{float})|(\*)?\s*({percent}|{float}))\s*\)'.format(
**parse.COLOR_PARTS
**_parse.COLOR_PARTS
)
),
"saturation": re.compile(r'(?i)\s+s(?:aturation)?\((\+\s|\-\s|\*)?\s*({percent})\s*\)'.format(**parse.COLOR_PARTS)),
"lightness": re.compile(r'(?i)\s+l(?:ightness)?\((\+\s|\-\s|\*)?\s*({percent})\s*\)'.format(**parse.COLOR_PARTS)),
"saturation": re.compile(
r'(?i)\s+s(?:aturation)?\((\+\s|\-\s|\*)?\s*({percent})\s*\)'.format(**_parse.COLOR_PARTS)
),
"lightness": re.compile(r'(?i)\s+l(?:ightness)?\((\+\s|\-\s|\*)?\s*({percent})\s*\)'.format(**_parse.COLOR_PARTS)),
"min-contrast_start": re.compile(r'(?i)\s+min-contrast\(\s*'),
"blend_start": re.compile(r'(?i)\s+blenda?\(\s*'),
"end": re.compile(r'(?i)\s*\)')
}

RE_HUE = re.compile(r'(?i){angle}'.format(**parse.COLOR_PARTS))
RE_HUE = re.compile(r'(?i){angle}'.format(**_parse.COLOR_PARTS))
RE_COLOR_START = re.compile(r'(?i)color\(\s*')
RE_BLEND_END = re.compile(r'(?i)\s+({percent})(?:\s+(rgb|hsl|hwb))?\s*\)'.format(**parse.COLOR_PARTS))
RE_BLEND_END = re.compile(r'(?i)\s+({percent})(?:\s+(rgb|hsl|hwb))?\s*\)'.format(**_parse.COLOR_PARTS))
RE_BRACKETS = re.compile(r'(?:(\()|(\))|[^()]+)')
RE_MIN_CONTRAST_END = re.compile(r'(?i)\s+({float})\s*\)'.format(**parse.COLOR_PARTS))
RE_MIN_CONTRAST_END = re.compile(r'(?i)\s+({float})\s*\)'.format(**_parse.COLOR_PARTS))
RE_VARS = re.compile(r'(?i)(?:(?<=^)|(?<=[\s\t\(,/]))(var\(\s*([-\w][-\w\d]*)\s*\))(?!\()(?=[\s\t\),/]|$)')


Expand Down Expand Up @@ -209,7 +211,7 @@ def _adjust(self, string, start=0):
start = m.end(0)
m = RE_HUE.match(string, start)
if m:
hue = parse.norm_hue_channel(m.group(0))
hue = _parse.norm_angle(m.group(0))
color = Color("hsl", [hue, 1, 0.5]).convert("srgb")
start = m.end(0)
if color is None:
Expand All @@ -233,12 +235,13 @@ def _adjust(self, string, start=0):

if color is not None:
self._color = color
if not self._color.in_gamut():
self._color.fit(method="clip", in_place=True)
self._color.fit(method="clip", in_place=True)

while not done:
m = None
for name, pattern in RE_ADJUSTERS.items():
name = None
for key, pattern in RE_ADJUSTERS.items():
name = key
m = pattern.match(string, start)
if m:
start = m.end(0)
Expand All @@ -260,8 +263,7 @@ def _adjust(self, string, start=0):
else:
break

if not self._color.in_gamut():
self._color.fit(method="clip", in_place=True)
self._color.fit(method="clip", in_place=True)
else:
raise ValueError('Could not calculate base color')
except Exception:
Expand Down Expand Up @@ -306,7 +308,7 @@ def process_alpha(self, m, hue):
else:
value = m.group(4)
if value.endswith('%'):
value = float(value.strip('%')) * parse.SCALE_PERCENT
value = float(value.strip('%')) * _parse.SCALE_PERCENT
else:
value = float(value)
op = ""
Expand Down Expand Up @@ -348,7 +350,7 @@ def process_blend(self, m, string, hue):
raise ValueError("Could not find a valid color for 'blend'")
m = RE_BLEND_END.match(string, start)
if m:
value = float(m.group(1).strip('%')) * parse.SCALE_PERCENT
value = float(m.group(1).strip('%')) * _parse.SCALE_PERCENT
space = "srgb"
if m.group(2):
space = m.group(2).lower()
Expand Down Expand Up @@ -539,7 +541,7 @@ def _parse(self, color, data=None, alpha=util.DEF_ALPHA, filters=None, variables
return obj
elif isinstance(color, ColorCSS):
if not filters or color.space() in filters:
obj = self.CS_MAP[color.space()](color._color)
obj = self.CS_MAP[color.space()](color._space)
else:
m = self._match(color, fullmatch=True, filters=filters, variables=variables)
if m is None:
Expand Down Expand Up @@ -581,7 +583,7 @@ def _match(cls, string, start=0, fullmatch=False, filters=None, variables=None):
string = handle_vars(string, variables)
obj, match_end = ColorMod(fullmatch).adjust(string, start)
if obj is not None:
return ColorMatch(obj._color, start, end if end is not None else match_end)
return ColorMatch(obj._space, start, end if end is not None else match_end)
else:
filters = set(filters) if filters is not None else set()
obj = None
Expand All @@ -605,17 +607,22 @@ def match(cls, string, start=0, fullmatch=False, *, filters=None, variables=None
obj.color = cls(obj.color.space(), obj.color.coords(), obj.color.alpha)
return obj

@classmethod
def new(cls, color, data=None, alpha=util.DEF_ALPHA, *, filters=None, variables=None, **kwargs):
def new(self, color, data=None, alpha=util.DEF_ALPHA, *, filters=None, variables=None, **kwargs):
"""Create new color object."""

return cls(color, data, alpha, filters=filters, variables=variables, **kwargs)
return type(self)(color, data, alpha, filters=filters, variables=variables, **kwargs)

def update(self, color, data=None, alpha=util.DEF_ALPHA, *, filters=None, variables=None, **kwargs):
"""Update the existing color space with the provided color."""

clone = self.clone()
obj = self._parse(color, data, alpha, filters=filters, variables=variables, **kwargs)
self._color.update(obj)
clone._attach(obj)

if clone.space() != self.space():
clone.convert(self.space(), in_place=True)

self._attach(clone._space)
return self

def mutate(self, color, data=None, alpha=util.DEF_ALPHA, *, filters=None, variables=None, **kwargs):
Expand Down
16 changes: 9 additions & 7 deletions lib/tmtheme.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Custom color that looks for colors of format `#RRGGBBAA` as `#AARRGGBB`."""
from mdpopups.coloraide.css.colors import Color, SRGB
from mdpopups.coloraide.colors import _parse as parse
from mdpopups.coloraide import Color
from mdpopups.coloraide.spaces.srgb.css import SRGB
from mdpopups.coloraide.spaces import _parse
from mdpopups.coloraide import util
import copy
import re

RE_COMPRESS = re.compile(r'(?i)^#({hex})\1({hex})\2({hex})\3(?:({hex})\4)?$'.format(**parse.COLOR_PARTS))
RE_COMPRESS = re.compile(r'(?i)^#({hex})\1({hex})\2({hex})\3(?:({hex})\4)?$'.format(**_parse.COLOR_PARTS))

name2hex_map = {
"black": "#000000",
Expand Down Expand Up @@ -691,11 +692,11 @@ class SRGBX11(SRGB):
# Names
\b(?<!\#)[a-z]{{3,}}(?!\()\b
)
""".format(**parse.COLOR_PARTS)
""".format(**_parse.COLOR_PARTS)
)

def to_string(
self, *, alpha=None, precision=None, fit=True, **kwargs
self, parent, *, alpha=None, precision=None, fit=True, **kwargs
):
"""Convert to CSS."""

Expand All @@ -706,7 +707,8 @@ def to_string(
alpha = alpha is not False and (alpha is True or a < 1.0)
hex_upper = options.get("upper", False)
compress = options.get("compress", False)
coords = util.no_nan(self.fit_coords())
method = None if not isinstance(fit, str) else fit
coords = util.no_nan(parent.fit(method=method).coords())

template = "#{:02x}{:02x}{:02x}{:02x}" if alpha else "#{:02x}{:02x}{:02x}"
if hex_upper:
Expand Down Expand Up @@ -748,7 +750,7 @@ def translate_channel(cls, channel, value):
"""Translate channel string."""

if channel in (-1, 0, 1, 2):
return parse.norm_hex_channel(value)
return _parse.norm_hex_channel(value)

@classmethod
def split_channels(cls, color):
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ theme:
code: Roboto Mono
features:
- navigation.tabs
- navigation.top

nav:
- Home:
Expand Down
2 changes: 1 addition & 1 deletion support.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import webbrowser
import re

__version__ = "2.17.2"
__version__ = "2.17.3"
__pc_name__ = 'ExportHtml'

CSS = '''
Expand Down
2 changes: 1 addition & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _get_json_files(self, pattern, folder='.'):
for root, dirnames, filenames in os.walk(folder):
for filename in fnmatch.filter(filenames, pattern):
yield os.path.join(root, filename)
dirnames = [d for d in dirnames if d not in ('.svn', '.git', '.tox')]
dirnames[:] = [d for d in dirnames if d not in ('.svn', '.git', '.tox')]

def test_json_settings(self):
"""Test each JSON file."""
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ commands=
flake8 '{toxinidir}'

[flake8]
ignore=D202,D203,D401,W504,E741
ignore=D202,D203,D401,W504,E741,N818
max-line-length=120
exclude=site/*.py,.tox/*

0 comments on commit 56763e9

Please sign in to comment.