加入收藏 | 设为首页 | 会员中心 | 我要投稿 好传媒网 (https://www.haochuanmei.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

8 个 Python 实用脚本,收藏备用

发布时间:2019-10-16 04:38:16 所属栏目:优化 来源:实验楼
导读:脚本写的好,下班下得早!程序员的日常工作除了编写程序代码,还不可避免地需要处理相关的测试和验证工作。 例如,访问某个网站一直不通,需要确定此地址是否可访问,服务器返回什么,进而确定问题在于什么。完成这个任务,如果一味希望采用编译型语言来编

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) 

(编辑:好传媒网)

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

热点阅读