加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

8 个 Python 适用剧本,保藏备用

发布时间:2019-10-10 06:34:07 所属栏目:建站 来源:实验楼
导读:剧本写的好,放工下得早!措施员的一般事变除了编写措施代码,还不行停止地必要处理赏罚相干的测试和验证事变。 譬喻,会见某个网站一向不通,必要确定此地点是否可会见,处事器返回什么,进而确定题目在于什么。完成这个使命,假如一味但愿回收编译型说话来编

7.将 Markdown 转换为 HTML。

  1. import sys 
  2. import os 
  3. from bs4 import BeautifulSoup 
  4. import markdown 
  5. class MarkdownToHtml: 
  6.  headTag = '<head><meta charset="utf-8" /></head>' 
  7.  def __init__(self,cssFilePath = None): 
  8.  if cssFilePath != None: 
  9.  self.genStyle(cssFilePath) 
  10.  def genStyle(self,cssFilePath): 
  11.  with open(cssFilePath,'r') as f: 
  12.  cssString = f.read() 
  13.  self.headTag = self.headTag[:-7] + '<style type="text/css">{}</style>'.format(cssString) + self.headTag[-7:] 
  14.  def markdownToHtml(self, sourceFilePath, destinationDirectory = None, outputFileName = None): 
  15.  if not destinationDirectory: 
  16.  # 未界说输出目次则将源文件目次(留意要转换为绝对路径)作为输出目次 
  17.  destinationDirectory = os.path.dirname(os.path.abspath(sourceFilePath)) 
  18.  if not outputFileName: 
  19.  # 未界说输出文件名则相沿输入文件名 
  20.  outputFileName = os.path.splitext(os.path.basename(sourceFilePath))[0] + '.html' 
  21.  if destinationDirectory[-1] != '/': 
  22.  destinationDirectory += '/' 
  23.  with open(sourceFilePath,'r', encoding='utf8') as f: 
  24.  markdownText = f.read() 
  25.  # 编译出原始 HTML 文本 
  26.  rawHtml = self.headTag + markdown.markdown(markdownText,output_format='html5') 
  27.  # 名目化 HTML 文本为可读性更强的名目 
  28.  beautifyHtml = BeautifulSoup(rawHtml,'html5lib').prettify() 
  29.  with open(destinationDirectory + outputFileName, 'w', encoding='utf8') as f: 
  30.  f.write(beautifyHtml) 
  31. if __name__ == "__main__": 
  32.  mth = MarkdownToHtml() 
  33.  # 做一个呼吁行参数列表的浅拷贝,不包括剧本文件名 
  34.  argv = sys.argv[1:] 
  35.  # 今朝列表 argv 也许包括源文件路径之外的元素(即选项信息) 
  36.  # 措施最后遍历列表 argv 举办编译 markdown 时,列表中的元素必需所有是源文件路径 
  37.  outputDirectory = None 
  38.  if '-s' in argv: 
  39.  cssArgIndex = argv.index('-s') +1 
  40.  cssFilePath = argv[cssArgIndex] 
  41.  # 检测样式表文件路径是否有用 
  42.  if not os.path.isfile(cssFilePath): 
  43.  print('Invalid Path: '+cssFilePath) 
  44.  sys.exit() 
  45.  mth.genStyle(cssFilePath) 
  46.  # pop 次序不能随意变革 
  47.  argv.pop(cssArgIndex) 
  48.  argv.pop(cssArgIndex-1) 
  49.  if '-o' in argv: 
  50.  dirArgIndex = argv.index('-o') +1 
  51.  outputDirectory = argv[dirArgIndex] 
  52.  # 检测输出目次是否有用 
  53.  if not os.path.isdir(outputDirectory): 
  54.  print('Invalid Directory: ' + outputDirectory) 
  55.  sys.exit() 
  56.  # pop 次序不能随意变革 
  57.  argv.pop(dirArgIndex) 
  58.  argv.pop(dirArgIndex-1) 
  59.  # 至此,列表 argv 中的元素均是源文件路径 
  60.  # 遍历全部源文件路径 
  61.  for filePath in argv: 
  62.  # 判定文件路径是否有用 
  63.  if os.path.isfile(filePath): 
  64.  mth.markdownToHtml(filePath, outputDirectory) 
  65.  else: 
  66.  print('Invalid Path: ' + filePath) 

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读