クレオール マークアップを HTMLに変換する Pyparsing 文法を作成しています。これらの 2 つの構造を解析しようとすると、少し競合が発生するため、行き詰まりました。
画像リンク: {{image.jpg|title}}
フォーマットを無視: {{{text}}}
画像リンクを解析する方法は次のとおりです (これは完全に正常に変換されることに注意してください)。
def parse_image(s, l, t):
try:
link, title = t[0].split("|")
except ValueError:
raise ParseFatalException(s,l,"invalid image link reference: " + t[0])
return '<img src="{0}" alt="{1}" />'.format(link, title)
image = QuotedString("{{", endQuoteChar="}}")
image.setParseAction(parse_image)
次に、{{{text}}} が検出されたときに、書式を設定せずに左中括弧と右中括弧の間を単純に返すようにルールを作成しました。
n = QuotedString("{{{", endQuoteChar="}}}")
n.setParseAction(lambda x: x[0])
ただし、次のテストケースを実行しようとすると:
text = italic | bold | hr | newline | image | n
print text.transformString("{{{ //ignore formatting// }}}")
次のスタック トレースを取得します。
Traceback (most recent call last):
File "C:\Users\User\py\kreyol\parser.py", line 36, in <module>
print text.transformString("{{{ //ignore formatting// }}}")
File "C:\Python27\lib\site-packages\pyparsing.py", line 1210, in transformString
raise exc
pyparsing.ParseFatalException: invalid image link reference: { //ignore formatting// (at char 0), (line:1, col:1)
私が理解していることから、パーサーは最初に {{ に遭遇し、テキストを書式設定なしのテキストではなく画像として解析しようとします。このあいまいさをどのように解決できますか?