-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow all entities from HTML5 spec. #76
Open
nandhp
wants to merge
6
commits into
reddit:master
Choose a base branch
from
nandhp:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b2c856
Allow all entities from HTML5 spec.
nandhp a0ca0dc
Convert HTML5 entities to numeric entities
nandhp 202c421
Store gperf input in a file for easier analysis.
nandhp 40b1273
Easy fixes in response to feedback
nandhp 62604d9
Separate numeric-translated entity output from validation.
nandhp ff7d99c
is_allowed_named_entity -> resolve_named_entity
nandhp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
build/ | ||
dist/ | ||
snudown.egg-info/ | ||
src/html_entities.gperf.generated | ||
src/html_entities.h | ||
*.pyc | ||
*.so | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from distutils.spawn import find_executable | ||
from distutils.dep_util import newer_group | ||
from setuptools import setup, Extension | ||
from setuptools.command.build_ext import build_ext | ||
|
||
import re | ||
import os | ||
import subprocess | ||
import fnmatch | ||
import json | ||
|
||
def c_files_in(directory): | ||
paths = [] | ||
|
@@ -14,11 +16,55 @@ def c_files_in(directory): | |
paths.append(os.path.join(directory, f)) | ||
return paths | ||
|
||
def send_html_entities(entities_file, outfh, seen_entities): | ||
# Convert entity list from HTML5 spec JSON and send it to gperf | ||
with open(entities_file) as entitiesfh: | ||
for entity, entityinfo in sorted(json.load(entitiesfh).items()): | ||
if not entity.endswith(';'): | ||
continue | ||
if entity in seen_entities: | ||
continue | ||
seen_entities.add(entity) | ||
# Some sanity checks on the codepoints | ||
assert len(entityinfo['codepoints']) <= 2 | ||
for bad_range in [xrange(0, 8), xrange(11, 12), xrange(14, 31), | ||
xrange(55296, 57343), xrange(65534, 65535)]: | ||
for codepoint in entityinfo['codepoints']: | ||
assert codepoint not in bad_range | ||
representation = ",".join(str(x) for x in entityinfo['codepoints']) | ||
# Output the entity | ||
outfh.write(entity + ", {" + representation + "}\n") | ||
|
||
def process_gperf_file(gperf_file, output_file): | ||
|
||
def process_gperf_file(gperf_file, entities_file, output_file, force=False): | ||
if not find_executable("gperf"): | ||
raise Exception("Couldn't find `gperf`, is it installed?") | ||
subprocess.check_call(["gperf", gperf_file, "--output-file=%s" % output_file]) | ||
|
||
# Do not rerun gperf if no change to input files | ||
if not force and not newer_group((gperf_file, entities_file), output_file): | ||
return | ||
|
||
# Output combined gperf input file | ||
gperf_temp_file = gperf_file + ".generated" | ||
seen_entities = set() | ||
found_separator = 0 | ||
with open(gperf_temp_file, 'w') as outfh: | ||
with open(gperf_file) as f: | ||
for line in f: | ||
entity = line.strip() | ||
if entity == '%%': | ||
found_separator += 1 | ||
if found_separator == 2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: A comment explaning which number maps to which section would be helpful for people who don't know gperf well |
||
send_html_entities(entities_file, outfh, seen_entities) | ||
elif found_separator == 1: | ||
entity = entity.split()[0] | ||
seen_entities.add(entity) | ||
outfh.write(line) | ||
if found_separator < 2: | ||
send_html_entities(entities_file, outfh, seen_entities) | ||
|
||
subprocess.check_call(["gperf", gperf_temp_file, | ||
"--output-file", output_file]) | ||
|
||
version = None | ||
version_re = re.compile(r'^#define\s+SNUDOWN_VERSION\s+"([^"]+)"$') | ||
|
@@ -32,7 +78,8 @@ def process_gperf_file(gperf_file, output_file): | |
|
||
class GPerfingBuildExt(build_ext): | ||
def run(self): | ||
process_gperf_file("src/html_entities.gperf", "src/html_entities.h") | ||
process_gperf_file("src/html_entities.gperf", "src/html_entities.json", | ||
"src/html_entities.h", force=self.force) | ||
build_ext.run(self) | ||
|
||
setup( | ||
|
@@ -47,6 +94,7 @@ def run(self): | |
Extension( | ||
name='snudown', | ||
sources=['snudown.c'] + c_files_in('src/') + c_files_in('html/'), | ||
depends=['src/html_entities.h'], | ||
include_dirs=['src', 'html'] | ||
) | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one's my bad, when I wrote the comment with the
xrange
s I forgot the upper bound onxrange
was exclusive. All those upper bounds should be increased by one.