remove unspecified inline code tag support

This commit is contained in:
yggverse 2025-03-21 17:02:50 +02:00
parent 10ff400d8f
commit 678f906f48
8 changed files with 104 additions and 181 deletions

View file

@ -4,6 +4,7 @@ pub mod link;
pub mod list;
pub mod quote;
pub use code::Code;
pub use header::Header;
pub use link::Link;
pub use list::List;

View file

@ -1,7 +1,90 @@
pub mod inline;
pub mod multiline;
pub use inline::Inline;
pub use multiline::Multiline;
pub mod error;
pub use error::Error;
pub const TAG: &str = "```";
pub const NEW_LINE: char = '\n';
/// Multi-line [preformatted](https://geminiprotocol.net/docs/gemtext-specification.gmi#in-pre-formatted-mode) entity holder
pub struct Code {
pub alt: Option<String>,
pub value: String,
pub is_completed: bool,
}
impl Code {
// Constructors
/// Search in line string for tag open,
/// return Self constructed on success or None
pub fn begin_from(line: &str) -> Option<Self> {
if line.starts_with(TAG) {
let alt = line.trim_start_matches(TAG).trim();
return Some(Self {
alt: match alt.is_empty() {
true => None,
false => Some(alt.to_string()),
},
value: String::new(),
is_completed: false,
});
}
None
}
/// Continue preformatted buffer from line string,
/// set `completed` as True on close tag found
pub fn continue_from(&mut self, line: &str) -> Result<(), Error> {
// Make sure buffer not completed yet
if self.is_completed {
return Err(Error::Completed);
}
// Append to value, trim close tag on exists
self.value.push_str(line.trim_end_matches(TAG));
// Line contain close tag
if line.ends_with(TAG) {
self.is_completed = true;
} else {
self.value.push(NEW_LINE);
}
Ok(())
}
// Converters
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) format
pub fn to_source(&self) -> String {
format!(
"{TAG}{}{NEW_LINE}{}{TAG}",
match &self.alt {
Some(alt) => format!(" {}", alt.trim()),
None => String::new(),
},
self.value
)
}
}
#[test]
fn test() {
match Code::begin_from("```alt") {
Some(mut code) => {
assert!(code.continue_from("line 1").is_ok());
assert!(code.continue_from("line 2").is_ok());
assert!(code.continue_from("```").is_ok()); // complete
assert!(code.is_completed);
assert_eq!(code.alt, Some("alt".into()));
assert_eq!(code.value.len(), 12 + 2); // +NL
assert_eq!(
code.to_source(),
format!("{TAG} alt{NEW_LINE}line 1{NEW_LINE}line 2{NEW_LINE}{TAG}")
)
}
None => assert!(false),
}
}

View file

@ -1,46 +0,0 @@
pub mod gemtext;
pub use gemtext::Gemtext;
use super::TAG;
/// Inline [preformatted](https://geminiprotocol.net/docs/gemtext-specification.gmi#in-pre-formatted-mode) entity holder
pub struct Inline {
pub value: String,
}
impl Inline {
// Constructors
/// Parse `Self` from line string
pub fn parse(line: &str) -> Option<Self> {
line.as_value().map(|v| Self {
value: v.to_string(),
})
}
// Converters
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
pub fn to_source(&self) -> String {
self.value.to_source()
}
}
#[test]
fn test() {
fn assert(source: &str, value: &str) {
let list = Inline::parse(source).unwrap();
assert_eq!(list.value, value);
assert_eq!(list.to_source(), format!("```{value}```"));
}
assert("```inline```", "inline");
assert("```inline ```", "inline");
assert("``` inline ```", "inline");
assert("``` inline```", "inline");
assert("``` inline``` ", "inline");
assert("``````inline``` ", "```inline");
assert("``````inline`````` ", "```inline```");
assert("```inline`````` ", "inline```");
assert!("```inline".as_value().is_none());
assert!("```inline``` ne".as_value().is_none());
}

View file

@ -1,38 +0,0 @@
use super::TAG;
pub trait Gemtext {
/// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self`
fn as_value(&self) -> Option<&str>;
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
fn to_source(&self) -> String;
}
impl Gemtext for str {
fn as_value(&self) -> Option<&str> {
if let Some(p) = self.strip_prefix(TAG) {
return p.trim().strip_suffix(TAG).map(|s| s.trim());
}
None
}
fn to_source(&self) -> String {
format!("{TAG}{}{TAG}", self.trim())
}
}
#[test]
fn test() {
fn assert(source: &str, value: &str) {
assert_eq!(source.as_value(), Some(value));
assert_eq!(value.to_source(), format!("```{value}```"));
}
assert("```inline```", "inline");
assert("```inline ```", "inline");
assert("``` inline ```", "inline");
assert("``` inline```", "inline");
assert("``` inline``` ", "inline");
assert("``````inline``` ", "```inline");
assert("``````inline`````` ", "```inline```");
assert("```inline`````` ", "inline```");
assert!("```inline".as_value().is_none());
assert!("```inline``` ne".as_value().is_none());
}

View file

@ -1,59 +0,0 @@
use super::TAG;
pub mod error;
pub use error::Error;
// Shared defaults
pub const NEW_LINE: char = '\n';
/// Multi-line [preformatted](https://geminiprotocol.net/docs/gemtext-specification.gmi#in-pre-formatted-mode) entity holder
pub struct Multiline {
pub alt: Option<String>,
pub value: String,
pub completed: bool,
}
impl Multiline {
// Constructors
/// Search in line string for tag open,
/// return Self constructed on success or None
pub fn begin_from(line: &str) -> Option<Self> {
if line.starts_with(TAG) {
let alt = line.trim_start_matches(TAG).trim();
return Some(Self {
alt: match alt.is_empty() {
true => None,
false => Some(alt.to_string()),
},
value: String::new(),
completed: false,
});
}
None
}
/// Continue preformatted buffer from line string,
/// set `completed` as True on close tag found
pub fn continue_from(&mut self, line: &str) -> Result<(), Error> {
// Make sure buffer not completed yet
if self.completed {
return Err(Error::Completed);
}
// Append to value, trim close tag on exists
self.value.push_str(line.trim_end_matches(TAG));
// Line contain close tag
if line.ends_with(TAG) {
self.completed = true;
} else {
self.value.push(NEW_LINE);
}
Ok(())
}
}