Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-strip out comments if some tried to add them to favorite_files.json
-be forgiving of trailing commas in favorite_files.json -autoload favorite_files.json if it has been detetected as modified -Fix issue where canceling opening a file or canceling removing an entry, the event stil occurs
- Loading branch information
1 parent
d92bdff
commit 9d2625d
Showing
5 changed files
with
133 additions
and
18 deletions.
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
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
''' | ||
Favorite Files | ||
Licensed under MIT | ||
Copyright (c) 2012 Isaac Muse <isaacmuse@gmail.com> | ||
''' | ||
|
||
import re | ||
|
||
# Comment types: lambda stripping function | ||
comment_styles = { | ||
"c": lambda text, save_newlines=True: strip_comments_by_regex( | ||
# First group comments, second group non comments | ||
r"""(/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|\s*//(?:[^\r\n])*)|("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|.[^/"']*)""", | ||
text, save_newlines | ||
), | ||
"python": lambda text, save_newlines: strip_comments_by_regex( | ||
# First group comments, second group non comments | ||
r"""(\s*#(?:[^\r\n])*)|("{3}(?:\\.|[^\\])*"{3}|'{3}(?:\\.|[^\\])*'{3}|"(?:\\.|[^"\\])*"|'(?:\\.|[^'])*'|.[^#"']*)""", | ||
text, save_newlines | ||
) | ||
} | ||
|
||
# Additional styles that use the same style as others | ||
comment_map = { | ||
"json": "c", # Comments are not officially in the JSON spec, but if you strip them, you can use them | ||
"trex": "c", | ||
"cpp": "c" | ||
} | ||
|
||
|
||
def strip_comments_by_regex(expression, text, spare_newlines=False): | ||
# If you want to remove comments, but keep the exact line_numbers | ||
def filter_comments(group, spare_newlines=False): | ||
if spare_newlines: | ||
regex = re.compile(r"\r?\n", re.MULTILINE) | ||
return ''.join([x[0] for x in regex.findall(group)]) | ||
else: | ||
return '' | ||
|
||
return ( | ||
''.join( | ||
map( | ||
lambda m: m.group(2) if m.group(2) else filter_comments(m.group(1), spare_newlines), | ||
re.compile(expression, re.MULTILINE | re.DOTALL).finditer(text) | ||
) | ||
) | ||
) | ||
|
||
|
||
def strip_comments(style, text, save_newlines=False): | ||
s = style.lower() | ||
func = None | ||
|
||
# Try to find specified comment, return none if cannot find | ||
if s in comment_styles: | ||
func = comment_styles[s] | ||
# Look for alternate mappings for existing remove functions | ||
elif s in comment_map: | ||
if comment_map[s] in comment_styles: | ||
func = comment_styles[comment_map[s]] | ||
|
||
# Exit if no suitable function could be found | ||
if func == None: | ||
return None | ||
|
||
return func(text, save_newlines) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
''' | ||
Favorite Files | ||
Licensed under MIT | ||
Copyright (c) 2012 Isaac Muse <isaacmuse@gmail.com> | ||
''' | ||
|
||
import re | ||
import comments | ||
|
||
|
||
def strip_dangling_comments(text, save_newlines=False): | ||
regex = re.compile( | ||
# ([1st group] dangling commas) | ([8th group] everything else) | ||
r"""((,([\s\r\n]*)(\]))|(,([\s\r\n]*)(\})))|("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|.[^,"']*)""", | ||
re.MULTILINE | re.DOTALL | ||
) | ||
|
||
def remove_comma(m, spare_newlines=False): | ||
if spare_newlines: | ||
if m.group(2): | ||
# ,] -> ] | ||
return m.group(3) + m.group(4) | ||
else: | ||
# ,} -> } | ||
return m.group(6) + m.group(7) | ||
else: | ||
# ,] -> ] else ,} -> } | ||
return m.group(4) if m.group(2) else m.group(7) | ||
|
||
return ( | ||
''.join( | ||
map( | ||
lambda m: m.group(8) if m.group(8) else remove_comma(m, save_newlines), | ||
regex.finditer(text) | ||
) | ||
) | ||
) | ||
|
||
|
||
def strip_comments(text, save_newlines): | ||
return strip_comments('json', text, save_newlines) | ||
|
||
|
||
def sanitize_json(text, save_newlines=False): | ||
return strip_dangling_comments(comments.strip_comments('json', text, save_newlines), save_newlines) |