Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yet another Brew Variables PR #3199

Merged
merged 33 commits into from
Feb 21, 2024
Merged

Yet another Brew Variables PR #3199

merged 33 commits into from
Feb 21, 2024

Conversation

calculuschild
Copy link
Member

@calculuschild calculuschild commented Dec 21, 2023

Building on #3173, I think this is a pretty good syntax that does everything everyone wants.

Deployed to https://homebrewery-pr-3199.herokuapp.com/

Notes:

  • Imperative definition works (i.e. variables can hold different values at different points in the document).
  • Double interpolation is not possible (i.e., composing variable names out of other variables).
  • Variable assignments inside of other variable assignments is not possible.
  • Variables are hoisted if not yet defined when called. Uses the latest possible contents of that variable.

Three syntaxes:

syntax description
[var]:content Assigns a variable
[var](content) Assigns a variable and outputs it
[var] Outputs a variable

Examples:

syntax output
[var]: Variable Contents Assigns "Variable Contents" to var
[var]: Variable Contents
on two
or three
or more lines
Assigns "Variable Contents\non two\nor three\nor more lines" to var
[var]:
| h1 | h2 |
|----|----|
| c1 | c2 |
Assigns "| h1 | h2 |\n|----|----|\n| c1 | c2 | to var (and can later be rendered as a table)
$[var](Variable Contents) Assigns "Variable Contents" to var and outputs that value
$[var](I love $[var2] and $[var3]) Assigns "I love var2 contents and var3 contents" to var and outputs that value
$[var] Outputs var contents
[var] outputs the variable contents as a link, if formatted as a valid link
![var] outputs as an image, if formatted as a valid image
$[var]: Variable Contents
or
![var]: Variable Contents
Identical to [var]: Variable Contents . Links, images, and variables all share the same global list. The ! or $ prefix only matters when the variable is being expressed
[var](Variable Contents)
or
![var](Variable Contents)
Identical to assignment via $[var](Variable Contents), but outputs as a link or image if contents are a valid link format
$[num1 + num2 * (num3 - round(5.5))] If +, -, *,/,^,(, or ) are found in the variable name, the whole variable is parsed mathematically with correct order of operations. Only parses if every variable is defined (hoisting works), and evaluate to a number, and the expression is valid. Supports round(x).

Math

Math currently supports the following function names: round(), floor(), ceil().
It also allows for () nesting operations.

So yes, you can do incrementing tables like this:

There are $[TableNum] tables in this document. // Final value of $[TableNum] gets hoisted up:
                                               //"There are 2 tables in this document."

$[TableNum]: 0 // Initialize to 0

$[TableNum]: $[TableNum + 1] // TableNum = 1

##### $[TableRefKnights](Table $[TableNum]: Horse Type and Quality)

...

$[TableNum]: $[TableNum + 1] // TableNum = 2

##### $[TableRefDragons](Table $[TableNum]: Dragons of the Realm)

@ericscheid
Copy link
Collaborator

How would one assign the string "https://example.com/" to a variable and then have it rendered as (literally) https://example.com/ ?

For an authoring system that produces print-destined output, being unable to print a url sounds like a problem.

I don't want printed documents that read as "For more details, visit our website at PublisherWebsite."

@ericscheid
Copy link
Collaborator

I'm a bit worried that double-interpolation might be setting us up for some recursion headaches.

@5e-Cleric
Copy link
Member

5e-Cleric commented Dec 22, 2023

How would one assign the string "https://example.com/" to a variable and then have it rendered as (literally) https://example.com/ ?

For an authoring system that produces print-destined output, being unable to print a url sounds like a problem.

I don't want printed documents that read as "For more details, visit our website at PublisherWebsite."

$[var]:https://example.com/ 
$[var]:(https://example.com/)

$[var]

first line is just assignment, second is assignment and output, third is just output
This worked just fine to output what you ask for in the deployment.

for clarification:

[var] will output equal to [var](https://example.com/)
$[var] will output equal to [https://example.com/](https://example.com/)

@calculuschild
Copy link
Member Author

calculuschild commented Dec 22, 2023

How would one assign the string "https://example.com/" to a variable and then have it rendered as (literally) https://example.com/ ?

Just use $[var]:(https://example.com/)

The $ indicates that it should be output as a string instead of a link.

@calculuschild
Copy link
Member Author

calculuschild commented Dec 22, 2023

I'm a bit worried that double-interpolation might be setting us up for some recursion headaches.

I'm not sure what you mean by double-interpolation.

Recursion would be handled by tracking a list of the variables you are currently inside and stop processing if we try to enter a nested variable already on that list.

@ericscheid
Copy link
Collaborator

I'm not sure what you mean by double-interpolation.

Interpolation is replacing variable references in a string with the value of those variables.

Double interpolation is replacing variable references in a string with the value of the variable named in those variables.

var foo = "some string"; // a string assigned to foo
var bar = `template with ${foo}`; // → "template with some string" (single interpolation)
var qaz = "foo"; // the string "foo" assigned to qaz
var qax = `template ${${qaz}}`; // the qaz variable is double interpolated, ultimately inserting the value of $foo

(I've also worked with systems which supported stuff like Set Field By Name[ variable_of_field_name, value_to_assign], but that's just more madness).

@calculuschild
Copy link
Member Author

calculuschild commented Dec 22, 2023

Double interpolation is replacing variable references in a string with the value of the variable named in those variables.

Ah ok. This PR isn't doing any double-interpolation by that definition so that shouldn't be a concern. I dont think it's even possible the way the code is set up.

Every example I listed above is "single interpolation".

@5e-Cleric
Copy link
Member

5e-Cleric commented Dec 22, 2023

Error list:

  • When using parentheses, no value is not expected, breaks the whole tool
  • String variables get lowercase'd
  • Image syntax completely broken
  • Variables not working inside other syntaxes, such as links (I don't know what i meant by that, so don't know if its fixed :))))) )

@5e-Cleric
Copy link
Member

$[var]:() will still break the brew completely.

@5e-Cleric 5e-Cleric added 🔍 R3 - Reviewed - Awaiting Fixes 🔧 PR is okayed but needs fixes before merging and removed 🔍 R4 - Reviewed - Fixed! ⭐ PR review comments addressed labels Jan 9, 2024
@calculuschild calculuschild temporarily deployed to homebrewery-pr-3199 January 10, 2024 03:29 Inactive
@calculuschild
Copy link
Member Author

$[var]:() will still break the brew completely.

Should be fixed. Now has same behavior as $[var]: where it just isn't parsed as a variable until it has some content, so empty () is not recognized.

@calculuschild calculuschild temporarily deployed to homebrewery-pr-3199 February 21, 2024 04:02 Inactive
@calculuschild calculuschild merged commit 53f1e53 into master Feb 21, 2024
2 checks passed
@calculuschild calculuschild deleted the GlobalReflinks branch March 19, 2024 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complicated P2 - minor feature or not urgent Minor bugs or less-popular features 🔍 R3 - Reviewed - Awaiting Fixes 🔧 PR is okayed but needs fixes before merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants