From 8075263b15fc59ee17d5d348e341414c358d065f Mon Sep 17 00:00:00 2001 From: yue Date: Sun, 26 Aug 2018 05:53:31 +0900 Subject: [PATCH] initial commit --- .gitignore | 6 ++++ README.md | 7 ++++ config.json | 7 ++++ lib/component.py | 64 +++++++++++++++++++++++++++++++++++++ lib/config.py | 24 ++++++++++++++ lib/engine.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/page.py | 57 +++++++++++++++++++++++++++++++++ 7 files changed, 248 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 config.json create mode 100644 lib/component.py create mode 100644 lib/config.py create mode 100644 lib/engine.py create mode 100644 lib/page.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..faae5a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea +build +example/ +build/ +test/ +*/__pycache__ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0cefb24 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Beryl + + a tiny engine to convert plain(no template syntax) vue into static none js pages + +### dependency: + ++ sass diff --git a/config.json b/config.json new file mode 100644 index 0000000..ebc1111 --- /dev/null +++ b/config.json @@ -0,0 +1,7 @@ +{ + "ENTRY": "../test/", + "OUTPUT": "../build/", + "COMPONENTS": "components/", + "PAGES": "views/", + "SCSS": "scss/" +} diff --git a/lib/component.py b/lib/component.py new file mode 100644 index 0000000..45d420c --- /dev/null +++ b/lib/component.py @@ -0,0 +1,64 @@ +import re + + +class Component(object): + def __init__(self, filename, engine): + with open(filename, 'r') as f: + self.file = f.read() + + self.name = filename.split('/')[-1] + + self.__template_reg = re.compile('') + self.__style_reg = re.compile('([\s\S]+?)') + self.__classname_reg = re.compile('(class="[\s\S]+?")/>') + + self.template = re.findall(self.__template_reg, self.file)[0] # .group() + self.style = re.findall(self.__style_reg, self.file)[0] # .group() + self.engine = engine + self.output_html = '' + self.output_style = '' + self.style_set = [] + + def resolve(self): + for nested_name in self.engine.components: + + ori_reg = nested_name.replace('/>', '[\s\S]*?/>') + + nested = re.findall(ori_reg, self.template) + + if nested: + print(nested_name, ' in ', self.name) + self.update(nested[0], + self.engine.components[nested_name].template) + + if nested_name not in self.style_set: + # self.style = self.style + self.engine.components[nested_name].style + # self.style_set.append(nested_name) + self.style_set.extend(self.engine.components[nested_name].style_set) + self.style_set.append(nested_name) + + self.style_set = list(set(self.style_set)) + + + def update(self, origin, inside): + + classes = re.findall(self.__classname_reg, origin) + + # print(classes) + + inner_html = self.make_div(classes, inside) + + self.template = self.template.replace(origin, inner_html) + + @staticmethod + def make_div(classes, inside): + # print(el) + # inner_html + if not classes: + classes = '' + else: + classes = classes[0] + inner_html = f"
{inside}
" + # print(inner_html) + + return inner_html diff --git a/lib/config.py b/lib/config.py new file mode 100644 index 0000000..198b05a --- /dev/null +++ b/lib/config.py @@ -0,0 +1,24 @@ +import json + + +class Config(object): + def __init__(self): + self.config_path = '../config.json' + + self.base = 'App.vue' + + self.source_path = '' + self.out_path = '' + self.scss_path = '' + self.pages_path = '' + self.components_path = '' + + def load(self): + with open(self.config_path, 'r') as cnf: + __config = json.loads(cnf.read()) + + self.source_path = __config.get('ENTRY') + self.out_path = __config.get('OUTPUT') + self.pages_path = self.source_path + __config.get('PAGES') + self.components_path = self.source_path + __config.get('COMPONENTS') + self.scss_path = self.source_path + __config.get('SCSS') diff --git a/lib/engine.py b/lib/engine.py new file mode 100644 index 0000000..5195249 --- /dev/null +++ b/lib/engine.py @@ -0,0 +1,83 @@ +import os +from component import Component +from page import Page +from config import Config + + +class Engine(object): + def __init__(self): + + self.base_html = '' + self.base_component = None + self.config = Config() + self.components = {} + self.pages = {} + + def run(self): + self.config.load() + + print('config loaded') + + with open(self.config.source_path + 'index.html', 'r') as base: + self.base_html = base.read() + + self.base_component = Component(self.config.source_path + 'App.vue', self) + + self.register_components() + print('components registered') + self.resolve_nested_components() + self.base_component.resolve() + self.make_dirs() + + self.make_pages() + + print('pages made') + self.compile_sass() + print('sass compiled') + print('done') + + 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/js') + os.mkdir('../build/statics/css') + os.system(f'cp -r {self.config.scss_path} ../build/statics/scss') + + def test(self): + self.run() + # print(self.components[''].style) + # print(self.pages['About'].style) + + def compile_sass(self): + os.chdir(self.config.out_path + 'statics') + os.system('sass --update scss:css --style compressed') + + def register_components(self): + components = [file for file in os.listdir(self.config.components_path) + if file.endswith('.vue')] + [self.register_component(component) for component in components] + + def register_component(self, filename): + name = filename.rsplit('.')[0] + self.components[f"<{name}/>"] = Component(self.config.components_path + filename, self) + + def resolve_nested_components(self): + for component in self.components: + self.components[component].resolve() + + def make_pages(self): + pages = [file for file in os.listdir(self.config.pages_path) + if file.endswith('.vue')] + for page in pages: + name = page.rsplit('.')[0] + self.pages[name] = Page(self.config.pages_path + page, self) + + for page in self.pages: + self.pages[page].make() + + +if __name__ == '__main__': + engine = Engine() + engine.test() diff --git a/lib/page.py b/lib/page.py new file mode 100644 index 0000000..8fd8972 --- /dev/null +++ b/lib/page.py @@ -0,0 +1,57 @@ +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)