Last active
May 16, 2018 12:56
-
-
Save cloudchou/d351ab3c5f8138808e1775d23611ba19 to your computer and use it in GitHub Desktop.
新建博客文章的脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
import sublime | |
import sublime_plugin | |
import datetime | |
import time | |
import functools | |
import re | |
import codecs | |
import os | |
import sys | |
import string | |
# reload(sys) | |
# sys.setdefaultencoding('utf-8') # 允许打印unicode字符 | |
blogDir = 'E:\\git\\cloudchou.github.io' | |
postDir = blogDir + '\\_posts' | |
class NewBlogCommand(sublime_plugin.WindowCommand): | |
def run(self): | |
nowTimeStr = datetime.datetime.now().strftime('%Y-%m-%d') | |
initFileName = nowTimeStr + '-*.md' | |
doneFunc = functools.partial(self.on_input) | |
self.window.show_input_panel( | |
"Blog File Name", initFileName, doneFunc, None, None) | |
def get_file_content(self, filename): | |
f = codecs.open(filename, 'r', 'utf-8') | |
s = f.readlines() | |
f.close() | |
return s | |
def write_file_content(self, filepath, content, append): | |
if append: | |
f = codecs.open(filepath, 'a+', 'utf-8') | |
else: | |
f = codecs.open(filepath, 'w', 'utf-8') | |
f.writelines(content) | |
f.close() | |
def getContentTemplate(self): | |
return ''' | |
--- | |
id: %d | |
title: %s | |
date: %s | |
author: cloud | |
layout: post | |
guid: http://www.cloudchou.com/?p=%d | |
permalink: /android/post-%d.html | |
categories: | |
- Android | |
tags: | |
- Android | |
--- | |
''' | |
def getMaxPostId(self, postDir): | |
files = os.listdir(postDir) | |
maxPostId = 1 | |
maxMetaFileContent = '' | |
for filename in files: | |
filepath = postDir + "\\" + filename | |
fileContent = ''.join(self.get_file_content(filepath)) | |
flags = re.M | re.S | |
pattern = r'(---.*---).*' | |
metaFileContent = re.sub( | |
pattern, r'\1', fileContent, flags=flags) | |
idStr = re.sub( | |
r'.*id:\s*(\d+).*', r'\1', metaFileContent, flags=flags) | |
id = int(idStr) | |
if id > maxPostId: | |
maxPostId = id | |
maxMetaFileContent = metaFileContent | |
# print('max id file: \n'+maxMetaFileContent) | |
return maxPostId | |
def on_input(self, message): | |
print("new file name: "+message) | |
m = re.match(r'\d+-\d+-\d+-(.*)\.md', message) | |
if not m: | |
sublime.error_message('文件名不正确,没有包含正确的标题') | |
return | |
title = m.group(1) | |
# 2013-12-26T23:40:06+08:00 | |
nowDateTime = datetime.datetime.now().strftime( | |
'%Y-%m-%dT%H:%M:%S+08:00') | |
maxId = self.getMaxPostId(postDir) | |
newId = maxId+1 | |
contentTemplate = self.getContentTemplate() | |
content = contentTemplate % (newId, title, nowDateTime, newId, newId) | |
content = re.sub(r' {8}', r'', content) | |
content = re.sub(r'^\n', r'', content) | |
# print('bbbb'+content) | |
filePath = postDir+'/'+message | |
self.write_file_content(filePath, content, False) | |
self.window.open_file(filePath) | |
class BlogOpenListener(sublime_plugin.EventListener): | |
def get_file_content(self, filename): | |
f = codecs.open(filename, 'r', 'utf-8') | |
s = f.readlines() | |
f.close() | |
return s | |
def on_load(self, view): | |
if not view.file_name().startswith(postDir): | |
# print('file name not start with post dir: '+view.file_name()) | |
return | |
if not view.file_name().endswith(".md"): | |
# print('file name not end with *.md: '+view.file_name()) | |
return | |
fileContent = ''.join(self.get_file_content(view.file_name())) | |
flags = re.M | re.S | |
pattern = r'(---.*---)(.*)' | |
blogContent = re.sub( | |
pattern, r'\2', fileContent, flags=flags) | |
# print('blog content: '+blogContent+'---') | |
# if blogContent.strip() == '': | |
# print('blog content is empty. move cursor to end') | |
# # view.run_command( | |
# # "move", {"by": "characters", "forward": True, "amount": view.size()}) | |
# view.run_command("move_to", {"to": "eof", "extend": True}) | |
# # { "keys": ["ctrl+down"], "command": "scroll_lines", "args": {"amount": -1.0 } }, | |
# view.run_command("scroll_lines", {"amount": 8.0}) | |
# self.get_window().get_active_view().run_command( | |
# "move_to", {"to": "eof", "extend": false}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment