From 90fbf0be283c4d68c74d1754bed06704ea02de8f Mon Sep 17 00:00:00 2001 From: Golmote Date: Mon, 26 Oct 2015 08:08:13 +0100 Subject: [PATCH] Python: Highlight triple-quoted strings before comments. Fix #815 --- components/prism-python.js | 6 +++++- components/prism-python.min.js | 2 +- tests/languages/python/string_feature.test | 10 --------- .../python/triple-quoted-string_feature.test | 21 +++++++++++++++++++ 4 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 tests/languages/python/triple-quoted-string_feature.test diff --git a/components/prism-python.js b/components/prism-python.js index eb97f48eca..e020c9dafd 100644 --- a/components/prism-python.js +++ b/components/prism-python.js @@ -1,9 +1,13 @@ Prism.languages.python= { + 'triple-quoted-string': { + pattern: /"""[\s\S]+?"""|'''[\s\S]+?'''/, + alias: 'string' + }, 'comment': { pattern: /(^|[^\\])#.*/, lookbehind: true }, - 'string': /"""[\s\S]+?"""|'''[\s\S]+?'''|("|')(?:\\?.)*?\1/, + 'string': /("|')(?:\\?.)*?\1/, 'function' : { pattern: /((?:^|\s)def[ \t]+)[a-zA-Z_][a-zA-Z0-9_]*(?=\()/g, lookbehind: true diff --git a/components/prism-python.min.js b/components/prism-python.min.js index 743aa35f7f..cecce9de2c 100644 --- a/components/prism-python.min.js +++ b/components/prism-python.min.js @@ -1 +1 @@ -Prism.languages.python={comment:{pattern:/(^|[^\\])#.*/,lookbehind:!0},string:/"""[\s\S]+?"""|'''[\s\S]+?'''|("|')(?:\\?.)*?\1/,"function":{pattern:/((?:^|\s)def[ \t]+)[a-zA-Z_][a-zA-Z0-9_]*(?=\()/g,lookbehind:!0},"class-name":{pattern:/(\bclass\s+)[a-z0-9_]+/i,lookbehind:!0},keyword:/\b(?:as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|pass|print|raise|return|try|while|with|yield)\b/,"boolean":/\b(?:True|False)\b/,number:/\b-?(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,operator:/[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]|\b(?:or|and|not)\b/,punctuation:/[{}[\];(),.:]/}; \ No newline at end of file +Prism.languages.python={"triple-quoted-string":{pattern:/"""[\s\S]+?"""|'''[\s\S]+?'''/,alias:"string"},comment:{pattern:/(^|[^\\])#.*/,lookbehind:!0},string:/("|')(?:\\?.)*?\1/,"function":{pattern:/((?:^|\s)def[ \t]+)[a-zA-Z_][a-zA-Z0-9_]*(?=\()/g,lookbehind:!0},"class-name":{pattern:/(\bclass\s+)[a-z0-9_]+/i,lookbehind:!0},keyword:/\b(?:as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|pass|print|raise|return|try|while|with|yield)\b/,"boolean":/\b(?:True|False)\b/,number:/\b-?(?:0[bo])?(?:(?:\d|0x[\da-f])[\da-f]*\.?\d*|\.\d+)(?:e[+-]?\d+)?j?\b/i,operator:/[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]|\b(?:or|and|not)\b/,punctuation:/[{}[\];(),.:]/}; \ No newline at end of file diff --git a/tests/languages/python/string_feature.test b/tests/languages/python/string_feature.test index fbb6fea4e8..8c7b6bbea4 100644 --- a/tests/languages/python/string_feature.test +++ b/tests/languages/python/string_feature.test @@ -1,9 +1,3 @@ -"""foobar""" -"""fo"o -bar""" -'''foobar''' -'''fo'o -bar''' "" "fo\"obar" '' @@ -12,10 +6,6 @@ bar''' ---------------------------------------------------- [ - ["string", "\"\"\"foobar\"\"\""], - ["string", "\"\"\"fo\"o\r\nbar\"\"\""], - ["string", "'''foobar'''"], - ["string", "'''fo'o\r\nbar'''"], ["string", "\"\""], ["string", "\"fo\\\"obar\""], ["string", "''"], diff --git a/tests/languages/python/triple-quoted-string_feature.test b/tests/languages/python/triple-quoted-string_feature.test new file mode 100644 index 0000000000..ab1e2ab759 --- /dev/null +++ b/tests/languages/python/triple-quoted-string_feature.test @@ -0,0 +1,21 @@ +"""foobar""" +"""fo"o +#bar +baz""" +'''foobar''' +'''fo'o +#bar +baz''' + +---------------------------------------------------- + +[ + ["triple-quoted-string", "\"\"\"foobar\"\"\""], + ["triple-quoted-string", "\"\"\"fo\"o\r\n#bar\r\nbaz\"\"\""], + ["triple-quoted-string", "'''foobar'''"], + ["triple-quoted-string", "'''fo'o\r\n#bar\r\nbaz'''"] +] + +---------------------------------------------------- + +Checks for triple-quoted strings. \ No newline at end of file