Python: Highlight triple-quoted strings before comments. Fix #815
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
diff --git a/components/prism-python.js b/components/prism-python.js
index eb97f48..e020c9d 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 743aa35..cecce9d 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 fbb6fea..8c7b6bb 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 0000000..ab1e2ab
--- /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