Skip to content

Commit

Permalink
add convert_list feature
Browse files Browse the repository at this point in the history
  • Loading branch information
insula1701 committed May 18, 2018
1 parent 7ae5a7e commit 069fd94
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
3 changes: 3 additions & 0 deletions Quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
|para_spacing |1.5em |正文段间距|
|align |多项 |各部分的水平对齐方式,建议`left``center``h1``h6`代表标题1~标题6,`content`代表正文)|
|main_margin |3% |内容两侧留白比例|
|banner_url |"" |文章头部引导关注图片的url|
|poster_url |"" |底部二维码/海报图片的地址|
|convert_list |true |将正文中的列表转换为普通段落,以修正微信不能正常显示列表序号样式的问题(仅用于微信)|
|ul_style |"○" |将无序列表转换为普通段落后,每项之前的符号标识(仅当`convert_list``true`时启用)|
|auto_archive |"" |是否自动存档(转换后将原始`.md`文件移动至`result/archive`目录下)|
|auto_rename |false |冲突文件名的处理:`true`自动重命名;`false`覆盖先前的文件|

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,20 @@
|para_spacing |1.5em |正文段间距|
|align |多项 |各部分的水平对齐方式,建议`left``center``h1``h6`代表标题1~标题6,`content`代表正文)|
|main_margin |3% |内容两侧留白比例|
|banner_url |"" |文章头部引导关注图片的url|
|poster_url |"" |底部二维码/海报图片的地址|
|convert_list |true |将正文中的列表转换为普通段落,以修正微信不能正常显示列表序号样式的问题(仅用于微信)|
|ul_style |"○" |将无序列表转换为普通段落后,每项之前的符号标识(仅当`convert_list``true`时启用)|
|auto_archive |"" |是否自动存档(转换后将原始`.md`文件移动至`result/archive`目录下)|
|auto_rename |false |冲突文件名的处理:`true`自动重命名;`false`覆盖先前的文件|


**备注:**

- 如果对自定义的要求不高,建议更换一下`theme_color`,其余可以采用默认配置。
- 目前这版微信UI,貌似对所有列表序号都只能显示默认样式,即使把样式写进上级元素,粘贴进编辑器的时候也会被“洗掉”,目前尚未找到方法绕过此限制,因此添加`convert_list`选项作为临时解决方案,当此项为`true`时,正文中的所有列表(不包括代码块中的内容)会被转化为段首带序号的普通段落。注意,这种情况下,`styles.less`中专门为列表设置的样式将会失效。如果你有更好的办法,欢迎开issue告诉我。


#### 更多自定义

如果你希望覆盖默认样式中的个别样式,可以自主编写`custom.css`,它将在`default.css`之后被引入。
Expand Down
2 changes: 2 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
},
"banner_url": "",
"poster_url": "",
"convert_list": true,
"ul_style": "",
"auto_archive": true,
"auto_rename": false
}
Expand Down
20 changes: 10 additions & 10 deletions less/default.less
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
@main_size: 16px;

@line_height: 1.8em;

@text_color: #555;
@quote_color: #999;

@para_spacing: 1.5em;

@main_margin: 3%;
@content_align: left;

@h2_align: left;

@h3_align: center;

@h6_align: center;

@h1_align: left;
@h4_align: center;

@h5_align: center;

@content_align: left;
@h1_align: left;

@h4_align: center;
@main_margin: 3%;

@main_size: 16px;

@text_color: #555;

@theme_color: #3fa944;

@quote_color: #999;
@line_height: 1.8em;
// 运行时程序将根据本文件中定义的格式自动生成default.less
Expand Down
20 changes: 18 additions & 2 deletions maxpress.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def import_config(file=join_path(ROOT, 'config.json')):
config = json.loads(json_text)

non_style_keys = ['poster_url', 'banner_url',
'convert_list', 'ul_style',
'auto_archive', 'auto_rename']

# 读取配置文件中的变量,最多支持两级嵌套
Expand Down Expand Up @@ -49,8 +50,21 @@ def compile_styles(file=join_path(ROOT, 'less', 'default.less')):


# 将待解析的md文档转换为适合微信编辑器的html
def md2html(text, styles=None, poster='', banner=''):
def md2html(text, styles=None, poster='', banner='', convert_list=True, ul_style='\u25CB'):
md = Markdown()

# 将markdown列表转化为带序号的普通段落(纯为适应微信中列表序号样式自动丢失的古怪现象)
if convert_list:
blocks = text.split('\n```')
for i in range(0, len(blocks)):
if i % 2 == 0:
blocks[i] = re.sub(r'(\n\d+)(\.\s.*?)', r'\n\1\\\2', blocks[i])
blocks[i] = re.sub(r'\n[\-\+\*](\s.*?)',
u'\n\n{} \1'.format(ul_style), blocks[i])
else:
continue # 跳过代码块内部内容
text = '\n```'.join(blocks)

inner_html = md(text)
result = premailer.transform(pack_html(inner_html, styles, poster, banner))
return result
Expand Down Expand Up @@ -161,7 +175,9 @@ def convert_all(src=join_path(ROOT, 'temp'),
text = md_file.read()
result = md2html(text, styles,
poster=config['poster_url'],
banner=config['banner_url'])
banner=config['banner_url'],
convert_list=config['convert_list'],
ul_style=config['ul_style'])
htmlpath = join_path(dst, file[:-3] + '.html')
if config['auto_rename']: htmlpath = autoname(htmlpath)
with open(htmlpath,'w', encoding='utf-8') as html_file:
Expand Down
Loading

0 comments on commit 069fd94

Please sign in to comment.