def test_basic(self): pages = FlatPages(Flask(__name__)) hello = pages.get('headerid') self.assertEqual( hello.html, u'<h1>Page Header</h1>\n<h2>Paragraph Header</h2>\n<p>Text</p>' ) pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [] pages.reload() pages._file_cache = {} hello = pages.get('headerid') self.assertEqual( hello.html, u'<h1>Page Header</h1>\n<h2>Paragraph Header</h2>\n<p>Text</p>' ) pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [ 'codehilite', 'headerid' ] pages.reload() pages._file_cache = {} hello = pages.get('headerid') self.assertEqual( hello.html, '<h1 id="page-header">Page Header</h1>\n' '<h2 id="paragraph-header">Paragraph Header</h2>\n' '<p>Text</p>' )
def test_basic(self): pages = FlatPages(Flask(__name__)) hello = pages.get('headerid') self.assertEqual( hello.html, u'<h1>Page Header</h1>\n<h2>Paragraph Header</h2>\n<p>Text</p>') pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [] pages.reload() pages._file_cache = {} hello = pages.get('headerid') self.assertEqual( hello.html, u'<h1>Page Header</h1>\n<h2>Paragraph Header</h2>\n<p>Text</p>') pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [ 'codehilite', 'headerid' ] pages.reload() pages._file_cache = {} hello = pages.get('headerid') self.assertEqual( hello.html, '<h1 id="page-header">Page Header</h1>\n' '<h2 id="paragraph-header">Paragraph Header</h2>\n' '<p>Text</p>')
def test_extension_object(self): app = Flask(__name__) from markdown.extensions.codehilite import CodeHiliteExtension codehilite = CodeHiliteExtension() app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [codehilite] pages = FlatPages(app) self.check_default_codehilite_page(pages) codehilite = CodeHiliteExtension(linenums='True') #Check config applies app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [codehilite] pages.reload() pages._file_cache = {} self.check_codehilite_with_linenums(pages)
def test_extension_object(self): app = Flask(__name__) from markdown.extensions.codehilite import CodeHiliteExtension codehilite = CodeHiliteExtension() app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [codehilite] pages = FlatPages(app) self.check_default_codehilite_page(pages) codehilite = CodeHiliteExtension( linenums='True') #Check config applies app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [codehilite] pages.reload() pages._file_cache = {} self.check_codehilite_with_linenums(pages)
def test_extension_importpath(self): app = Flask(__name__) app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [ 'markdown.extensions.codehilite:CodeHiliteExtension' ] pages = FlatPages(app) self.check_default_codehilite_page(pages) app.config['FLATPAGES_EXTENSION_CONFIGS'] = { #Markdown 3 style config 'markdown.extensions.codehilite:CodeHiliteExtension': { 'linenums': True } } pages.reload() pages._file_cache = {} self.check_codehilite_with_linenums(pages)
def test_codehilite_linenums_disabled(self): #Test explicity disabled app = Flask(__name__) app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = ['codehilite'] pages = FlatPages(app) self.check_default_codehilite_page(pages) #Test explicity disabled pages.app.config['FLATPAGES_EXTENSION_CONFIGS'] = { 'codehilite': { 'linenums': 'False' } } pages.reload() pages._file_cache = {} self.check_default_codehilite_page(pages)
def test_headerid_with_toc(self): app = Flask(__name__) pages = FlatPages(app) pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [ 'codehilite', 'toc' #headerid is deprecated in Markdown 3.0 ] pages.reload() pages._file_cache = {} hello = pages.get('headerid') self.assertEqual( hello.html, '<h1 id="page-header">Page Header</h1>\n' '<h2 id="paragraph-header">Paragraph Header</h2>\n' '<p>Text</p>') self.check_default_codehilite_page(pages) #test codehilite also loaded
def page(path): page = pages.get_or_404(path) if request.method == "POST" and "user_id" in session: page_fullpath = (Path() / app.config["FLASK_APP"] / app.config["FLATPAGES_ROOT"] / secure_filename(page.path + ".md")) try: page_fullpath.unlink() FlatPages.reload(pages) flash(f"Blog post {page.path} has been deleted!", "Info") return redirect(url_for("index")) except OSError: abort(404) return render_template("page.j2", page=page)
def test_headerid_with_toc(self): app = Flask(__name__) pages = FlatPages(app) pages.app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [ 'codehilite', 'toc' #headerid is deprecated in Markdown 3.0 ] pages.reload() pages._file_cache = {} hello = pages.get('headerid') self.assertEqual( hello.html, '<h1 id="page-header">Page Header</h1>\n' '<h2 id="paragraph-header">Paragraph Header</h2>\n' '<p>Text</p>' ) self.check_default_codehilite_page(pages) #test codehilite also loaded
class CategorizedFlatPages(object): def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(None, '<root>') def init_app(self, app): self.flat_pages.init_app(app) self._set_categories() def __iter__(self): return iter(sorted(self.root_category.categories.values(), cmp=compare)) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category.articles.get(article_id) def get_articles_of_category(self, category_id): barticles = [] for a in self.root_category.categories.get( category_id).articles.values(): if a.id != 'index': barticles.append(a) return barticles def get_or_404(self, category_id, article_id): page = self.get(category_id, article_id) if page is None: abort(404) return page def _set_categories(self): for page in self.flat_pages: components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_category(category_id) page_name = components[-1] parent.add_article(page_name, page) def reload(self): self.flat_pages.reload() self._set_categories()
class CategorizedFlatPages(object): def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(None, '<root>') def init_app(self, app): self.flat_pages.init_app(app) self._set_categories() def __iter__(self): return iter(sorted(self.root_category.categories.values(), cmp=compare)) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category.articles.get(article_id) def get_articles_of_category(self, category_id): barticles = [] for a in self.root_category.categories.get(category_id).articles.values(): if a.id != 'index': barticles.append(a) return barticles def get_or_404(self, category_id, article_id): page = self.get(category_id, article_id) if page is None: abort(404) return page def _set_categories(self): for page in self.flat_pages: components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_category(category_id) page_name = components[-1] parent.add_article(page_name, page) def reload(self): self.flat_pages.reload() self._set_categories()
def test_mixed_extension_types(self): app = Flask(__name__) from markdown.extensions.toc import TocExtension toc = TocExtension() app.config['FLATPAGES_MARKDOWN_EXTENSIONS'] = [ toc, 'codehilite', 'markdown.extensions.extra:ExtraExtension' ] pages = FlatPages(app) self.check_toc_page(pages) self.check_default_codehilite_page(pages) self.check_extra(pages) app.config['FLATPAGES_EXTENSION_CONFIGS'] = { 'codehilite': { 'linenums': 'True' } } pages.reload() pages._file_cache = {} self.check_toc_page(pages) self.check_extra(pages) self.check_codehilite_with_linenums(pages)
def admin(): # Handle forwarding if redirected here after login if session.get("next_url"): next_url = session.get("next_url") session.pop("next_url", None) return redirect(next_url) # Process form data form = AdminForm() form.tag.choices = app.config["BLOG_TAGS"] if form.validate_on_submit(): pages_directory = (Path() / app.config["FLASK_APP"] / app.config["FLATPAGES_ROOT"]) if not pages_directory.exists(): abort(500) result = form.save( template="page_contents.j2", directory=pages_directory, ) if result: FlatPages.reload(pages) flash("Blog entry posted successfully!", "Info") else: flash("Something went wrong. Please try again shortly.", "Error") return redirect(url_for("admin")) else: if form.errors: flash( "Please correct errors detected in your form", "Error", ) return render_template("admin.j2", form=form)
class CategorizedFlatPages: """The main interface to gather pages and categories * What is it used for? - Looping: E.g. In the navbar - get news → get_articles_of_category('news') - get static page → get_or_404() """ def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(None, '<root>') def init_app(self, app): self.flat_pages.init_app(app) self._init_categories() @property def categories(self): """Yield all categories as an iterable """ return sorted(self.root_category.categories.values(), key=attrgetter('rank')) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category._articles.get(article_id) def get_category(self, category_id): """Return the `Category` object from a given name (id) """ return self.root_category.categories.get(category_id) def get_articles_of_category(self, category_id): """Get the articles of a category - ONLY used for fetching news """ articles = [] category = self.get_category(category_id) if category: for a in category._articles.values(): if a.id != 'index': articles.append(a) return articles def get_or_404(self, category_id, article_id): """Fetch a static page""" page = self.get(category_id, article_id) if page is None: abort(404) return page def _init_categories(self): # TODO: Store categories, not articles for page in self.flat_pages: # get category + page name # plus, assert that there is nothing more to that. components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_child_category(category_id) basename = components[-1] parent.add_article(basename, page) def reload(self): self.flat_pages.reload() self._init_categories()
class CategorizedFlatPages: """The main interface to gather pages and categories * What is it used for? - Looping: E.g. In the navbar - get news → get_articles_of_category('news') - get static page → get_or_404() """ def __init__(self): self.flat_pages = FlatPages() self.root_category = Category(self, None, '<root>') self.app = None def init_app(self, app): assert self.app is None, "Already initialized with an app" self.app = app app.cf_pages = self self.flat_pages.init_app(app) self._init_categories() @property def categories(self): """Yield all categories as an iterable """ return sorted(self.root_category.categories.values(), key=attrgetter('rank')) def get(self, category_id, article_id): category = self.root_category.categories.get(category_id) if category is None: return None return category._articles.get(article_id) def get_category(self, category_id): """Return the `Category` object from a given name (id) """ return self.root_category.categories.get(category_id) def get_articles_of_category(self, category_id): """Get the articles of a category - ONLY used for fetching news """ category = self.get_category(category_id) if category is None: return [] return [article for article in category._articles.values() if article.id != 'index'] def get_or_404(self, category_id, article_id): """Fetch a static page""" page = self.get(category_id, article_id) if page is None: abort(404) return page def _init_categories(self): # TODO: Store categories, not articles for page in self.flat_pages: # get category + page name # plus, assert that there is nothing more to that. components = page.path.split('/') parent = self.root_category for category_id in components[:-1]: parent = parent.add_child_category(category_id) prefix = components[-1] parent.add_article(prefix, page) def reload(self): self.flat_pages.reload() self._init_categories()