class MyRap(Yawrap): resources = [ LinkCss("#nothing {}", file_name="that_new_style.css"), LinkCss.from_url("https://want.to/have/it/from_web.css"), LinkCss.from_file("/link/to/local_style.css"), LinkJs("Store that script to the file.js", file_name="that_new_script.js"), LinkJs.from_url("https://want.to/have/it/from_web.js"), LinkJs.from_file("link/to/local_script.js"), ]
def build_page(objects: List): jawrap = Yawrap('mainpage.html') with jawrap.tag('div', klass='content'): with jawrap.tag('span'): jawrap.text('CSS in yawrap.') jawrap.add(LinkCss.from_file('mainpage.css')) return jawrap.render() return response.html(jawrap.render())
class Root(NavedYawrap): resources = [ EmbedCss({"body": { "background": "#DAD" }}), LinkCss({"#that": { "color": "#DAD" }}, file_name="common_style.css"), ]
def test_defining_css_as_a_dict(mocked_save_file): class Root(NavedYawrap): resources = [ EmbedCss({"body": { "background": "#DAD" }}), LinkCss({"#that": { "color": "#DAD" }}, file_name="common_style.css"), ] root_doc = Root("one.html") root_doc.add(EmbedCss("div {padding: 0px;}")) sub_doc = root_doc.sub("two.html") sub_doc.add(EmbedCss("#id {margin: 10px 12px;}")) sub_doc.add(LinkCss("a {width: 90%;}", file_name="sub.css")) root_soup = BeautifulSoup(root_doc._render_page(), "html.parser") sub_soup = BeautifulSoup(sub_doc._render_page(), "html.parser") expected_root_styles = [ "body { background: #DAD; }".split(), "div {padding: 0px;}".split() ] expected_sub_styles = [ "body { background: #DAD; }".split(), "#id {margin: 10px 12px;}".split() ] root_styles = [ style.text.split() for style in root_soup.html.head.find_all("style") ] assert all([style in root_styles for style in expected_root_styles]) sub_styles = [ style.text.split() for style in sub_soup.html.head.find_all("style") ] assert all([style in sub_styles for style in expected_sub_styles]) root_links = [ link["href"] for link in root_soup.html.head.find_all("link") ] assert root_links == ['resources/common_style.css'] sub_links = [link["href"] for link in sub_soup.html.head.find_all("link")] assert sub_links == ['resources/common_style.css', 'resources/sub.css']
def test_link_local_style(out_dir, yawrap_class_with_naved, mocker): from yawrap._sourcer import os, _Resource, _ExportToTargetFs dummy_css = os.path.join(out_dir, 'some.css') dummy_target = os.path.join(out_dir, 'anything', 'index.html') mocker.patch.object(os.path, "isfile", return_value=True) mocker.patch.object(os.path, "isdir", return_value=True) mocker.patch.object(_Resource, "_read_file") mocker.patch.object(_ExportToTargetFs, "_save_as_file") jarap = yawrap_class_with_naved(dummy_target) jarap.add(LinkCss.from_file(dummy_css)) render = jarap._render_page() soup = BeautifulSoup(render, "html.parser") link = soup.html.head.link assert link assert link['href'] == 'resources/some.css' assert link['type'] == 'text/css' assert link['rel'] == ['stylesheet'] # list? wtf soup?
class MyPage(MyPageTemplate): resources = [ ExternalJs("https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"), LinkCss("""\ body { padding: 12px; font-family: helvetica, sans-serif; font-size: 14px; } .box { display: inline-block; height: 80px; margin: 8px; padding: 8px; width: 80px; }""", file_name="common_linked.css"), LinkJs("""\ $("button").click(function(){ $("#red-box").fadeToggle(); $("#green-box").fadeToggle("slow"); $("#blue-box").fadeToggle(3000); }); """, placement=BODY_END, file_name="common_linked.js"), ]
class LocalSources(Yawrap): resources = [ LinkCss.from_file("path/to/source_of/w3.css"), LinkJs.from_file("path/to/source_of/jquery.min.js"), ]
class LinkedCssPage(Yawrap): resources = [ LinkCss.from_url("https://www.w3schools.com/w3css/4/w3.css"), LinkJs.from_url("https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"), ]
class Root(NavedYawrap): resources = [ LinkCss("body { background: #DAD; }", file_name="common_style.css"), LinkJs('console.log("all right!");', file_name="common_script.js"), ]
class MyRap(Yawrap): resources = [ LinkCss(css_definition, file_name=css_file_name), ]