src/eric7/UI/Previewers/MarkdownExtensions.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 ## 19 ##
20 ## Original code Copyright 2018-2020 [Olivier Ruelle] 20 ## Original code Copyright 2018-2020 [Olivier Ruelle]
21 ## License: GNU GPLv3 21 ## License: GNU GPLv3
22 ###################################################################### 22 ######################################################################
23 23
24 MermaidRegex = re.compile( 24 MermaidRegex = re.compile(r"^(?P<mermaid_sign>[\~\`]){3}[ \t]*[Mm]ermaid[ \t]*$")
25 r"^(?P<mermaid_sign>[\~\`]){3}[ \t]*[Mm]ermaid[ \t]*$" 25 MermaidRegexFullText = re.compile(r"([\~\`]){3}[ \t]*[Mm]ermaid")
26 )
27 MermaidRegexFullText = re.compile(
28 r"([\~\`]){3}[ \t]*[Mm]ermaid"
29 )
30 26
31 27
32 class MermaidPreprocessor(Preprocessor): 28 class MermaidPreprocessor(Preprocessor):
33 """ 29 """
34 Class implementing a markdown pre-processor for Mermaid. 30 Class implementing a markdown pre-processor for Mermaid.
35 """ 31 """
32
36 def run(self, lines): 33 def run(self, lines):
37 """ 34 """
38 Public method to do the pre-processing. 35 Public method to do the pre-processing.
39 36
40 @param lines text lines to be processed 37 @param lines text lines to be processed
41 @type list of str 38 @type list of str
42 @return processed lines 39 @return processed lines
43 @rtype list of str 40 @rtype list of str
44 """ 41 """
67 if not is_mermaid: 64 if not is_mermaid:
68 is_mermaid = True 65 is_mermaid = True
69 new_lines.append('<div class="mermaid">') 66 new_lines.append('<div class="mermaid">')
70 m_start = None 67 m_start = None
71 elif m_end: 68 elif m_end:
72 new_lines.append('</div>') 69 new_lines.append("</div>")
73 new_lines.append("") 70 new_lines.append("")
74 m_end = None 71 m_end = None
75 elif in_mermaid_code: 72 elif in_mermaid_code:
76 new_lines.append( 73 new_lines.append(line.strip().replace("<", "&lt;").replace(">", "&gt;"))
77 line.strip()
78 .replace("<", "&lt;")
79 .replace(">", "&gt;")
80 )
81 else: 74 else:
82 75
83 new_lines.append(line) 76 new_lines.append(line)
84 77
85 old_line = line 78 old_line = line
86 79
87 if is_mermaid: 80 if is_mermaid:
88 new_lines.append('') 81 new_lines.append("")
89 82
90 return new_lines 83 return new_lines
91 84
92 85
93 class MermaidExtension(Extension): 86 class MermaidExtension(Extension):
94 """ 87 """
95 Class implementing a Markdown Extension for Mermaid. 88 Class implementing a Markdown Extension for Mermaid.
96 """ 89 """
90
97 def extendMarkdown(self, md, md_globals): 91 def extendMarkdown(self, md, md_globals):
98 """ 92 """
99 Public method to register the extension. 93 Public method to register the extension.
100 94
101 @param md reference to markdown 95 @param md reference to markdown
102 @param md_globals global config parameters 96 @param md_globals global config parameters
103 """ 97 """
104 md.preprocessors.register(MermaidPreprocessor(md), 'mermaid', 35) 98 md.preprocessors.register(MermaidPreprocessor(md), "mermaid", 35)
105 md.registerExtension(self) 99 md.registerExtension(self)
100
106 101
107 ###################################################################### 102 ######################################################################
108 ## Some extension to some basic additions 103 ## Some extension to some basic additions
109 ###################################################################### 104 ######################################################################
110 105
111 106
112 class SimplePatternExtension(Extension): 107 class SimplePatternExtension(Extension):
113 """ 108 """
114 Class implementing a Markdown extension for ~, ~~, ^, ^^ and ==. 109 Class implementing a Markdown extension for ~, ~~, ^, ^^ and ==.
115 110
116 Note: This is a very simple pattern extension that might conflict with 111 Note: This is a very simple pattern extension that might conflict with
117 formulas set for MathJax. Use the 'pymdown-extensions' package in this 112 formulas set for MathJax. Use the 'pymdown-extensions' package in this
118 case. 113 case.
119 """ 114 """
120 DEL_RE = r'(~~)(.+?)~~' 115
121 SUB_RE = r'(~)(.+?)~' 116 DEL_RE = r"(~~)(.+?)~~"
122 INS_RE = r'(\^\^)(.*?)\^\^' 117 SUB_RE = r"(~)(.+?)~"
123 SUP_RE = r'(\^)(.*?)\^' 118 INS_RE = r"(\^\^)(.*?)\^\^"
124 MARK_RE = r'(==)(.*?)==' 119 SUP_RE = r"(\^)(.*?)\^"
125 120 MARK_RE = r"(==)(.*?)=="
121
126 def extendMarkdown(self, md): 122 def extendMarkdown(self, md):
127 """ 123 """
128 Public method to register the extension. 124 Public method to register the extension.
129 125
130 @param md reference to markdown 126 @param md reference to markdown
131 """ 127 """
132 md.inlinePatterns.register( 128 md.inlinePatterns.register(
133 SimpleTagInlineProcessor(self.SUB_RE, 'sub'), 'subscript', 30) 129 SimpleTagInlineProcessor(self.SUB_RE, "sub"), "subscript", 30
130 )
134 md.inlinePatterns.register( 131 md.inlinePatterns.register(
135 SimpleTagInlineProcessor(self.DEL_RE, 'del'), 'deleted', 40) 132 SimpleTagInlineProcessor(self.DEL_RE, "del"), "deleted", 40
133 )
136 md.inlinePatterns.register( 134 md.inlinePatterns.register(
137 SimpleTagInlineProcessor(self.SUP_RE, 'sup'), 'superscript', 30) 135 SimpleTagInlineProcessor(self.SUP_RE, "sup"), "superscript", 30
136 )
138 md.inlinePatterns.register( 137 md.inlinePatterns.register(
139 SimpleTagInlineProcessor(self.INS_RE, 'ins'), 'inserted', 40) 138 SimpleTagInlineProcessor(self.INS_RE, "ins"), "inserted", 40
139 )
140 md.inlinePatterns.register( 140 md.inlinePatterns.register(
141 SimpleTagInlineProcessor(self.MARK_RE, 'mark'), 'mark', 40) 141 SimpleTagInlineProcessor(self.MARK_RE, "mark"), "mark", 40
142 )

eric ide

mercurial