Skip to content

Commit

Permalink
Feature: added automatic escaping of comas by default (to prevent bre…
Browse files Browse the repository at this point in the history
…aking the line), and option '--do-not-force-escape-comas' to disable it
  • Loading branch information
mbideau committed Oct 25, 2020
1 parent 014c2c8 commit 0a5be80
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vcardlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
OPTION_MATCH_APPROX_RATIO = 100
OPTION_UPDATE_GROUP_KEY = True
OPTION_FRENCH_TWEAKS = False
OPTION_DOT_NOT_FORCE_ESCAPE_COMAS = False

SINGLE_INSTANCE_PROPERTIES = {'prodid', 'rev', 'uid'}

Expand Down Expand Up @@ -886,6 +887,8 @@ def fix_and_convert_to_v3(file_path):
logging.debug("\tmultiline value hack (joining this line with the previous)")
if last_line.replace('\n', '')[-1:] == '=':
last_line = re.sub('=$', '', last_line.replace('\n', '')) + '\n'
if not OPTION_DOT_NOT_FORCE_ESCAPE_COMAS:
line = re.sub('([^\\\\]|^),', '\\1\\,', line)
last_line = last_line.replace('\n', '') + line.strip() + '\n'
logging.debug("\tconcatened: '%s'", line.strip())
logging.debug("\t")
Expand All @@ -912,8 +915,12 @@ def fix_and_convert_to_v3(file_path):
# convert keys to upper case
key_part = new_line.rsplit(':')[0].upper()
logging.debug("\tkey part: '%s'", key_part)

rest_part = re.sub(r'^([^:]+):', '', new_line)
logging.debug("\trest part: '%s'", rest_part)
if not OPTION_DOT_NOT_FORCE_ESCAPE_COMAS:
rest_part = re.sub('([^\\\\]|^),', '\\1\\,', rest_part)

new_line = key_part + ':' + rest_part
logging.debug("\tbuilt new line: %s", new_line)

Expand Down
7 changes: 7 additions & 0 deletions vcardtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def init_parser():
'--no-remove-name-in-email', dest='no_remove_name_in_email', action='store_true', \
help="Do not removes name in email, i.e.: keep email like the following untouched: \"John Doe\" <[email protected]>" \
)
parser.add_argument( \
'--do-not-force-escape-comas', dest='do_not_force_escape_comas', action='store_true', \
help="Disable automatically escaping comas." \
)
parser.add_argument( \
'-l', '--log-level', dest='log_level', default='INFO', \
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], \
Expand Down Expand Up @@ -143,6 +147,9 @@ def main():
# french tweaks
vcardlib.OPTION_FRENCH_TWEAKS = args.french_tweaks

# coma auto escape
vcardlib.OPTION_DOT_NOT_FORCE_ESCAPE_COMAS = args.do_not_force_escape_comas


# check DESTDIR argument
if exists(args.dest_dir):
Expand Down

0 comments on commit 0a5be80

Please sign in to comment.