def generate_pages_by_file(): """Generates custom pages of 'file' storage type.""" from veripress import app from veripress.model import storage from veripress.model.parsers import get_standard_format_name from veripress.helpers import traverse_directory deploy_dir = get_deploy_dir() def copy_file(src, dst): makedirs(os.path.dirname(dst), mode=0o755, exist_ok=True) shutil.copyfile(src, dst) with app.app_context(), app.test_client() as client: root_path = os.path.join(app.instance_path, 'pages') for path in traverse_directory(root_path): rel_path = os.path.relpath(path, root_path) # e.g. 'a/b/c/index.md' filename, ext = os.path.splitext(rel_path) # e.g. ('a/b/c/index', '.md') if get_standard_format_name(ext[1:]) is not None: # is source of custom page rel_url = filename.replace(os.path.sep, '/') + '.html' # e.g. 'a/b/c/index.html' page = storage.get_page(rel_url, include_draft=False) if page is not None: # it's not a draft, so generate the html page makedirs(os.path.join(deploy_dir, os.path.dirname(rel_path)), mode=0o755, exist_ok=True) with open(os.path.join(deploy_dir, filename + '.html'), 'wb') as f: f.write(client.get('/' + rel_url).data) if app.config['PAGE_SOURCE_ACCESSIBLE']: copy_file(path, os.path.join(deploy_dir, rel_path)) else: # is other direct files copy_file(path, os.path.join(deploy_dir, rel_path))
def do_generate(): from veripress import app from veripress.model import storage deploy_dir = get_deploy_dir() # copy global static folder dst_static_folder = os.path.join(deploy_dir, 'static') if os.path.isdir(app.static_folder): shutil.copytree(app.static_folder, dst_static_folder) # copy theme static files makedirs(dst_static_folder, mode=0o755, exist_ok=True) copy_folder_content(app.theme_static_folder, dst_static_folder) # collect all possible urls (except custom pages) all_urls = {'/', '/feed.xml', '/atom.xml', '/archive/'} with app.app_context(): posts = list(storage.get_posts(include_draft=False)) index_page_count = int(math.ceil(len(posts) / app.config['ENTRIES_PER_PAGE'])) for i in range(2, index_page_count + 1): # ignore '/page/1/', this will be generated separately later all_urls.add('/page/{}/'.format(i)) for post in posts: all_urls.add(post.unique_key) all_urls.add('/archive/{}/'.format(post.created.strftime('%Y'))) all_urls.add('/archive/{}/{}/'.format(post.created.strftime('%Y'), post.created.strftime('%m'))) tags = storage.get_tags() for tag_item in tags: all_urls.add('/tag/{}/'.format(tag_item[0])) categories = storage.get_categories() for category_item in categories: all_urls.add('/category/{}/'.format(category_item[0])) with app.test_client() as client: # generate all possible urls for url in all_urls: resp = client.get(url) file_path = os.path.join(get_deploy_dir(), url.lstrip('/').replace('/', os.path.sep)) if url.endswith('/'): file_path += 'index.html' makedirs(os.path.dirname(file_path), mode=0o755, exist_ok=True) with open(file_path, 'wb') as f: f.write(resp.data) # generate 404 page resp = client.get('/post/this-is-a-page-that-never-gonna-exist' '-because-it-is-a-post-with-wrong-url-format/') with open(os.path.join(deploy_dir, '404.html'), 'wb') as f: f.write(resp.data) if app.config['STORAGE_TYPE'] == 'file': generate_pages_by_file()
def setup_command(repo, name, email): user, repo = repo from veripress_cli.generate import get_deploy_dir deploy_dir = get_deploy_dir() makedirs(deploy_dir, mode=0o755, exist_ok=True) os.system('git -C {} init'.format(deploy_dir)) os.system('git -C {} config user.email "{}"'.format(deploy_dir, email)) os.system('git -C {} config user.name "{}"'.format(deploy_dir, name)) os.system('git -C {} remote add origin [email protected]:{}/{}.git'.format( deploy_dir, user, repo))
def deploy_command(): from veripress_cli.generate import get_deploy_dir deploy_dir = get_deploy_dir() makedirs(deploy_dir, mode=0o755, exist_ok=True) os.system('git -C {} add .'.format(deploy_dir)) if os.system('git -C {} diff --quiet --exit-code'.format(deploy_dir)) == 0 \ and os.system('git -C {} diff --quiet --cached --exit-code'.format(deploy_dir)) == 0: click.echo('There are no changes to be deployed.') return dt = datetime.now() os.system('git -C {} commit -m "Updated on {} at {}"'.format( deploy_dir, dt.strftime('%Y-%m-%d'), dt.strftime('%H:%M:%S'))) os.system('git -C {} push origin master'.format(deploy_dir))
def copy_file(src, dst): makedirs(os.path.dirname(dst), mode=0o755, exist_ok=True) shutil.copyfile(src, dst)