Skip to content

Commit

Permalink
Remove escapes from embedded quotes
Browse files Browse the repository at this point in the history
Fixes issue #3
  • Loading branch information
hsolbrig committed Apr 10, 2018
1 parent c217a6c commit 24cdef2
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions parsers/python/pyshexc/parser_impl/parser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,15 @@ def literal_to_ObjectLiteral(self, literal: ShExDocParser.LiteralContext) -> ShE
if literal.rdfLiteral():
rdflit = literal.rdfLiteral()
txt = rdflit.string().getText()
txt = txt[3:-3] \
if len(txt) > 5 and (txt.startswith("'''") and txt.endswith("'''") or
txt.startswith('"""') and txt.endswith('"""')) else txt[1:-1]
txt = self.fix_text_escapes(txt)
quote_char = ''
if len(txt) > 5 and (txt.startswith("'''") and txt.endswith("'''") or
txt.startswith('"""') and txt.endswith('"""')):
txt = txt[3:-3]
else:
quote_char = txt[0]
txt = txt[1:-1]

txt = self.fix_text_escapes(txt, quote_char)
rval.value = jsg.String(txt)
if rdflit.LANGTAG():
rval.language = ShExJ.LANGTAG(rdflit.LANGTAG().getText()[1:].lower())
Expand Down Expand Up @@ -187,11 +192,12 @@ def is_empty_shape(sh: ShExJ.Shape) -> bool:

re_trans_table = str.maketrans('bfnrt', '\b\f\n\r\t')

def fix_text_escapes(self, txt: str) -> str:
def fix_text_escapes(self, txt: str, quote_char: str) -> str:
""" Fix the various text escapes """
def _subf(matchobj):
return matchobj.group(0).translate(self.re_trans_table)

if quote_char:
txt = re.sub(r'\\'+quote_char, quote_char, txt)
return re.sub(r'\\.', _subf, txt, flags=re.MULTILINE + re.DOTALL + re.UNICODE)

def fix_re_escapes(self, txt: str) -> str:
Expand Down

0 comments on commit 24cdef2

Please sign in to comment.