python实现 CCF201703-3 Markdown

题目链接:https://blog.csdn.net/qq_34950042/article/details/88427915

满分代码

import sys

data = []           # 记录Markdown转化为html后的文档,data中的一个元素代表html的一行(除开'')
flag = False        # 标记段落是否多行
list_tag = False    # 标记无序列表是否多行
for line in sys.stdin: # line代表键盘输入的每行内容

    # 区块
    line = line.strip()
    if '#' in line:                          # 标题
        count = line.count("#")              # 计算是第几标题
        temp = line.split('#')[-1].strip()   # 这里分割最好不要用“空格”防止标题含有空格
        temp = "<h" + str(count) + ">" + temp + "</h" + str(count) + ">"
    elif '*' in line:                        # 无序列表
        if list_tag == False:
            data.append('<ul>')
            list_tag = True
        temp = line.split("*")[-1].strip()   # 采用“*”分割
        temp = "<li>" + temp + "</li>"
    else:                                    # 段落
        if line and flag == False:           # 首次出现段落
            temp = '<p>' + line
            flag = True
        elif line and flag == True:          # 中间出现段落
            temp = line
        elif line == '' and flag == True:    # 段落结束,修改data最后一个元素的值(即加上'</p>')
            data[-1] = data[-1] + '</p>'
            flag = False
            temp = ''
        elif line == '' and list_tag == True:# 无序列表结束
            data.append("</ul>")
            temp = ""
            list_tag = False
        else:
            temp = ''
            flag = False
            list_tag = False

    # 行内

    # 强调
    i = 1                                   # 标记是第一个"_",还是第二个
    while '_' in temp:                      # 注意强调以及超链接都可能多个,所以用无限循环
        index_1 = temp.find('_')
        if i == 1:
            temp = temp[:index_1] + '<em>' + temp[index_1 + 1:]
            i = 2
        else:
            temp = temp[:index_1] + '</em>' + temp[index_1 + 1:]
            i = 1
    # 超链接
    while '[' in temp:                      # 注意这里是while,可能有多个超链接
        i1 = temp.find('[')
        i2 = temp.find(']', i1 + 1)
        i3 = temp.find('(', i2 + 1)
        i4 = temp.find(')', i3 + 1)
        temp = temp[:i1] + '<a href="' + temp[(i3 + 1):i4] + '">' + temp[(i1 + 1):i2] + "</a>" + temp[(i4 + 1):]

    data.append(temp)                       # 将转化后的html加入data
if flag == True:                            # 当以段落结束时
    data[-1] = data[-1] + '</p>'
if list_tag == True:                        # 当以无序列表结束时
    data.append("</ul>")
for d in data:
    if d == '':
        continue
    print(d)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值