Implement contiguous preformat blocks with alt-text

CandyCorvid
Jul 13, 2023, 10:33 AM
OSV63NXNIRGOEJZUECHXYYYEFUAFXLN4BNIH3MKLE7SDZLHGFEWAC

Dependencies

  • [2] HVFBGX2A Implement basic text/gemini
  • [3] DBKKKHC2 Initial commit
  • [4] ONQEIR5B WIP mime-type handling
  • [*] 3SPNKI46 Improve parsing. Add modules that were missed

Change contents

  • edit in src/main.rs at line 63
    [6.7856]
    [2.41]
    println!("response body: {body}");
  • replacement in src/main.rs at line 144
    [2.1018][2.1018:1040]()
    url: Url,
    [2.1018]
    [2.1040]
    url: TextLine<'a>,
  • replacement in src/main.rs at line 148
    [2.1126][2.1126:1162]()
    Preformatted(TextLine<'a>),
    [2.1126]
    [2.1162]
    Preformatted(Preformat<'a>),
  • edit in src/main.rs at line 151
    [2.1223]
    [3.663]
    }
    #[derive(Clone, Debug)]
    pub struct Preformat<'a> {
    alt: TextLine<'a>,
    lines: Vec<TextLine<'a>>,
  • replacement in src/main.rs at line 166
    [2.1491][2.1491:1565]()
    fn string_to_line(preformat: bool, string: TextLine<'_>) -> RawLine {
    [2.1491]
    [2.1565]
    fn string_to_preformat(string: TextLine<'_>) -> Option<TextLine<'_>> {
    let line = string.0;
    if line.starts_with("```") {
    // ignore anything after the lead chars on preformat lines
    return None;
    } else {
    // ignore any other formatting between preformat toggle lines
    return Some(string);
    }
    }
    fn string_to_line(string: TextLine<'_>) -> RawLine {
  • replacement in src/main.rs at line 179
    [2.1618][2.1618:1643]()
    match line {
    [2.1618]
    [2.1643]
    if line.starts_with("```") {
  • replacement in src/main.rs at line 181
    [2.1718][2.1718:2469]()
    "```" => {
    return RawLine::Toggle {
    alt: TextLine(&line[3..]),
    }
    }
    // ignore any other formatting between preformat toggle lines
    _ if preformat => Line::Preformatted(string),
    "=> " => {
    let line = &line[3..];
    match line.split_once(' ') {
    Some((url, desc)) => {
    let url = url.trim_start_matches(' ');
    Line::Link {
    url: url.parse().expect("invalid link url"),
    description: Some(TextLine(desc)),
    }
    [2.1718]
    [2.2469]
    return RawLine::Toggle {
    alt: TextLine(&line[3..]),
    };
    } else if line.starts_with("=> ") {
    let line = &line[3..];
    match line.split_once(' ') {
    Some((url, desc)) => {
    let url = url.trim_start_matches(' ');
    Line::Link {
    url: TextLine(url),
    description: Some(TextLine(desc)),
  • edit in src/main.rs at line 193
    [2.2495][2.2495:2688]()
    None => Line::Link {
    url: line.parse().expect("invalid link url"),
    description: None,
    },
  • edit in src/main.rs at line 194
    [2.2710]
    [2.2710]
    None => Line::Link {
    url: TextLine(line),
    description: None,
    },
  • replacement in src/main.rs at line 199
    [2.2728][2.2728:2830]()
    "* " => Line::ListItem(TextLine(&line[2..])),
    "# " => Line::Heading {
    [2.2728]
    [2.2830]
    } else if line.starts_with("* ") {
    Line::ListItem(TextLine(&line[2..]))
    } else if line.starts_with("# ") {
    Line::Heading {
  • replacement in src/main.rs at line 205
    [2.2909][2.2909:2969]()
    },
    "## " => Line::Heading {
    [2.2909]
    [2.2969]
    }
    } else if line.starts_with("## ") {
    Line::Heading {
  • replacement in src/main.rs at line 210
    [2.3048][2.3048:3109]()
    },
    "### " => Line::Heading {
    [2.3048]
    [2.3109]
    }
    } else if line.starts_with("### ") {
    Line::Heading {
  • replacement in src/main.rs at line 215
    [2.3188][2.3188:3307]()
    },
    "> " => Line::Quote(TextLine(&line[2..])),
    _ => Line::Text(string),
    [2.3188]
    [2.3307]
    }
    } else if line.starts_with(">") {
    Line::Quote(TextLine(&line[1..]))
    } else {
    Line::Text(string)
  • replacement in src/main.rs at line 224
    [2.3389][2.3389:3806]()
    value
    .lines()
    .map(TextLine)
    // start with preformatting set to off
    .scan(false, |preformat, line| {
    match string_to_line(*preformat, line) {
    // if we hit a toggle line, switch preformatting mode
    RawLine::Toggle { alt } => {
    *preformat = !*preformat;
    None
    [2.3389]
    [2.3806]
    {
    let mut outer = vec![];
    let mut preformat_block: Option<Preformat> = None;
    let lines = value.lines().map(TextLine);
    for line in lines {
    if preformat_block.is_some() {
    match string_to_preformat(line) {
    // if we hit a toggle line, switch preformatting mode
    None => {
    let Some(i) = preformat_block.take()
    else {unreachable!("This is within the is_some arm of the if")};
    outer.push(Line::Preformatted(i));
    }
    Some(p) => preformat_block.as_mut().unwrap().lines.push(p),
    }
    } else {
    match string_to_line(line) {
    // if we hit a toggle line, switch preformatting mode
    RawLine::Toggle { alt } => {
    preformat_block = Some(Preformat { alt, lines: vec![] });
    }
    RawLine::Line(l) => outer.push(l),
  • edit in src/main.rs at line 247
    [2.3828][2.3828:3926]()
    // otherwise, yield the line
    RawLine::Line(l) => Some(l),
  • replacement in src/main.rs at line 248
    [2.3944][2.3944:3982]()
    })
    .collect()
    [2.3944]
    [2.3982]
    }
    outer
    }