import datetime
import json
import os.path
import re
import shutil

from pypinyin import lazy_pinyin

from common import file, console
from manage import get_excerpt

system_info = json.loads(file.read_file("./config/system.json"))


def get_name(nameinput):
    name_raw = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——！，。？、~@#￥%……&*（）]+", "", nameinput)
    namelist = lazy_pinyin(name_raw)
    name = ""
    for item in namelist:
        name = name + "-" + item
    return name[1:len(name)]


def new_post_init(config, editor,independent=False):
    if config is not None:
        config = json.loads(config)
        title = config["title"]
        name = config["name"]
        filename = config["file"]
    else:
        title = input("Please enter the title of the article:")
        name = input("Please enter the URL (Leave a blank use pinyin):")
        filename = None

    if len(name) == 0:
        name = get_name(title)

    if filename is not None and os.path.exists(filename):
        shutil.copyfile(filename, "./document/{0}.md".format(name))

    if config is None:
        # Check if the editor configuration exists
        if editor is None:
            editor = "vim"
            if "Editor" in system_info:
                editor = system_info["Editor"]
        os.system("{0} ./document/{1}.md".format(editor, name))
    excerpt = get_excerpt.get_excerpt("./document/{0}.md".format(name))
    post_info = {"name": name, "title": title, "excerpt": excerpt, "time": str(datetime.date.today())}
    write_json=post_info
    page_config="./document/{0}.json".format(name)
    if not independent:
        write_json = json.loads(file.read_file("./config/page.json"))
        write_json.insert(0, post_info)
        page_config="./config/page.json"

    file.write_file(page_config, json.dumps(write_json, ensure_ascii=False))
    console.log("Success","Create a new article successfully!","green")

@@ -1322,24 +1322,136 @@
 tor, name))%0A
+%0A    post_info = %7B%22name%22: name, %22title%22: title, %22time%22: str(datetime.date.today())%7D%0A    if not independent:%0A    
     excerpt 
@@ -1506,24 +1506,28 @@
 rmat(name))%0A
+    
     post_inf
@@ -1531,42 +1531,9 @@
 info
- = %7B%22name%22: name, %22title%22: title, 
+%5B
 %22exc
@@ -1541,17 +1541,19 @@
 rpt%22
-:
+%5D =
  excerpt
 , %22t
@@ -1552,45 +1552,9 @@
 erpt
-, %22time%22: str(datetime.date.today())%7D
+%0A
 %0A   
@@ -1564,17 +1564,19 @@
 ite_json
-=
+ = 
 post_inf
@@ -1592,17 +1592,19 @@
 e_config
-=
+ = 
 %22./docum
@@ -1783,17 +1783,19 @@
 e_config
-=
+ = 
 %22./confi
@@ -1876,17 +1876,19 @@
 re_ascii
-=
+ = 
 False))%0A

修改摘要部分实现，避免多余数据