python.tkinter操持标志语言(转译2-html)

[复制链接]
发表于 2026-2-10 06:25:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
@TOC
前言

本文只作为条记纪录。
前文我们已经通过TinText渲染器部分和TinML得到了test.tin表明后的标志内容列表。本文,我们将根据这个表明结果将Tin标志转为html文件。
转为html的长处

第一,Tin标志语言作为一个小小小小小项目,光把编写内容出现在TinText中是不敷的,必要导出为别的常见文件。
第二,html富足常见,而且可以通过html格式继承转化为别的格式文件。html转图片、PDF、Markdown等格式的功能,已经有了美满的实现。我们只必要把Tin标志文本转译为html格式文本,就相称于打开了一扇落地窗😇,进而可以借助html格式变化为别的可以大概变化的任何格式文件。
而假如我们想给Tin标志文本转化为另一个新的格式文件,大概单纯就是为了练手,同样可以根据表明结果列表来举行编写。本篇文章中,就以HTML为例。
tin转译为html

工具类

我们先界说一下转译工具类。
  1. class TinTranslator():
  2.     """
  3.     tin->html的转译工具类
  4.     *后续可能支持tin->markdown
  5.     """
  6.     def __init__(self,tinml:TinML):
  7.         self.tinml=tinml
  8.         self.doc=None
  9.         
  10.         self.tinPmark=('*','/','_','-','!')
  11.         self.tinPlink_re=re.compile('.*?!\[(.*?)\]\((..*?)\)')
复制代码
这里,我们界说了一些根本属性和小工具。
TinTranslator直接绑定TinML,做到及时同步。
转译功能

这里先以<title>为例。
TinText利用dominate作为html天生工具。
  1.     def tohtml(self,_title='TinText',_style=''):
  2.         #tin->html
  3.         doc=dominate.document(title=_title)
  4.         doc.head.add(meta(charset='utf-8'))
  5.         if _style!='':
  6.             doc.head.add(style(_style))
  7.         _body=div()
  8.         doc.body.add(_body)
  9.         for tag,kw in self.tinml:
  10.             if tag == '<title>':
  11.                 #标题
  12.                 text=kw['title']
  13.                 level=int(kw['level'])
  14.                 if level==1:
  15.                     _body.add(h1(text))
  16.                 elif level==2:
  17.                     _body.add(h2(text))
  18.                 elif level==3:
  19.                     _body.add(h3(text))
  20.                 elif level==4:
  21.                     _body.add(h4(text))
  22.                 elif level==5:
  23.                     _body.add(h5(text))
  24.                 elif level==6:
  25.                     _body.add(h6(text))
复制代码
有相识释结果,转译就很方便了,不必要处置处罚大概的错误。
<p>有点复杂,由于Tin的p标志与html的p标志并差别级,而是包罗了html中的b, u, i, s, a等标志,还要继承下分情况,因此必要雷同TinText中的预渲染步调。
  1.     def __tinP_to_html(self,texts):
  2.         #tin段落转html段落
  3.         res=[]
  4.         for text in texts:
  5.             if text=='':
  6.                 res.append(br())
  7.             elif text[0]==' ':
  8.                 res.append(text[1:])
  9.             elif text[0] not in self.tinPmark:
  10.                 res.append(text)
  11.             else:
  12.                 head_mark=text[:5]
  13.                 head_num=0
  14.                 now_p=None#初始化,空
  15.                 if '*' in head_mark: head_num+=1
  16.                 if '/' in head_mark: head_num+=1
  17.                 if '_' in head_mark: head_num+=1
  18.                 if '-' in head_mark: head_num+=1
  19.                 #开始具体转义html<p>
  20.                 if '!' in head_mark:
  21.                     result=self.tinPlink_re.match(text)
  22.                     if result:
  23.                         url_text,url=result.groups()
  24.                         if url_text=='':
  25.                             url_text=url
  26.                         now_p=a(url_text,href=url)
  27.                     else:
  28.                         head_num+=1
  29.                 if '*' in head_mark:
  30.                     if now_p:
  31.                         now_p=b(now_p)
  32.                     else:
  33.                         now_p=b(text[head_num:])
  34.                 if '/' in head_mark:
  35.                     if now_p:
  36.                         now_p=i(now_p)
  37.                     else:
  38.                         now_p=i(text[head_num:])
  39.                 if '_' in head_mark:
  40.                     if now_p:
  41.                         now_p=u(now_p)
  42.                     else:
  43.                         now_p=u(text[head_num:])
  44.                 if '-' in head_mark:
  45.                     if now_p:
  46.                         now_p=s(now_p)
  47.                     else:
  48.                         now_p=s(text[head_num:])
  49.                 res.append(now_p)
  50.         return res
复制代码
转译部分的代码如下:
  1.             elif tag == '<p>':
  2.                 #段落
  3.                 texts=kw['text']
  4.                 _p=p('')
  5.                 htmltexts=self.__tinP_to_html(texts)
  6.                 for htmltext in htmltexts:
  7.                     _p.add(htmltext)
  8.                 _body.add(_p)
复制代码
只管保持转译部分的干净,额外的处置处罚交给转译器类的内置函数。
结果展示

下图中,左侧为欣赏器,右侧为TinText.TinReader。

项目所在

Github - TinText
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表