Skip to content
This repository has been archived by the owner on May 29, 2021. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yue committed Aug 25, 2018
0 parents commit 8075263
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.idea
build
example/
build/
test/
*/__pycache__
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Beryl

a tiny engine to convert plain(no template syntax) vue into static none js pages

### dependency:

+ sass
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ENTRY": "../test/",
"OUTPUT": "../build/",
"COMPONENTS": "components/",
"PAGES": "views/",
"SCSS": "scss/"
}
64 changes: 64 additions & 0 deletions lib/component.py
Original file line number Diff line number Diff line change
@@ -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('<template>([\s\S]+?)</template>')
self.__style_reg = re.compile('<style[\s\S]+?>([\s\S]+?)</style>')
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"<div {classes}> {inside}</div>"
# print(inner_html)

return inner_html
24 changes: 24 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
@@ -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')
83 changes: 83 additions & 0 deletions lib/engine.py
Original file line number Diff line number Diff line change
@@ -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['<PageFooter/>'].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()
57 changes: 57 additions & 0 deletions lib/page.py
Original file line number Diff line number Diff line change
@@ -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('<title>([\s\S]+?)</title>')
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('<div id="app"></div>',
self.engine.base_component.template)
self.resolve()

self.css_link = f'<link href="./statics/css/{self.title}.css" rel="stylesheet"/>'

self.output_html = re.sub(self.title_reg,
lambda x: f'''<title>{self.title}</title>
{self.css_link}
''',
self.output_html)
self.output_html = self.output_html.replace('<router-view/>', 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)

0 comments on commit 8075263

Please sign in to comment.