From 0db068560c24544f967341085126d5a305f535d5 Mon Sep 17 00:00:00 2001 From: yue Date: Wed, 29 Aug 2018 01:52:31 +0900 Subject: [PATCH] merge Component and Page; add clean --- .gitignore | 4 +++- config.json | 2 +- lib/component.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++- lib/engine.py | 16 +++++++++---- lib/page.py | 57 ---------------------------------------------- 5 files changed, 73 insertions(+), 65 deletions(-) delete mode 100644 lib/page.py diff --git a/.gitignore b/.gitignore index faae5a9..b2f6c0f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ build example/ build/ test/ -*/__pycache__ \ No newline at end of file +*/__pycache__ +src +public \ No newline at end of file diff --git a/config.json b/config.json index ebc1111..3d08b24 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "ENTRY": "../test/", + "ENTRY": "../src/", "OUTPUT": "../build/", "COMPONENTS": "components/", "PAGES": "views/", diff --git a/lib/component.py b/lib/component.py index 45d420c..7602d65 100644 --- a/lib/component.py +++ b/lib/component.py @@ -1,4 +1,5 @@ import re +import os class Component(object): @@ -39,7 +40,6 @@ def resolve(self): self.style_set = list(set(self.style_set)) - def update(self, origin, inside): classes = re.findall(self.__classname_reg, origin) @@ -62,3 +62,60 @@ def make_div(classes, inside): # print(inner_html) return inner_html + + +class Page(Component): + def __init__(self, page_name, engine): + super().__init__(page_name, engine) + self.base_html = engine.base_html + self.engine = engine + self.title_reg = re.compile('([\s\S]+?)') + self.import_reg = re.compile('@import[\s\S]+?scss";') + self.title = self.name.rsplit('.', 1)[0].lower() + self.css_link = '' + + def make(self): + + print(f'making {self.title}') + self.make_html() + self.make_style() + self.write() + + def make_html(self): + self.output_html = self.base_html.replace('
', + self.engine.base_component.template) + self.resolve() + + self.css_link = f'' + + self.output_html = re.sub(self.title_reg, + lambda x: f'''{self.title} + {self.css_link} + ''', + self.output_html) + self.output_html = self.output_html.replace('', self.template) + + def make_style(self): + self.output_style += self.engine.base_component.style + + for each in self.engine.base_component.style_set: + self.output_style += self.engine.components[each].style + + self.output_style += self.style + scss_imports = re.findall(self.import_reg, self.output_style) + scss_imports = list(set(scss_imports)) + for each in scss_imports: + self.output_style = re.sub(each, '', self.output_style) + self.output_style = '\n'.join(scss_imports) + self.output_style + + def write(self): + html_file_name = self.engine.config.out_path + self.title + scss_file_name = self.engine.config.out_path + '/statics/scss/' + self.title + + os.mkdir(html_file_name) + + with open(html_file_name + '/index.html', 'w') as html: + html.write(self.output_html) + + with open(scss_file_name + '.scss', 'w') as scss: + scss.write(self.output_style) diff --git a/lib/engine.py b/lib/engine.py index 5195249..08b1b78 100644 --- a/lib/engine.py +++ b/lib/engine.py @@ -1,6 +1,5 @@ import os -from component import Component -from page import Page +from component import Component, Page from config import Config @@ -14,11 +13,12 @@ def __init__(self): self.pages = {} def run(self): + self.clean() self.config.load() print('config loaded') - with open(self.config.source_path + 'index.html', 'r') as base: + with open(self.config.source_path.replace('src', 'public') + 'index.html', 'r') as base: self.base_html = base.read() self.base_component = Component(self.config.source_path + 'App.vue', self) @@ -40,9 +40,10 @@ def make_dirs(self): if not os.path.isdir('../build'): os.mkdir('../build') os.mkdir('../build/statics') - os.mkdir('../build/statics/img') + # os.mkdir('../build/statics/imgs') os.mkdir('../build/statics/js') os.mkdir('../build/statics/css') + os.system(f'cp -r {self.config.source_path+"statics/imgs"} ../build/statics/imgs') os.system(f'cp -r {self.config.scss_path} ../build/statics/scss') def test(self): @@ -50,7 +51,12 @@ def test(self): # print(self.components[''].style) # print(self.pages['About'].style) + def clean(self): + os.system('rm -rf ../build/') + print('cleared up') + def compile_sass(self): + os.chdir(self.config.out_path + 'statics') os.system('sass --update scss:css --style compressed') @@ -80,4 +86,4 @@ def make_pages(self): if __name__ == '__main__': engine = Engine() - engine.test() + engine.run() diff --git a/lib/page.py b/lib/page.py deleted file mode 100644 index 8fd8972..0000000 --- a/lib/page.py +++ /dev/null @@ -1,57 +0,0 @@ -from component import Component -import re - - -class Page(Component): - def __init__(self, page_name, engine): - super().__init__(page_name, engine) - self.base_html = engine.base_html - self.engine = engine - self.title_reg = re.compile('([\s\S]+?)') - self.import_reg = re.compile('@import[\s\S]+?scss";') - self.title = self.name.rsplit('.', 1)[0] - self.css_link = '' - - def make(self): - - print(f'making {self.title}') - self.make_html() - self.make_style() - self.write() - - def make_html(self): - self.output_html = self.base_html.replace('
', - self.engine.base_component.template) - self.resolve() - - self.css_link = f'' - - self.output_html = re.sub(self.title_reg, - lambda x: f'''{self.title} - {self.css_link} - ''', - self.output_html) - self.output_html = self.output_html.replace('', self.template) - - def make_style(self): - self.output_style += self.engine.base_component.style - - for each in self.engine.base_component.style_set: - self.output_style += self.engine.components[each].style - - self.output_style += self.style - scss_imports = re.findall(self.import_reg, self.output_style) - scss_imports = list(set(scss_imports)) - for each in scss_imports: - self.output_style = re.sub(each, '', self.output_style) - self.output_style = '\n'.join(scss_imports) + self.output_style - - def write(self): - html_file_name = self.engine.config.out_path + self.title - scss_file_name = self.engine.config.out_path + '/statics/scss/' + self.title - - with open(html_file_name + '.html', 'w') as html: - html.write(self.output_html) - - with open(scss_file_name + '.scss', 'w') as scss: - scss.write(self.output_style)