diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2020-07-11 00:50:00 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2020-07-11 00:50:00 +0200 |
| commit | 91e8fdb99e321496c54288fe5a3db6397c768c10 (patch) | |
| tree | 2bb82323466c7895eb705138e561278b02ce8ca2 /cli/commands/site/export.py | |
| parent | 2001ddd7e2a8926ec97fdd4d5c73b2d4e5b293de (diff) | |
building basic site. need to include cursors, etc
Diffstat (limited to 'cli/commands/site/export.py')
| -rw-r--r-- | cli/commands/site/export.py | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/cli/commands/site/export.py b/cli/commands/site/export.py index 8212f55..c8e687a 100644 --- a/cli/commands/site/export.py +++ b/cli/commands/site/export.py @@ -1,6 +1,8 @@ import click from app.settings import app_cfg +from app.utils.file_utils import load_text, write_json, write_text +from os.path import join @click.command('info') @click.option('-g', '--graph', 'opt_graph_path', required=True, @@ -14,12 +16,10 @@ def cli(ctx, opt_graph_path, opt_output_dir): # ------------------------------------------------ # imports - from os.path import join - from app.sql.common import db, Session, Graph, Page, Tile # ------------------------------------------------ - # generate HTML for all pages + # generate HTML for index and all pages session = Session() graph = session.query(Graph).filter(Graph.path == opt_graph_path).first() @@ -27,12 +27,76 @@ def cli(ctx, opt_graph_path, opt_output_dir): print(f"Not a graph: {opt_graph_path}") return + print(f"Output site to {opt_output_dir}") + + site_data = { 'graph': sanitize_graph(graph.toSiteJSON()) } + + index_html = load_text(join(app_cfg.DIR_STATIC, 'site.html'), split=False) + write_json(site_data, join(opt_output_dir, graph.path, 'index.json'), default=str) + write_index(graph, None, index_html, join(opt_output_dir, graph.path, 'index.html')) + + index_path = "" for page in graph.pages: page_path = f'{graph.path}/{page.path}' if page.id == graph.home_page_id: + index_path = page_path print(f'/{page_path} [index]') else: print(f'/{page_path}') - # + write_index(graph, page, index_html, join(opt_output_dir, graph.path, page.path, 'index.html')) + # ------------------------------------------------ - # cat all the relevant CSS from the main site + # generate javascript... + + # NODE_ENV=production webpack --config ./webpack.config.site.js -o ./data_store/exports/asdf/bundle.js + +def write_index(graph, page, index_html, fp_out): + if page is None: + page_title = graph.title + else: + page_title = page.title + index_html = index_html.replace('BUNDLE_PATH', join('/', graph.path, 'bundle.js')) + index_html = index_html.replace('PAGE_TITLE', page_title) + write_text(index_html, fp_out) + +def sanitize_graph(graph): + page_path_lookup = {} + page_lookup = {} + for page in graph['pages']: + page_path_lookup[page['id']] = join('/', graph['path'], page['path']) + for page in graph['pages']: + sanitize_page(page) + for tile in page['tiles']: + if tile['target_page_id']: + if tile['target_page_id'] == -1: + tile['href'] = tile['external_link_url'] + elif tile['target_page_id'] > 0: + tile['href'] = page_path_lookup[tile['target_page_id']] + sanitize_tile(tile) + page_path = page_path_lookup[page['id']] + page_lookup[page_path] = page + graph['pages'] = page_lookup + graph['home_page'] = page_path_lookup[graph['home_page_id']] + return graph + +def sanitize_page(data): + if 'created_at' in data: + del data['created_at'] + if 'updated_at' in data: + del data['updated_at'] + if 'graph_id' in data: + del data['graph_id'] + +def sanitize_tile(data): + if 'created_at' in data: + del data['created_at'] + if 'updated_at' in data: + del data['updated_at'] + if 'username' in data: + del data['username'] + if 'graph_id' in data: + del data['graph_id'] + if 'page_id' in data: + del data['page_id'] + if 'target_page_id' in data: + del data['target_page_id'] |
