如何批量将md转为html,idea之md文件批量转化为html页面

#-*- coding: utf-8 -*

__author__ = ‘geebos‘

importreimportmarkdownfrom pygments importhighlightfrom pygments.token importText, STANDARD_TYPESfrom pygments.formatter importFormatterfrom pygments.lexers importget_lexer_by_namefrom pygments.lexers importguess_lexerdef_get_ttype_class(ttype):

fname=STANDARD_TYPES.get(ttype)iffname:returnfname

aname= ‘‘

while fname isNone:

aname= ‘-‘ + ttype[-1] +aname

ttype=ttype.parent

fname=STANDARD_TYPES.get(ttype)return fname +anamedef_line_num_tag_gen():

line_num=0defresult():

nonlocal line_num

line_num+= 1

return f‘

returnresultclassHtmlLiFormatter(Formatter):def __init__(self, **options):

Formatter.__init__(self, **options)def_get_css_class(self, ttype):"""Return the css class of this token type prefixed with

the classprefix option."""ttypeclass=_get_ttype_class(ttype)ifttypeclass:returnttypeclassreturn ‘‘

defhtml_encode(self, value):if ‘

value= value.replace(‘‘ invalue:

value= value.replace(‘>‘, ‘>‘)returnvaluedef_get_css_classes(self, ttype):"""Return the css classes of this token type prefixed with

the classprefix option."""cls=self._get_css_class(ttype)while ttype not inSTANDARD_TYPES:

ttype=ttype.parent

cls= self._get_css_class(ttype) + ‘ ‘ +clsreturnclsdefformat(self, tokensource, outfile):

get_line_num_tag=_line_num_tag_gen()

line_start_tag= ‘

‘line_end_tag= ‘‘code_tags= [‘
  1. ‘]

num_tags= [‘

  1. ‘]

line_value= ‘‘outfile.write(‘

‘)#预处理

temp_tokensource =[]for ttype, value intokensource:

value= value.replace(‘ ‘, ‘ ‘)if ttype == Text and ‘\n‘ invalue:

values= re.findall(pattern=‘([^\n]*)(\n)([^\n]*)‘, string=value)for i invalues:for k ini:if k != ‘‘:

temp_tokensource.append((ttype, k))else:

temp_tokensource.append((ttype, value))for ttype, value intemp_tokensource:

ttype_class=self._get_css_classes(ttype)

value=self.html_encode(value)if value != ‘\n‘:

line_value+= f‘{value}

else:

num_tags.append(get_line_num_tag())

code_tags.append(f‘{line_start_tag}

{line_value}
{line_end_tag}\n‘)

line_value= ‘‘num_tags.append(‘‘)

code_tags.append(‘‘)

outfile.write(f‘{"".join(num_tags)}{"".join(code_tags)}‘)

outfile.write(‘

\n‘)defcode_to_html(match):

type_and_content= re.findall(pattern=‘```(\w*)[\n|\r]([^`]+)```‘, string=match.group(0))

formatter= HtmlLiFormatter(linenos=True, style=‘colorful‘)

code_type=type_and_content[0][0]

code_content= type_and_content[0][1]if code_type != ‘‘:

substring= highlight(code=code_content, lexer=get_lexer_by_name(code_type), formatter=formatter)else:

substring= highlight(code=code_content, lexer=guess_lexer(code_content), formatter=formatter)returnsubstringdefmd_to_html(mdstr):

sub_string= re.sub(pattern=‘```([^`]+)```‘, repl=code_to_html, string=mdstr)

exts= [‘markdown.extensions.extra‘, ‘markdown.extensions.tables‘]

html= markdown.markdown(sub_string, extensions=exts)returnhtmlif __name__ == ‘__main__‘:#在这里调用md_to_html就可以将md文件内容转化为str类型的html代码

res = md_to_html(open(‘blog.md‘, ‘r‘, encoding=‘utf8‘).read())

htmlstr= ‘

‘ + res + ‘‘open(‘webpage.html‘, ‘w‘, encoding=‘utf8‘).write(htmlstr)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值