(self)
| 20 | return t.hexdigest() |
| 21 | |
| 22 | def run(self): |
| 23 | if self.env.app.builder.name == 'gettext': |
| 24 | return super().run() |
| 25 | |
| 26 | code_save_path = os.environ.get('CODE_EXPORT_PATH') |
| 27 | caption = self.options.get('summary', '') |
| 28 | |
| 29 | if code_save_path and not os.path.exists(code_save_path): |
| 30 | os.mkdir(code_save_path) |
| 31 | |
| 32 | code_id = self.md5('\n'.join(self.content))[-5:] |
| 33 | if self.options.get('name', None) is None: |
| 34 | # 设置name属性,从而让生成的代码html块具有id属性 |
| 35 | self.options.update({'name': 'demo-' + code_id}) |
| 36 | else: |
| 37 | name = self.options.get('name', '') |
| 38 | self.options.update({'name': 'demo-' + name}) |
| 39 | |
| 40 | name = self.options.get('name').replace('_', '-') |
| 41 | if name in type(self).names: |
| 42 | name += '-' + code_id |
| 43 | self.options.update({'name': name}) |
| 44 | else: |
| 45 | type(self).names.add(name) |
| 46 | |
| 47 | # 设置特殊class值,用于在js中搜索 |
| 48 | classes = self.options.get('class', []) |
| 49 | classes.append('demo-cb') |
| 50 | self.options.update({'class': classes}) |
| 51 | |
| 52 | raw_content_text = '\n'.join(self.content) |
| 53 | |
| 54 | content, self.content = self.content, [] |
| 55 | for c in content: |
| 56 | if '..demo-only' in c or '## ----' in c: |
| 57 | continue |
| 58 | c = c.replace('# ..doc-only', '') |
| 59 | self.content.append(c) |
| 60 | |
| 61 | nodes = super().run() |
| 62 | |
| 63 | try: |
| 64 | elem_id = nodes[0]['ids'][0] |
| 65 | except IndexError: |
| 66 | elem_id = None |
| 67 | |
| 68 | if code_save_path and elem_id: |
| 69 | fpath = os.path.join(code_save_path, elem_id) |
| 70 | open(fpath, 'w').write(caption + '\n\n' + raw_content_text) |
| 71 | |
| 72 | return nodes |
| 73 | |
| 74 | |
| 75 | def setup(app): |
no test coverage detected