Skip to content

Commit

Permalink
Merge pull request #24 from carlogico/feature/MultiplyAmountButton
Browse files Browse the repository at this point in the history
Feature/multiply amount button
  • Loading branch information
carlogico authored Dec 5, 2024
2 parents 1b34c97 + 49c890c commit 40dd2d8
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/source/Recipes/Breads/Pastry_RoughPuff.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
Rough Puff Pastry
=================

.. multiply::

.. makes::
2 pie crusts or a top and a bottom.
:amnt:`2` pie crusts or :amnt:`1` top and bottom.

.. ingredients::

- 150 g water, cold from the fridge, plus some extra in a different container
- 300 g butter, cut in 1.5cm cubes, just about defrosted
- 450 g AP flour, also fridge temperature
- 5 g salt
- :amnt:`150 g` water, cold from the fridge, plus some extra in a different container
- :amnt:`300 g` butter, cut in 1.5cm cubes, just about defrosted
- :amnt:`450 g` AP flour, also fridge temperature
- :amnt:`5 g` salt

.. tools::

Expand All @@ -27,7 +29,7 @@ Rough Puff Pastry
Squeeze the butter cubes between your fingers to flatten them down. If they are too solid,
wait a little bit and try again...

Drizzle the 150 g of water over the mixture and gently incorporate with your fingers, do not knead.
Drizzle the :amnt:`150 g` of water over the mixture and gently incorporate with your fingers, do not knead.
Once incorporated, invert the bowl onto the working surface. It should be very shaggy and dry, with lots of dry flour hanging around.
Gather the "dough" into a rough square and start rolling. The goal is to flatten the butter even more into very thin sheets.
Use the bench scraper to fold the "dough" onto itself (it is still expected for it to not fully hold together here)
Expand Down
64 changes: 64 additions & 0 deletions docs/source/_static/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function storeAndParseOriginal(section, match, parseFunction) {
if (!section.dataset.original) {
section.dataset.original = parseFunction(match); // Store the original value
}
return section.dataset.original
}
function multiplyAndFormatResult(val, multiplier) {
const result = val * multiplier;
return Number.isInteger(result) ? result : result.toFixed(2);
}

document.addEventListener('DOMContentLoaded', () => {
const multiplyButton = document.getElementById('multiply-btn');
const resetButton = document.getElementById('reset-btn');

multiplyButton.addEventListener('click', () => {
const multiplier = parseFloat(document.getElementById('multiplier').value);
if (isNaN(multiplier)) {
alert('Please enter a valid multiplier');
return;
}

const amounts = document.querySelectorAll('.amnt'); // Select all elements with the class 'amnt'
amounts.forEach((section) => {
let text = section.innerHTML;
if (!section.dataset.originalText) {
section.dataset.originalText = text; // Store the original text
}
// Handle mixed fractions
if (/\b\d+\s+\d+\/\d+/.test(text)) {
text = text.replace(/\b(\d+)\s+(\d+)\/(\d+)/g, (_, whole, num, den) => {
const result = storeAndParseOriginal(section, {whole, num, den}, (val) => parseFloat(val.whole) + val.num / val.den);
return multiplyAndFormatResult(result, multiplier);
});
}
// Handle fractions
else if (/\b\d+\/\d+/.test(text)) {
text = text.replace(/\b(\d+)\/(\d+)/g, (_, num, den) => {
const result = storeAndParseOriginal(section, {num, den}, (val) => val.num / val.den);
return multiplyAndFormatResult(result, multiplier);
});
}
// Handle whole numbers and decimals
else if (/\b\d+(\.\d+)?/.test(text)) {
text = text.replace(/\b\d+(\.\d+)?/g, (match) => {
const result = storeAndParseOriginal(section, match, (val) => parseFloat(val));
return multiplyAndFormatResult(result, multiplier)
});
}
// Update the HTML with the modified text
section.innerHTML = text;
});
});

resetButton.addEventListener('click', () => {
const amounts = document.querySelectorAll('.amnt'); // Select all elements with the class 'amnt'
amounts.forEach((section) => {
if (section.dataset.originalText) {
section.innerHTML = section.dataset.originalText;
return section.dataset.original;
}
});
});
});
73 changes: 73 additions & 0 deletions docs/source/_static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,76 @@ footer a{
background-color: white !important;
font-weight: normal !important;
}

.button {
align-items: center;
appearance: none;
background-color: #fff;
border: 1px solid #dbdbdb;
border-radius: .375em;
box-shadow: none;
box-sizing: border-box;
color: #363636;
cursor: pointer;
display: inline-flex;
justify-content: center;
padding: calc(.5em - 1px) 1em;
position: relative;
text-align: center;
user-select: none;
-webkit-user-select: none;
touch-action: manipulation;
vertical-align: center;
white-space: nowrap;
}

.button:active {
border-color: #4a4a4a;
outline: 0;
}

.button:focus {
border-color: #485fc7;
outline: 0;
}

.button:hover {
border-color: #b5b5b5;
}

.button:focus:not(:active) {
box-shadow: rgba(72, 95, 199, .25) 0 0 0 .125em;
}

input[type=number] {
appearance: none;
background-color: #fff;
border: 1px solid #dbdbdb;
border-radius: .375em;
box-shadow: none;
padding:5px;
-webkit-border-radius: 5px;
border-radius: 5px;
width: 4em;
max-width: 8em;
vertical-align: center;
}

input[type=number]:focus {
border-color: #485fc7;
outline: 0;
}

input[type=number]:focus {
border-color: #4a4a4a;
outline: 0;
}

input[type=submit] {
padding:5px 15px;
background:#ccc;
border:0 none;
cursor:pointer;
-webkit-border-radius: 5px;
border-radius: 5px;
}
10 changes: 10 additions & 0 deletions docs/source/_templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "!layout.html" %}
<div role="main">
{% block document %}
{{ super()|replace('<div class="multiply admonition">
</div>',
'<input type="number" id="multiplier" placeholder="1" />
<button class="button" role="button" id="multiply-btn">Multiply</button>
<button class="button" role="button" id="reset-btn">Reset</button>') }}
{% endblock %}
</div>
26 changes: 26 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@

master_doc = 'index'

html_js_files = [
'script.js'
]

rst_prolog = """
.. role:: amnt
:class: amnt
"""

# -- Options for HTML output -------------------------------------------------

Expand Down Expand Up @@ -193,6 +201,23 @@ def run(self):
admonition_node)
return [admonition_node]

class Multiply(BaseAdmonition):

required_arguments = 0
optional_arguments = 0
node_class = nodes.admonition

def run(self):
set_classes(self.options)
text = '\n'.join(self.content)
admonition_node = self.node_class(text, **self.options)
self.add_name(admonition_node)
if self.node_class is nodes.admonition:
if 'classes' not in self.options:
admonition_node['classes'] += ['multiply']
self.state.nested_parse(self.content, self.content_offset,
admonition_node)
return [admonition_node]

def add_buttonsData_to_context(app, pagename, templatename, context, doctree):

Expand All @@ -213,6 +238,7 @@ def setup(app):
app.add_directive('tools', Tools)
app.add_directive('procedure', Procedure)
app.add_directive('makes', Makes)
app.add_directive('multiply', Multiply)


# -- Options for LaTeX output ---------------------------------------------
Expand Down

0 comments on commit 40dd2d8

Please sign in to comment.