From 922db49b9fcccbf2a93c3939ad9dea66e18f33d8 Mon Sep 17 00:00:00 2001 From: crossdark Date: Mon, 14 Oct 2024 19:22:02 +0800 Subject: [PATCH] =?UTF-8?q?1.9=20=E4=BB=A3=E7=A0=81=E9=AB=98=E4=BA=AE?= =?UTF-8?q?=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CrossDown/Core.py | 37 ++++++++++++++++++++++++--------- CrossDown/__init__.py | 8 ++++--- README.html | 33 +++++++++++++++-------------- styles.css => Static/styles.css | 0 4 files changed, 49 insertions(+), 29 deletions(-) rename styles.css => Static/styles.css (100%) diff --git a/CrossDown/Core.py b/CrossDown/Core.py index 63810dc..920714f 100644 --- a/CrossDown/Core.py +++ b/CrossDown/Core.py @@ -1,4 +1,6 @@ -from markdown.extensions import Extension, extra, admonition, meta, sane_lists, toc, wikilinks +from markdown.extensions import Extension, extra, admonition, meta, sane_lists, toc, wikilinks, codehilite + +from pygments.formatters import HtmlFormatter from markdown.treeprocessors import Treeprocessor from markdown.inlinepatterns import Pattern as Pattern_ @@ -11,15 +13,6 @@ import re import xml import emoji -Extensions = { - "Extra": extra.ExtraExtension(fenced_code={'lang_prefix': ''}), # 基本扩展 - "Admonition": admonition.AdmonitionExtension(), # 警告扩展 - "Meta-Data": meta.MetaExtension(), # 元数据 - "Sane Lists": sane_lists.SaneListExtension(), # 只能列表 - "Table of Contents": toc.TocExtension(), # 目录 - "WikiLinks": wikilinks.WikiLinkExtension(), # 内部链接 -} - try: # 检测当前平台是否支持扩展语法 from .Extra import * @@ -28,6 +21,30 @@ except ModuleNotFoundError: # 不支持扩展语法 EXTRA_ABLE = False +class HighlightHtmlFormatter(HtmlFormatter): + def __init__(self, lang_str='', **options): + super().__init__(**options) + # lang_str has the value {lang_prefix}{lang} + # specified by the CodeHilite's options + self.lang_str = lang_str.split('-')[-1] + + def _wrap_code(self, source): + yield 0, f'' + yield from source + yield 0, '' + + +Extensions = { + '基本扩展': extra.ExtraExtension(fenced_code={'lang_prefix': ''}), + '警告扩展': admonition.AdmonitionExtension(), + '元数据': meta.MetaExtension(), + '能列表': sane_lists.SaneListExtension(), + '目录': toc.TocExtension(), + '内部链接': wikilinks.WikiLinkExtension(), + '代码高亮': codehilite.CodeHiliteExtension(guess_lang=False, pygments_formatter=HighlightHtmlFormatter), +} + + class Simple(InlineProcessor): """ 可通过简单的正则表达式和HTML标签实现的样式 diff --git a/CrossDown/__init__.py b/CrossDown/__init__.py index 4af49aa..b108b75 100644 --- a/CrossDown/__init__.py +++ b/CrossDown/__init__.py @@ -28,9 +28,11 @@ HEAD = ( '', # Highlight.js - '', - '', - '', + # '', + # '', + # '', + + '', '