Switch back to hashtags for headings

This commit is contained in:
Laurenz 2021-06-07 18:06:27 +02:00
parent 7218892c72
commit f26526ba75
13 changed files with 38 additions and 36 deletions

View File

@ -61,7 +61,7 @@ fn node(p: &mut Parser, at_start: &mut bool) -> Option<Node> {
// Markup. // Markup.
Token::Star => Node::Strong(span), Token::Star => Node::Strong(span),
Token::Underscore => Node::Emph(span), Token::Underscore => Node::Emph(span),
Token::Eq => { Token::Hashtag => {
if *at_start { if *at_start {
return Some(heading(p)); return Some(heading(p));
} else { } else {
@ -128,11 +128,11 @@ fn node(p: &mut Parser, at_start: &mut bool) -> Option<Node> {
/// Parse a heading. /// Parse a heading.
fn heading(p: &mut Parser) -> Node { fn heading(p: &mut Parser) -> Node {
let start = p.start(); let start = p.start();
p.assert(Token::Eq); p.assert(Token::Hashtag);
// Count depth. // Count depth.
let mut level: usize = 1; let mut level: usize = 1;
while p.eat_if(Token::Eq) { while p.eat_if(Token::Hashtag) {
level += 1; level += 1;
} }

View File

@ -73,7 +73,7 @@ impl<'s> Iterator for Tokens<'s> {
'{' => Token::LeftBrace, '{' => Token::LeftBrace,
'}' => Token::RightBrace, '}' => Token::RightBrace,
// Keywords, variables, functions, colors. // Headings, keywords, identifiers, colors.
'#' => self.hash(start), '#' => self.hash(start),
// Whitespace. // Whitespace.
@ -93,7 +93,6 @@ impl<'s> Iterator for Tokens<'s> {
// Markup. // Markup.
'*' => Token::Star, '*' => Token::Star,
'_' => Token::Underscore, '_' => Token::Underscore,
'=' => Token::Eq,
'~' => Token::Tilde, '~' => Token::Tilde,
'`' => self.raw(), '`' => self.raw(),
'$' => self.math(), '$' => self.math(),
@ -157,6 +156,10 @@ impl<'s> Tokens<'s> {
match self.mode { match self.mode {
TokenMode::Markup => { TokenMode::Markup => {
if read.is_empty() {
return Token::Hashtag;
}
if let Some(token) = keyword(read) { if let Some(token) = keyword(read) {
return token; return token;
} }
@ -607,8 +610,8 @@ mod tests {
// Test markup tokens. // Test markup tokens.
t!(Markup[" a1"]: "*" => Star); t!(Markup[" a1"]: "*" => Star);
t!(Markup: "_" => Underscore); t!(Markup: "_" => Underscore);
t!(Markup[""]: "===" => Eq, Eq, Eq); t!(Markup[""]: "###" => Hashtag, Hashtag, Hashtag);
t!(Markup["a1/"]: "= " => Eq, Space(0)); t!(Markup["a1/"]: "# " => Hashtag, Space(0));
t!(Markup: "~" => Tilde); t!(Markup: "~" => Tilde);
t!(Markup[" "]: r"\" => Backslash); t!(Markup[" "]: r"\" => Backslash);
} }
@ -666,7 +669,7 @@ mod tests {
for &(s, t) in &keywords { for &(s, t) in &keywords {
t!(Markup[" "]: format!("#{}", s) => t); t!(Markup[" "]: format!("#{}", s) => t);
t!(Markup[" "]: format!("#{0}#{0}", s) => t, t); t!(Markup[" "]: format!("#{0}#{0}", s) => t, t);
t!(Markup[" /"]: format!("# {}", s) => Token::Invalid("#"), Space(0), Text(s)); t!(Markup[" /"]: format!("# {}", s) => Token::Hashtag, Space(0), Text(s));
} }
for &(s, t) in &keywords { for &(s, t) in &keywords {

View File

@ -141,7 +141,7 @@ impl PrettyWithMap for Node {
impl PrettyWithMap for HeadingNode { impl PrettyWithMap for HeadingNode {
fn pretty_with_map(&self, p: &mut Printer, map: Option<&NodeMap>) { fn pretty_with_map(&self, p: &mut Printer, map: Option<&NodeMap>) {
for _ in 0 .. self.level { for _ in 0 .. self.level {
p.push('='); p.push('#');
} }
self.contents.pretty_with_map(p, map); self.contents.pretty_with_map(p, map);
} }
@ -666,7 +666,7 @@ mod tests {
roundtrip("hi"); roundtrip("hi");
// Heading. // Heading.
roundtrip("= *Ok*"); roundtrip("# *Ok*");
// Raw. // Raw.
roundtrip("``"); roundtrip("``");

View File

@ -20,8 +20,8 @@ pub enum Token<'s> {
Star, Star,
/// An underscore: `_`. /// An underscore: `_`.
Underscore, Underscore,
/// A single equals sign: `=`. /// A single hashtag: `#`.
Eq, Hashtag,
/// A tilde: `~`. /// A tilde: `~`.
Tilde, Tilde,
/// A backslash followed by nothing or whitespace: `\`. /// A backslash followed by nothing or whitespace: `\`.
@ -38,6 +38,8 @@ pub enum Token<'s> {
Hyph, Hyph,
/// A slash: `/`. /// A slash: `/`.
Slash, Slash,
/// A single equals sign: `=`.
Eq,
/// Two equals signs: `==`. /// Two equals signs: `==`.
EqEq, EqEq,
/// An exclamation mark followed by an equals sign: `!=`. /// An exclamation mark followed by an equals sign: `!=`.
@ -200,6 +202,7 @@ impl<'s> Token<'s> {
Self::RightParen => "closing paren", Self::RightParen => "closing paren",
Self::Star => "star", Self::Star => "star",
Self::Underscore => "underscore", Self::Underscore => "underscore",
Self::Hashtag => "hashtag",
Self::Tilde => "tilde", Self::Tilde => "tilde",
Self::Backslash => "backslash", Self::Backslash => "backslash",
Self::Comma => "comma", Self::Comma => "comma",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

@ -1,9 +1,5 @@
// Test invalid function calls. // Test invalid function calls.
---
// Error: 1-2 unexpected invalid token
#
--- ---
// Error: 7-8 expected expression, found colon // Error: 7-8 expected expression, found colon
#args(:) #args(:)

View File

@ -2,7 +2,7 @@
#let name = "Klaus" #let name = "Klaus"
== Chapter 1 ## Chapter 1
#name stood in a field of wheat. There was nothing of particular interest about #name stood in a field of wheat. There was nothing of particular interest about
the field #name just casually surveyed for any paths on which the corn would not the field #name just casually surveyed for any paths on which the corn would not
totally ruin his semi-new outdorsy jacket but then again, most of us spend totally ruin his semi-new outdorsy jacket but then again, most of us spend

View File

@ -2,7 +2,7 @@
#let name = "Klaus" #let name = "Klaus"
== Chapter 2 ## Chapter 2
Their motivations, however, were pretty descript, so to speak. #name had not yet Their motivations, however, were pretty descript, so to speak. #name had not yet
conceptualized their consequences, but that should change pretty quickly. #name conceptualized their consequences, but that should change pretty quickly. #name
approached the center of the field and picked up a 4-foot long disk made from approached the center of the field and picked up a 4-foot long disk made from

View File

@ -1,7 +1,7 @@
// Test include statements. // Test include statements.
--- ---
= Document # Document
// Include a file // Include a file
#include "importable/chap1.typ" #include "importable/chap1.typ"

View File

@ -30,7 +30,7 @@
// the parentheses. // the parentheses.
#align(center)[ #align(center)[
// Markdown-like syntax for headings. // Markdown-like syntax for headings.
==== 3. Übungsblatt Computerorientierte Mathematik II #v(4mm) #### 3. Übungsblatt Computerorientierte Mathematik II #v(4mm)
*Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) #v(4mm) *Abgabe: 03.05.2019* (bis 10:10 Uhr in MA 001) #v(4mm)
*Alle Antworten sind zu beweisen.* *Alle Antworten sind zu beweisen.*
] ]

View File

@ -4,44 +4,44 @@
// Different number of hashtags. // Different number of hashtags.
// Valid levels. // Valid levels.
=1 # 1
===2 ### 2
======6 ###### 6
// Too many hashtags. // Too many hashtags.
// Warning: 1-8 should not exceed depth 6 // Warning: 1-8 should not exceed depth 6
=======7 ####### 7
--- ---
// Heading continuation over linebreak. // Heading continuation over linebreak.
// Code blocks continue heading. // Code blocks continue heading.
= A{ # A{
"B" "B"
} }
// Function call continues heading. // Function call continues heading.
= #rect[ # #rect[
A A
] B ] B
// Without some kind of block, headings end at a line break. // Without some kind of block, headings end at a line break.
= A # A
B B
--- ---
// Heading vs. no heading. // Heading vs. no heading.
// Parsed as headings if at start of the context. // Parsed as headings if at start of the context.
/**/ = Ok /**/ # Ok
{[== Ok]} {[## Ok]}
#rect[=== Ok] #rect[### Ok]
// Not at the start of the context. // Not at the start of the context.
No = heading No # heading
// Escaped. // Escaped.
\= No heading \# No heading
--- ---
// Make small, but double heading. // Make small, but double heading.
@ -49,4 +49,4 @@ No = heading
// The new heading's argument list doesn't contain `level`. // The new heading's argument list doesn't contain `level`.
// Error: 1-11 unexpected argument // Error: 1-11 unexpected argument
=== Twice. ### Twice.

View File

@ -75,18 +75,17 @@
{ {
"name": "markup.heading.typst", "name": "markup.heading.typst",
"contentName": "entity.name.section.typst", "contentName": "entity.name.section.typst",
"begin": "^\\s*={1,6}", "begin": "^\\s*#{1,6}\\s+",
"end": "\n", "end": "\n",
"beginCaptures": { "0": { "name": "punctuation.definition.heading.typst" } }, "beginCaptures": { "0": { "name": "punctuation.definition.heading.typst" } },
"patterns": [{ "include": "#markup" }] "patterns": [{ "include": "#markup" }]
}, },
{ {
"name": "punctuation.definition.list.unnumbered.typst", "name": "punctuation.definition.list.unnumbered.typst",
"match": "^\\s*-" "match": "^\\s*-\\s+"
}, },
{ {
"name": "punctuation.definition.list.numbered.typst", "name": "punctuation.definition.list.numbered.typst",
"match": "^\\s*[0-9]*\\."
}, },
{ {
"name": "string.other.math.block.typst", "name": "string.other.math.block.typst",
@ -111,6 +110,7 @@
"begin": "`", "begin": "`",
"end": "`", "end": "`",
"captures": { "0": { "name": "punctuation.definition.raw.typst" } } "captures": { "0": { "name": "punctuation.definition.raw.typst" } }
"match": "^\\s*[0-9]*\\.\\s+"
}, },
{ {
"name": "keyword.control.typst", "name": "keyword.control.typst",