def create_post(self, url): """ Create new post file. Get url name of new post. :type url: str """ time = datetime.now(tz=conf.TIME_ZONE) folder = conf.POST_PATH_STYLE.format(date=time) fname = '{date.year}-{date.month}-{date.day}-{name}.md'.format( date=time, name=url) path = filejoin(conf.POST_PATH, folder, fname) try: os.makedirs(filejoin(conf.POST_PATH, folder)) except OSError: pass print('# creating new post..') print('# file: {0} '.format(path)) with open(path, 'w', encoding='utf8') as f: f.write('# title: {0}\n'.format(url)) f.write('# url: {0}\n'.format(url)) f.write('# categories: []\n') f.write('# tags: []\n') f.write('# time: {0}\n'.format(time)) f.write('\n![left](~logo.png)\nHello world\n') f.write('\n[more]\n\nend')
def copy_static(self): """ Make link to static directory """ template = os.path.abspath(filejoin(conf.TEMPLATE_PATH, 'static')) deploy = filejoin(conf.DEPLOY_PATH, 'static') if not os.path.exists(conf.DEPLOY_PATH): os.makedirs(conf.DEPLOY_PATH) if not os.path.lexists(deploy): print('# Deploy static') shutil.copytree(template, deploy)
def load(self, pattern=None): """ Load all markdown files to memory """ if pattern: for path in glob(filejoin(conf.POST_PATH, pattern + '.md')): self.posts.append(self.reader.read(path)) else: for root, folder, files in os.walk(conf.POST_PATH): for name in files: if name.endswith('.md'): path = filejoin(root, name) self.posts.append(self.reader.read(path)) self.posts.sort(key=lambda x: x.meta.time, reverse=True)
def write(self, posts): """ Make links to resources """ for post in posts: if post.resources: folder = os.path.dirname(post.path) for res in post.resources: r = os.path.abspath(filejoin(folder, res.link)) if os.path.exists(r): self.mkdir(post.furl, os.path.dirname(res.link)) s = filejoin(conf.DEPLOY_PATH, post.furl, res.link) if not os.path.lexists(s): try: os.symlink(r, s) except: shutil.copyfile(r, s) else: print('#! No resource found: {0}'.format(r))
def gzip_content(self): content = [] for root, folder, files in os.walk(conf.DEPLOY_PATH): for name in files: if any(name.endswith(i) for i in ('.js', '.css', '.html')): path = filejoin(root, name) content.append(path) for fname in content: with open(fname, 'rb') as fin: with gzip.open(fname + '.gz', 'wb') as fout: fout.writelines(fin)
def mkdir(self, url, *args): try: os.makedirs(filejoin(conf.DEPLOY_PATH, url, *args)) except OSError: pass
def open(self, path, mode='w'): self.mkdir(os.path.dirname(path)) return open(filejoin(conf.DEPLOY_PATH, path), mode=mode, encoding='utf8')
def write(self, posts): self.render(filejoin('tags', 'index.html'), 'tags.html')
def write(self, posts): for post in posts: self.render(filejoin(post.furl, 'index.html'), 'post.html', post=post)
def write(self, posts): path = filejoin(conf.DEPLOY_PATH, 'robots.txt') with open(path, mode='w', encoding='utf8') as f: f.write(conf.ROBOTS_TXT)