Exemple #1
0
 def test_multiple_output_hidden_subrecipe(self) -> None:
     assert render_recipe_tree(
         SubRecipe(
             Ingredient(SVS("spam")),
             (SVS("spam"), SVS("tin")),
             show_output_names=False,
         ),
         "qux-",
     ) == (""
           '<table class="rg-table">\n'
           "  <tr>\n"
           '    <td class="rg-ingredient '
           "rg-border-left-sub-recipe "
           "rg-border-right-sub-recipe "
           "rg-border-top-sub-recipe "
           'rg-border-bottom-sub-recipe">spam</td>\n'
           '    <td class="rg-sub-recipe-outputs '
           "rg-border-right-none "
           "rg-border-top-none "
           'rg-border-bottom-none">\n'
           '      <ul class="rg-sub-recipe-output-list">\n'
           '        <li id="qux-spam">spam</li>\n'
           '        <li id="qux-tin">tin</li>\n'
           "      </ul>\n"
           "    </td>\n"
           "  </tr>\n"
           "</table>")
Exemple #2
0
def visit_pending_recipe_node(self: HTMLTranslator, node: nodes.Node) -> None:
    self.body.append(
        t(
            "div",
            "\n".join(
                render_recipe_tree(recipe_tree, node.id_prefix)
                for recipe_tree in node.recipe.scale(node.scale).recipe_trees),
            class_="rg-recipe-block",
        ))
Exemple #3
0
 def test_non_subrecipe(self) -> None:
     assert render_recipe_tree(Ingredient(
         SVS("spam"))) == ('<table class="rg-table">'
                           "<tr>"
                           '<td class="rg-ingredient '
                           "rg-border-left-sub-recipe "
                           "rg-border-right-sub-recipe "
                           "rg-border-top-sub-recipe "
                           'rg-border-bottom-sub-recipe">spam</td>'
                           "</tr>"
                           "</table>")
Exemple #4
0
 def test_single_output_hidden_subrecipe(self) -> None:
     assert render_recipe_tree(
         SubRecipe(Ingredient(SVS("spam")), (SVS("spam"), ),
                   show_output_names=False),
         "qux-",
     ) == ('<table class="rg-table" id="qux-spam">'
           "<tr>"
           '<td class="rg-ingredient '
           "rg-border-left-sub-recipe "
           "rg-border-right-sub-recipe "
           "rg-border-top-sub-recipe "
           'rg-border-bottom-sub-recipe">spam</td>'
           "</tr>"
           "</table>")
Exemple #5
0
    def render(self, scale: Union[int, float, Fraction] = 1) -> str:
        """
        Render this recipe scaled by a given factor.
        """
        html = self.html

        # Substitute scaled value strings
        for placeholder, svs in self.scaled_value_strings.items():
            html = html.replace(
                placeholder,
                render_scaled_value_string(svs.scale(scale)),
            )

        # Substitute scaled recipes
        id_prefix_index = 0
        for placeholder, recipe in self.recipe_placeholders.items():
            if recipe.follows is None:
                id_prefix_index += 1

            if id_prefix_index > 1:
                id_prefix = f"recipe{id_prefix_index}-"
            else:
                id_prefix = "recipe-"

            html = html.replace(
                placeholder,
                t(
                    "div",
                    "\n".join(
                        render_recipe_tree(recipe_tree, id_prefix)
                        for recipe_tree in recipe.scale(scale).recipe_trees
                    ),
                    class_="rg-recipe-block",
                ),
            )

        # Modify heading to include scaling info
        if (
            self.title is not None
            and self.pre_title_placeholder is not None
            and self.post_title_placeholder is not None
        ):
            html = html.replace(self.pre_title_placeholder, "<header>")
            post_title_text = ""
            if scale != 1:
                if self.servings is not None:
                    orig_servings = t(
                        "span",
                        f"{self.servings} serving{'s' if self.servings != 1 else ''}",
                        class_="rg-original-servings",
                    )
                    post_title_text = t("p", f"Rescaled from {orig_servings}.")
                else:
                    scale_str = t(
                        "span",
                        f"{render_number(scale)}&times;",
                        class_="rg-scaling-factor",
                    )
                    post_title_text = t("p", f"Scaled {scale_str}")
            html = html.replace(
                self.post_title_placeholder, f"{post_title_text}</header>"
            )

        return html