BlueGit

timfa posprint

Accept URL query strings in @qr/@barcode data.

Only treat simple key=value tokens as attributes so https://…?a=1 is not rejected.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-05 · 3 months ago

Hash
21ed3af7188f8e0597701fe79c907c9dec74c29f
Parent
127cb394
1 file changed +55 −2
src/dsl/parser.rs +55 −2
@@ -659,6 +659,9 @@ fn parse_align_value(value: &str, line_no: usize) -> Result<Align, ParseError> {
659 659
660 660 /// Collect data tokens from `data_idx` until the first `key=value` attribute.
661 661 /// Remaining tokens are returned as attributes (must all be `key=value`).
662 ///
663 /// Tokens that contain `=` inside a URL/query (e.g. `https://x?a=1`) are still
664 /// treated as data — only simple identifier keys count as attributes.
662 665 fn take_data_tokens<'a>(
663 666 parts: &'a [&str],
664 667 data_idx: usize,
@@ -668,7 +671,7 @@ fn take_data_tokens<'a>(
668 671 if data_idx >= parts.len() {
669 672 return Err(err(line_no, 1, format!("{directive} requires data")));
670 673 }
671 if parts[data_idx].contains('=') {
674 if looks_like_attribute_token(parts[data_idx]) {
672 675 return Err(err(
673 676 line_no,
674 677 1,
@@ -676,13 +679,25 @@ fn take_data_tokens<'a>(
676 679 ));
677 680 }
678 681 let mut data_end = data_idx + 1;
679 while data_end < parts.len() && !parts[data_end].contains('=') {
682 while data_end < parts.len() && !looks_like_attribute_token(parts[data_end]) {
680 683 data_end += 1;
681 684 }
682 685 let data = parts[data_idx..data_end].join(" ");
683 686 Ok((data, &parts[data_end..]))
684 687 }
685 688
689 /// True for tokens like `size=4` / `align=center`, false for URLs with `?a=1`.
690 fn looks_like_attribute_token(token: &str) -> bool {
691 let Some((key, _value)) = token.split_once('=') else {
692 return false;
693 };
694 if key.is_empty() {
695 return false;
696 }
697 key.chars()
698 .all(|c| c.is_ascii_alphanumeric() || c == '_')
699 }
700
686 701 fn is_list_item_line(line: &str) -> bool {
687 702 line.trim_start().starts_with("- ")
688 703 }
@@ -1225,4 +1240,42 @@ mod tests {
1225 1240 other => panic!("expected qr, got {other:?}"),
1226 1241 }
1227 1242 }
1243
1244 #[test]
1245 fn qr_url_with_query_equals_is_data_not_attribute() {
1246 let doc = parse(
1247 "@qr https://sleutels.kvt.nl/moirai/index.php?t=l&d=5CD146GMCC size=4 ecc=M align=center",
1248 )
1249 .unwrap();
1250 match &doc.blocks[0] {
1251 Block::Qr {
1252 data,
1253 size,
1254 ecc,
1255 align,
1256 ..
1257 } => {
1258 assert_eq!(
1259 data,
1260 "https://sleutels.kvt.nl/moirai/index.php?t=l&d=5CD146GMCC"
1261 );
1262 assert_eq!(*size, 4);
1263 assert_eq!(*ecc, QrEcc::M);
1264 assert_eq!(*align, Align::Center);
1265 }
1266 other => panic!("expected qr, got {other:?}"),
1267 }
1268 }
1269
1270 #[test]
1271 fn barcode_url_with_query_equals_is_data() {
1272 let doc = parse("@barcode RASTER CODE128 https://example.com/x?a=1&b=2 height=40").unwrap();
1273 match &doc.blocks[0] {
1274 Block::Barcode { data, height, .. } => {
1275 assert_eq!(data, "https://example.com/x?a=1&b=2");
1276 assert_eq!(*height, 40);
1277 }
1278 other => panic!("expected barcode, got {other:?}"),
1279 }
1280 }
1228 1281 }
@@ -659,6 +659,9 @@ fn parse_align_value(value: &str, line_no: usize) -> Result<Align, ParseError> {
659
659
660/// Collect data tokens from `data_idx` until the first `key=value` attribute.
660/// Collect data tokens from `data_idx` until the first `key=value` attribute.
661/// Remaining tokens are returned as attributes (must all be `key=value`).
661/// Remaining tokens are returned as attributes (must all be `key=value`).
662///
663/// Tokens that contain `=` inside a URL/query (e.g. `https://x?a=1`) are still
664/// treated as data — only simple identifier keys count as attributes.
662fn take_data_tokens<'a>(
665fn take_data_tokens<'a>(
663 parts: &'a [&str],
666 parts: &'a [&str],
664 data_idx: usize,
667 data_idx: usize,
@@ -668,7 +671,7 @@ fn take_data_tokens<'a>(
668 if data_idx >= parts.len() {
671 if data_idx >= parts.len() {
669 return Err(err(line_no, 1, format!("{directive} requires data")));
672 return Err(err(line_no, 1, format!("{directive} requires data")));
670 }
673 }
671 if parts[data_idx].contains('=') {
674 if looks_like_attribute_token(parts[data_idx]) {
672 return Err(err(
675 return Err(err(
673 line_no,
676 line_no,
674 1,
677 1,
@@ -676,13 +679,25 @@ fn take_data_tokens<'a>(
676 ));
679 ));
677 }
680 }
678 let mut data_end = data_idx + 1;
681 let mut data_end = data_idx + 1;
679 while data_end < parts.len() && !parts[data_end].contains('=') {
682 while data_end < parts.len() && !looks_like_attribute_token(parts[data_end]) {
680 data_end += 1;
683 data_end += 1;
681 }
684 }
682 let data = parts[data_idx..data_end].join(" ");
685 let data = parts[data_idx..data_end].join(" ");
683 Ok((data, &parts[data_end..]))
686 Ok((data, &parts[data_end..]))
684}
687}
685
688
689/// True for tokens like `size=4` / `align=center`, false for URLs with `?a=1`.
690fn looks_like_attribute_token(token: &str) -> bool {
691 let Some((key, _value)) = token.split_once('=') else {
692 return false;
693 };
694 if key.is_empty() {
695 return false;
696 }
697 key.chars()
698 .all(|c| c.is_ascii_alphanumeric() || c == '_')
699}
700
686fn is_list_item_line(line: &str) -> bool {
701fn is_list_item_line(line: &str) -> bool {
687 line.trim_start().starts_with("- ")
702 line.trim_start().starts_with("- ")
688}
703}
@@ -1225,4 +1240,42 @@ mod tests {
1225 other => panic!("expected qr, got {other:?}"),
1240 other => panic!("expected qr, got {other:?}"),
1226 }
1241 }
1227 }
1242 }
1243
1244 #[test]
1245 fn qr_url_with_query_equals_is_data_not_attribute() {
1246 let doc = parse(
1247 "@qr https://sleutels.kvt.nl/moirai/index.php?t=l&d=5CD146GMCC size=4 ecc=M align=center",
1248 )
1249 .unwrap();
1250 match &doc.blocks[0] {
1251 Block::Qr {
1252 data,
1253 size,
1254 ecc,
1255 align,
1256 ..
1257 } => {
1258 assert_eq!(
1259 data,
1260 "https://sleutels.kvt.nl/moirai/index.php?t=l&d=5CD146GMCC"
1261 );
1262 assert_eq!(*size, 4);
1263 assert_eq!(*ecc, QrEcc::M);
1264 assert_eq!(*align, Align::Center);
1265 }
1266 other => panic!("expected qr, got {other:?}"),
1267 }
1268 }
1269
1270 #[test]
1271 fn barcode_url_with_query_equals_is_data() {
1272 let doc = parse("@barcode RASTER CODE128 https://example.com/x?a=1&b=2 height=40").unwrap();
1273 match &doc.blocks[0] {
1274 Block::Barcode { data, height, .. } => {
1275 assert_eq!(data, "https://example.com/x?a=1&b=2");
1276 assert_eq!(*height, 40);
1277 }
1278 other => panic!("expected barcode, got {other:?}"),
1279 }
1280 }
1228}
1281}