-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
154 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:recipe_flavored_markdown/recipe_flavored_markdown.dart'; | ||
|
||
const recipe = '1 3/4 cups [sugar]'; | ||
|
||
const parser = RecipeMarkdownParser( | ||
markdown: recipe, | ||
); | ||
|
||
final nodes = parser.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,87 @@ | ||
import 'package:markdown/markdown.dart'; | ||
import 'package:recipe_flavored_markdown/src/recipe_nodes.dart'; | ||
|
||
const String _units = '(?:' | ||
const String _units = '(' | ||
'tsp(?:s?)|tbsp(?:s?)|oz|cup(?:s?)|pint(?:s?)|quart(?:s?)|' | ||
'gal(?:s?)|gallon(?:s?)|lb(?:s?)|kg|g|ml|l' | ||
'gal(?:s?)|gallon(?:s?)|lb(?:s?)|kg|g|ml|l|hr(?:s?)|' | ||
'hour(?:s?)|min(?:s?)|minute(?:s?)|sec(?:s?)|second(?:s?)' | ||
')'; | ||
|
||
/// {@template quantity_mixed_number_syntax} | ||
/// Represents a cooking quantity given as a mixed number: i.e., | ||
/// `1 2/3 cups` or `1.5 2.3/4.5 tbsp` (you probably shouldn't use | ||
/// decimals, but you can). | ||
/// {@template scalar_mixed_number_syntax} | ||
/// Represents a cooking scalar given as a mixed number: i.e., | ||
/// `1 2/3 cups`, `1.5 2.3/4.5 tbsp`, or `30 4/5 mins`. | ||
/// Decimals are allowed, but not recommended. | ||
/// {@endtemplate} | ||
class QuantityMixedNumberSyntax extends InlineSyntax { | ||
/// {@macro quantity_mixed_number_syntax} | ||
QuantityMixedNumberSyntax() : super(_pattern); | ||
class ScalarMixedNumberSyntax extends InlineSyntax { | ||
/// {@macro scalar_mixed_number_syntax} | ||
ScalarMixedNumberSyntax() : super(_pattern); | ||
|
||
static const String _quantifier = | ||
r'(\d+(?:\.\d+)?\s+(\d+(?:\.\d+)?)\s*\/\s*(\d+(?:\.\d+)?))'; | ||
r'(\d+(?:\.\d+)?)\s+(\d+(?:\.\d+)?)\s*\/\s*(\d+(?:\.\d+)?)'; | ||
static const String _pattern = '$_quantifier\\s+$_units'; | ||
|
||
@override | ||
bool onMatch(InlineParser parser, Match match) { | ||
parser.addNode(Quantity(match.group(0)!)); | ||
parser.addNode( | ||
Scalar( | ||
textContent: match.group(0)!, | ||
wholeNumberString: match.group(1)!, | ||
numeratorString: match.group(2)!, | ||
denominatorString: match.group(3)!, | ||
unitString: match.group(4)!, | ||
), | ||
); | ||
return true; | ||
} | ||
} | ||
|
||
/// {@template quantity_whole_number_syntax} | ||
/// Represents a cooking quantity given as a whole number: i.e., | ||
/// `1 cup` or `1.5 tbsp` (you probably shouldn't use decimals, but you can). | ||
/// {@template scalar_whole_number_syntax} | ||
/// Represents a cooking scalar given as a whole number: i.e., | ||
/// `1 cup`, `1.5 tbsp`, '35.6 seconds`. | ||
/// Decimals are allowed, but not recommended. | ||
/// {@endtemplate} | ||
class QuantityWholeNumberSyntax extends InlineSyntax { | ||
/// {@macro quantity_whole_number_syntax} | ||
QuantityWholeNumberSyntax() : super(_pattern); | ||
class ScalarWholeNumberSyntax extends InlineSyntax { | ||
/// {@macro scalar_whole_number_syntax} | ||
ScalarWholeNumberSyntax() : super(_pattern); | ||
|
||
static const String _quantifier = r'(\d+(?:\.\d+)?)'; | ||
static const String _pattern = '$_quantifier\\s+$_units'; | ||
|
||
@override | ||
bool onMatch(InlineParser parser, Match match) { | ||
parser.addNode(Quantity(match.group(0)!)); | ||
parser.addNode( | ||
Scalar( | ||
textContent: match.group(0)!, | ||
wholeNumberString: match.group(1)!, | ||
unitString: match.group(2)!, | ||
), | ||
); | ||
return true; | ||
} | ||
} | ||
|
||
/// {@template quantity_fractional_syntax} | ||
/// Represents a cooking quantity given as a fraction: i.e., | ||
/// `1/2 tsp` or `1.5/4.5 tbsp` (you probably shouldn't use decimals, but you can). | ||
/// {@template scalar_fractional_syntax} | ||
/// Represents a cooking scalar given as a fraction: i.e., | ||
/// `1/2 tsp`, `1.5/4.5 tbsp`, or `1/2 minute`. | ||
/// Decimals are allowed, but not recommended. | ||
/// {@endtemplate} | ||
class QuantityFractionalSyntax extends InlineSyntax { | ||
/// {@macro quantity_fractional_syntax} | ||
QuantityFractionalSyntax() : super(_pattern); | ||
class ScalarFractionalSyntax extends InlineSyntax { | ||
/// {@macro scalar_fractional_syntax} | ||
ScalarFractionalSyntax() : super(_pattern); | ||
|
||
static const String _quantifier = r'(\d+(?:\.\d+)?\s*\/\s*\d+(?:\.\d+)?)'; | ||
static const String _quantifier = r'(\d+(?:\.\d+)?)\s*\/\s*(\d+(?:\.\d+)?)'; | ||
static const String _pattern = '$_quantifier\\s+$_units'; | ||
|
||
@override | ||
bool onMatch(InlineParser parser, Match match) { | ||
parser.addNode(Quantity(match.group(0)!)); | ||
parser.addNode( | ||
Scalar( | ||
textContent: match.group(0)!, | ||
numeratorString: match.group(1)!, | ||
denominatorString: match.group(2)!, | ||
unitString: match.group(3)!, | ||
), | ||
); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters