"""Test Html Renderer.
The commands are not run, so what a command wrote stands in for itself. What is checked is the page that comes out, since where a
linefeed goes is what makes one build of the documentation comparable with the next."""
####################################################################################################################################
import os
import struct
import tempfile
from harness.test import *
from common.error import *
from common.storage import file_read, file_write, path_list
from common.var_store import VarStore
from common.xml import xml_node_attribute, xml_parse
from command.render.execute import CacheInvalidError
from command.render.html import DocHtmlPage, HtmlBuilder, HtmlElement, _resource_version, html_render
from command.render.manifest import RENDER_HTML, Manifest
MANIFEST = """pgBackRestpgbackrest/Footer.
"""
USER_GUIDE = """How to use it.Start
An introduction.
A paragraph with markup.
Only in the markdown.
Careful.OneTwo
echo one
echo two
OptionsNameValueone1
Run itpgbackrest infosuppressedConfigure/var/libConfigure PostgreSQLonShown In ContentsInnerA SubtitleA SubsubtitleAside
Down the side of the page.
Quiet
Not in the contents.
Bare
No header on the page.
Elsewhere
Only in the markdown.
"""
SIMPLE_GUIDE = """How to use it.StartRun itpgbackrest info
"""
INDEX = """The index.AboutOneTwo
"""
####################################################################################################################################
class _Config:
"""The options a manifest reads."""
def __init__(self, **kwargs):
self.deploy = False
self.cache_only = False
self.pre = False
self.require = []
self.include = []
self.exclude = []
self.key_var_map = {}
for name, value in kwargs.items():
setattr(self, name, value)
####################################################################################################################################
def _doc_path(path, manifest=MANIFEST, user_guide=USER_GUIDE):
"""Write the documentation a page is rendered from."""
file_write(os.path.join(path, "manifest.xml"), manifest)
file_write(os.path.join(path, "xml/index.xml"), INDEX)
file_write(os.path.join(path, "xml/user-guide.xml"), user_guide)
file_write(os.path.join(path, "resource/html/default.css"), "body\n{\n color: black; /* black */\n}\n")
file_write(os.path.join(path, "resource/html/default.js"), "// what it is\nvar page = 1;\n")
file_write(os.path.join(path, "resource/slogo.svg"), "\n")
return path
####################################################################################################################################
def _manifest(path, config=None):
"""Load the documentation."""
return Manifest(path, VarStore(), {}, _Config() if config is None else config)
####################################################################################################################################
def _page(path, key="user-guide", config=None):
"""Render a page without running any of the commands it holds."""
manifest = _manifest(path, config)
render = manifest.render_get(RENDER_HTML)
return manifest.var_store.replace_str(DocHtmlPage(manifest, key, render.menu, False, "style1", "script1").process())
####################################################################################################################################
def test_element():
"""A page is built as a tree of elements so the code that decides what it holds never deals with markup."""
builder = HtmlBuilder("Name", "Title", None, None, None, None, "style1", "script1")
body = builder.body
body.add_new("div", "one", content="Content")
body.add(HtmlElement("div", "two"))
assert_in('
\nContent\n', builder.render())
assert_in('', builder.render())
####################################################################################################################################
def test_builder():
"""A page says what it is."""
builder = HtmlBuilder("pgBackRest", "Guide & More", "logo.svg", "card.png", (1200, 630), "How to use it.", "style1", "script1")
result = builder.render(analytics=True)
assert_in("', result)
# The card says where it is and what shape it is, so a preview can be laid out before the image has been fetched
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in('', result)
assert_in("googletagmanager.com", result)
# What cannot appear in an attribute is escaped
assert_in("Guide & More", result)
####################################################################################################################################
def test_page():
"""A page holds a header, a menu, a table of contents, and the sections of the document."""
with tempfile.TemporaryDirectory() as path:
page = _page(_doc_path(path))
# The logo is a link home and holds the name for a reader who cannot see what it is drawn as
assert_in('\npgBackRest\n', page)
assert_in('
\nTable of Contents\n', page)
# A page with contents says so, since that is what puts them in a column beside the text
assert_in('', page)
# A section can give the contents a shorter title than it gives the page
assert_in('
', page)
assert_true(page.index('id="side"') < page.index('id="start"'))
# What the document wrote after it says so, which is what puts the aside back in place when it cannot go down the side
assert_in('