def to_event(self): with open(self.mdfile, encoding="utf-8-sig") as f: post = Frontmatter.read(f.read()) if len(post["body"]) == 0 or len(post["frontmatter"]) == 0: raise Exception("Error reading {} or incorrect format.".format( self.mdfile)) soup = BeautifulSoup(markdown.markdown(post["body"]), "html.parser") first_element = soup.find() try: if first_element.name != "h1": raise Exception( 'Expected first element to be an "H1" instead it was {}'. format(first_element.name)) except Exception as e: raise Exception( "Failed to parse heading from first element: {}".format( first_element), e, ) first_element.extract() event = post["attributes"] caption_elt = BeautifulSoup( markdown.markdown(event["media"]["caption"]), "html.parser") credit_elt = BeautifulSoup(markdown.markdown(event["media"]["credit"]), "html.parser") event["media"]["caption"] = self.get_inner_html(caption_elt.p) event["media"]["credit"] = self.get_inner_html(credit_elt.p) event["text"] = {"headline": first_element.text, "text": str(soup)} return event
def geninfo(dir,file): node=Frontmatter.read(open(dir+file,encoding='utf-8').read()) return { **{ 'id':None, 'origin_addr':file[0:len(file)-3], 'addr':'/'+file[0:len(file)-3], 'link':None, 'pre_link': None, 'nxt_link': None, 'pre_title': None, 'nxt_title': None, 'title': file[0:len(file)-3], 'date': time.strftime('%Y-%m-%d %H:%M',time.localtime(os.stat(dir+file).st_mtime)), 'author': config['author'], 'tags':[], 'categories':[], 'top':0, 'content':md.gen(node['body']), 'preview':md.gen(node['body'][0:min(len(node['body']),config['preview_len'])]) }, **t_setting['defaut_front'], **del_none(node['attributes']) }
def parse(cls, text): return cls(Frontmatter.read(text))
import os import sys import time import shutil import json import yaml import random import math from frontmatter import Frontmatter list = os.listdir('.') for i in list: if 'уБо' not in i: continue node = Frontmatter.read(open(i, encoding='utf-8').read()) # x={**node['attributes'],**{'permalink':node['attributes']['title'].strip()}} x = node['attributes'] open(i, "w", encoding='utf-8').write('---\n' + yaml.dump(x, allow_unicode=True) + '---\n' + node['body'])
def geninfo(dir, file): # 获取文件信息 node = Frontmatter.read(open(dir + file, encoding='utf-8').read()) info = node['attributes'] article = "" if ("body" in node and node["body"] != None): article = node["body"] if (info != None and "thumbnail" in info and info["thumbnail"] != None): thumbnail = info["thumbnail"] else: thumbnail = randomimg() if (info != None and "title" in info and info["title"] != None): title = info["title"] else: title = file[0:len(file) - 3] # print(title) if (info != None and "tags" in info and info["tags"] != None): tags = info["tags"] else: tags = [] if (info != None and "categories" in info and info["categories"] != None): categories = info["categories"] else: categories = [] if (info != None and "author" in info and info["author"] != None): author = info["author"] else: author = config["author"] if (info != None and "date" in info and info["date"] != None): date = str(info["date"]) else: date = time.strftime('%Y-%m-%d %H:%M', time.localtime(os.stat(dir + file).st_mtime)) top = 0 if (info != None and "top" in info and info["top"] != None): top = info["top"] istop = "" if (top > 0): istop = "[置顶]" comment = comment_mb if (("comment" in config and config["comment"] == False) or (info != None and "comment" in info and info["comment"] == 0)): comment = "评论已关闭" html = "" if (info != None and "html" in info and info["html"] != None): html = info["html"] return { "id": None, "title": title, "author": author, "thumbnail": thumbnail, "article": article, "preview": article[0:config["preview_len"]], "link": None, "categories": categories, "tags": tags, "tagshtml": turn_tag_to_html(tags), "top": top, "istop": istop, "date": date, "comment": comment, "pre_link": None, "pre_title": None, "nxt_link": None, "nxt_title": None, "html": html }