Compare commits
35 Commits
Author | SHA1 | Date |
---|---|---|
Cadey Ratio | e9bcdf9f52 | |
Emi Tatsuo | c49c76b5ad | |
Cadey Ratio | f6424dca1a | |
Cadey Ratio | 6524ee688a | |
Emi Tatsuo | aaac9d0c93 | |
Emi Tatsuo | 2566d930bf | |
Emi Tatsuo | 3b4e9c77ec | |
Emi Tatsuo | accd372320 | |
Emi Tatsuo | 5a6208dedf | |
Emi Tatsuo | 3dadcc05ba | |
Cadey Ratio | d08994c0cb | |
Emi Tatsuo | e44decab07 | |
Cadey Ratio | cf73a3bb1e | |
Cadey Ratio | 87a96151b5 | |
Emii Tatsuo | 9bc2ea1159 | |
Emii Tatsuo | 4c86fbba1e | |
Emii Tatsuo | a7fabdc909 | |
Emii Tatsuo | adf82e9d9b | |
Emii Tatsuo | c69ec3b7df | |
Emii Tatsuo | ac88fb60ee | |
Emii Tatsuo | 2f3dd72d90 | |
Emii Tatsuo | 34dca8d92d | |
Cadey Ratio | c743056263 | |
Cadey Ratio | bebfa4d7b1 | |
Cadey Ratio | 725957bf8c | |
Melody Horn | c07d81077a | |
Cadey Ratio | d437ac6e8f | |
Cadey Ratio | f2a251e829 | |
Cadey Ratio | e58a01d14d | |
Melody Horn | 3cd71ce302 | |
Cadey Ratio | 85a3cfda4a | |
Cadey Ratio | 740ac00628 | |
Cadey Ratio | 6058af8b44 | |
Cadey Ratio | bae0ccb136 | |
Cadey Ratio | 6b9070e200 |
13
.drone.yml
13
.drone.yml
|
@ -7,9 +7,6 @@ steps:
|
||||||
pull: always
|
pull: always
|
||||||
commands:
|
commands:
|
||||||
- cargo test --all --all-features
|
- cargo test --all --all-features
|
||||||
when:
|
|
||||||
event:
|
|
||||||
- push
|
|
||||||
|
|
||||||
- name: auto-release
|
- name: auto-release
|
||||||
image: xena/gitea-release
|
image: xena/gitea-release
|
||||||
|
@ -25,6 +22,10 @@ steps:
|
||||||
- push
|
- push
|
||||||
branch:
|
branch:
|
||||||
- main
|
- main
|
||||||
|
trigger:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
- pull_request
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -39,6 +40,6 @@ steps:
|
||||||
environment:
|
environment:
|
||||||
CARGO_TOKEN:
|
CARGO_TOKEN:
|
||||||
from_secret: CARGO_TOKEN
|
from_secret: CARGO_TOKEN
|
||||||
when:
|
trigger:
|
||||||
event:
|
event:
|
||||||
- tag
|
- tag
|
||||||
|
|
10
CHANGELOG.md
10
CHANGELOG.md
|
@ -1,5 +1,15 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## 0.6.2
|
||||||
|
|
||||||
|
Bump gemtext to 0.2.0
|
||||||
|
|
||||||
|
## 0.6.1
|
||||||
|
|
||||||
|
### FIXED
|
||||||
|
|
||||||
|
- [#9] Fixes from @boringcactus for gemtext rendering
|
||||||
|
|
||||||
## 0.6.0
|
## 0.6.0
|
||||||
|
|
||||||
### ADDED
|
### ADDED
|
||||||
|
|
19
Cargo.toml
19
Cargo.toml
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "maj"
|
name = "maj"
|
||||||
version = "0.6.0"
|
version = "0.6.2"
|
||||||
authors = ["Christine Dodrill <me@christine.website>"]
|
authors = ["Christine Dodrill <me@christine.website>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "0BSD"
|
license = "0BSD"
|
||||||
|
@ -10,8 +10,6 @@ repository = "https://tulpa.dev/cadey/maj"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-std = { version = "1.6", optional = true }
|
|
||||||
async-tls = { default-features = false, optional = true, version = "0" }
|
|
||||||
async-trait = { version = "0", optional = true }
|
async-trait = { version = "0", optional = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
mime_guess = "2.0"
|
mime_guess = "2.0"
|
||||||
|
@ -19,15 +17,17 @@ num = "0.2"
|
||||||
num-derive = "0.3"
|
num-derive = "0.3"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
once_cell = "1.4"
|
once_cell = "1.4"
|
||||||
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
|
rustls = { version = "0.19", optional = true, features = ["dangerous_configuration"] }
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
|
tokio-rustls = { version = "0.21", features = ["dangerous_configuration"] }
|
||||||
tokio = { version = "0.2", features = ["full"], optional = true }
|
tokio = { version = "0.3", features = ["full"] }
|
||||||
url = "2"
|
url = "2"
|
||||||
webpki-roots = { version = "0.20", optional = true }
|
webpki-roots = { version = "0.20", optional = true }
|
||||||
webpki = { version = "0.21.0", optional = true }
|
webpki = { version = "0.21.0", optional = true }
|
||||||
|
|
||||||
|
gemtext = { path = "./gemtext" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_env_logger = "0.4"
|
pretty_env_logger = "0.4"
|
||||||
|
|
||||||
|
@ -35,12 +35,8 @@ pretty_env_logger = "0.4"
|
||||||
default = ["client", "server"]
|
default = ["client", "server"]
|
||||||
|
|
||||||
client = [
|
client = [
|
||||||
"tokio-rustls",
|
|
||||||
"webpki",
|
"webpki",
|
||||||
"webpki-roots",
|
"webpki-roots",
|
||||||
"tokio",
|
|
||||||
"async-std",
|
|
||||||
"async-tls/client"
|
|
||||||
]
|
]
|
||||||
|
|
||||||
server = [
|
server = [
|
||||||
|
@ -48,12 +44,11 @@ server = [
|
||||||
"webpki",
|
"webpki",
|
||||||
"webpki-roots",
|
"webpki-roots",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"async-std",
|
|
||||||
"async-tls/server"
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
"./gemtext",
|
||||||
"./majc",
|
"./majc",
|
||||||
"./majd",
|
"./majd",
|
||||||
"./site",
|
"./site",
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
[package]
|
||||||
|
name = "gemtext"
|
||||||
|
version = "0.2.1"
|
||||||
|
authors = ["Christine Dodrill <me@christine.website>"]
|
||||||
|
edition = "2018"
|
||||||
|
license = "0BSD"
|
||||||
|
description = "A gemini client and server for Rust"
|
||||||
|
repository = "https://tulpa.dev/cadey/maj"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
pretty_env_logger = "0.4"
|
|
@ -0,0 +1,486 @@
|
||||||
|
/// This module implements a simple text/gemini parser based on the description
|
||||||
|
/// here: https://gemini.circumlunar.space/docs/specification.html
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
/// Build a gemini document up from a series of nodes.
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Builder {
|
||||||
|
nodes: Vec<Node>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Builder {
|
||||||
|
pub fn new() -> Builder {
|
||||||
|
Builder::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn text<T: Into<String>>(mut self, data: T) -> Builder {
|
||||||
|
self.nodes.push(Node::Text(data.into()));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Append a single blank line to the document
|
||||||
|
///
|
||||||
|
/// This is equivilent to calling [`text()`] with an empty string, or pushing a blank
|
||||||
|
/// [`Node`]
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use gemtext::Builder;
|
||||||
|
/// let greeting = Builder::new()
|
||||||
|
/// .text("Hello")
|
||||||
|
/// .blank_line()
|
||||||
|
/// .text("universe")
|
||||||
|
/// .to_string();
|
||||||
|
///
|
||||||
|
/// assert_eq!(greeting.trim(), "Hello\n\nuniverse");
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`text()`]: Self::text()
|
||||||
|
pub fn blank_line(mut self) -> Self {
|
||||||
|
self.nodes.push(Node::blank());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn link<T: Into<String>>(mut self, to: T, name: Option<String>) -> Builder {
|
||||||
|
self.nodes.push(Node::Link {
|
||||||
|
to: to.into(),
|
||||||
|
name: name,
|
||||||
|
});
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn preformatted<A, T>(mut self, alt_text: A, data: T) -> Builder
|
||||||
|
where
|
||||||
|
A: Into<String>,
|
||||||
|
T: Into<String>,
|
||||||
|
{
|
||||||
|
self.nodes.push(Node::Preformatted { alt: alt_text.into(), body: data.into() });
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn heading<T: Into<String>>(mut self, level: u8, body: T) -> Builder {
|
||||||
|
self.nodes.push(Node::Heading {
|
||||||
|
level: level,
|
||||||
|
body: body.into(),
|
||||||
|
});
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_item<T: Into<String>>(mut self, item: T) -> Builder {
|
||||||
|
self.nodes.push(Node::ListItem(item.into()));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quote<T: Into<String>>(mut self, body: T) -> Builder {
|
||||||
|
self.nodes.push(Node::Quote(body.into()));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> Vec<Node> {
|
||||||
|
self.nodes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToString for Builder {
|
||||||
|
/// Render a document to a string
|
||||||
|
///
|
||||||
|
/// This produces a text/gemini compliant text document, represented as a string
|
||||||
|
fn to_string(&self) -> String {
|
||||||
|
let len: usize = self.nodes.iter().map(Node::estimate_len).sum(); // sum up node lengths
|
||||||
|
let mut bytes = Vec::with_capacity(len + self.nodes.len()); // add in inter-node newlines
|
||||||
|
render(self, &mut bytes).unwrap(); // Writing to a string shouldn't produce errors
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
// This is safe because bytes is composed of Strings. We could have this as
|
||||||
|
// pure safe code by replicating the `render()` method and switching it to use
|
||||||
|
// a fmt::Write (or even `String::push()`)instead of a io::Write, but this has
|
||||||
|
// the same effect, with much DRYer code.
|
||||||
|
String::from_utf8_unchecked(bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsRef<[Node]> for Builder {
|
||||||
|
/// Get a reference to the internal node list of this builder
|
||||||
|
fn as_ref(&self) -> &[Node] {
|
||||||
|
self.nodes.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AsMut<[Node]> for Builder {
|
||||||
|
/// Get a mutable reference to the internal node list of this builder
|
||||||
|
fn as_mut(&mut self) -> &mut [Node] {
|
||||||
|
self.nodes.as_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Builder> for Vec<Node> {
|
||||||
|
/// Convert into a collection of [`Node`]s.
|
||||||
|
///
|
||||||
|
/// Equivilent to calling [`Builder::build()`]
|
||||||
|
fn from(builder: Builder) -> Self {
|
||||||
|
builder.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Render a set of nodes as a document to a writer.
|
||||||
|
pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()> {
|
||||||
|
use Node::*;
|
||||||
|
|
||||||
|
for node in nodes.as_ref() {
|
||||||
|
match node {
|
||||||
|
Text(body) => {
|
||||||
|
let special_prefixes = ["=>", "```", "#", "*", ">"];
|
||||||
|
if special_prefixes.iter().any(|prefix| body.starts_with(prefix)) {
|
||||||
|
write!(out, " ")?;
|
||||||
|
}
|
||||||
|
write!(out, "{}\n", body)?
|
||||||
|
},
|
||||||
|
Link { to, name } => match name {
|
||||||
|
Some(name) => write!(out, "=> {} {}\n", to, name)?,
|
||||||
|
None => write!(out, "=> {}\n", to)?,
|
||||||
|
},
|
||||||
|
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n", alt, body)?,
|
||||||
|
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(*level as usize), body)?,
|
||||||
|
ListItem(body) => write!(out, "* {}\n", body)?,
|
||||||
|
Quote(body) => write!(out, "> {}\n", body)?,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Individual nodes of the document. Each node correlates to a line in the file.
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||||
|
pub enum Node {
|
||||||
|
/// Text lines are the most fundamental line type - any line which does not
|
||||||
|
/// match the definition of another line type defined below defaults to
|
||||||
|
/// being a text line. The majority of lines in a typical text/gemini document will be text lines.
|
||||||
|
Text(String),
|
||||||
|
|
||||||
|
/// Lines beginning with the two characters "=>" are link lines, which have the following syntax:
|
||||||
|
///
|
||||||
|
/// ```gemini
|
||||||
|
/// =>[<whitespace>]<URL>[<whitespace><USER-FRIENDLY LINK NAME>]
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// where:
|
||||||
|
///
|
||||||
|
/// * `<whitespace>` is any non-zero number of consecutive spaces or tabs
|
||||||
|
/// * Square brackets indicate that the enclosed content is optional.
|
||||||
|
/// * `<URL>` is a URL, which may be absolute or relative. If the URL
|
||||||
|
/// does not include a scheme, a scheme of `gemini://` is implied.
|
||||||
|
Link { to: String, name: Option<String> },
|
||||||
|
|
||||||
|
/// Any line whose first three characters are "```" (i.e. three consecutive
|
||||||
|
/// back ticks with no leading whitespace) are preformatted toggle lines.
|
||||||
|
/// These lines should NOT be included in the rendered output shown to the
|
||||||
|
/// user. Instead, these lines toggle the parser between preformatted mode
|
||||||
|
/// being "on" or "off". Preformatted mode should be "off" at the beginning
|
||||||
|
/// of a document. The current status of preformatted mode is the only
|
||||||
|
/// internal state a parser is required to maintain. When preformatted mode
|
||||||
|
/// is "on", the usual rules for identifying line types are suspended, and
|
||||||
|
/// all lines should be identified as preformatted text lines (see 5.4.4).
|
||||||
|
///
|
||||||
|
/// Preformatted text lines should be presented to the user in a "neutral",
|
||||||
|
/// monowidth font without any alteration to whitespace or stylistic
|
||||||
|
/// enhancements. Graphical clients should use scrolling mechanisms to present
|
||||||
|
/// preformatted text lines which are longer than the client viewport, in
|
||||||
|
/// preference to wrapping. In displaying preformatted text lines, clients
|
||||||
|
/// should keep in mind applications like ASCII art and computer source
|
||||||
|
/// code: in particular, source code in languages with significant whitespace
|
||||||
|
/// (e.g. Python) should be able to be copied and pasted from the client into
|
||||||
|
/// a file and interpreted/compiled without any problems arising from the
|
||||||
|
/// client's manner of displaying them.
|
||||||
|
///
|
||||||
|
/// The first preformatted toggle of a document is often followed by a short
|
||||||
|
/// string, which acts as alt-text for the preformatted block. This is also
|
||||||
|
/// often used to denote the language of code in a block of text. For example,
|
||||||
|
/// a block starting with the text `\`\`\`rust` may be interpreted as rust
|
||||||
|
/// code, and a block starting with `\`\`\` An ascii art owl` would be
|
||||||
|
/// described aptly to visually impaired users using a screen reader. The alt
|
||||||
|
/// text may be separated from the toggle by whitespace. `gemtext` currently
|
||||||
|
/// renders alt text without this separation.
|
||||||
|
///
|
||||||
|
/// To create a preformatted block with no alt text, simply pass a zero-length
|
||||||
|
/// string as alt text.
|
||||||
|
Preformatted { alt: String, body: String },
|
||||||
|
|
||||||
|
/// Lines beginning with "#" are heading lines. Heading lines consist of one,
|
||||||
|
/// two or three consecutive "#" characters, followed by optional whitespace,
|
||||||
|
/// followed by heading text. The number of # characters indicates the "level"
|
||||||
|
/// of header; #, ## and ### can be thought of as analogous to `<h1>`, `<h2>`
|
||||||
|
/// and `<h3>` in HTML.
|
||||||
|
///
|
||||||
|
/// Heading text should be presented to the user, and clients MAY use special
|
||||||
|
/// formatting, e.g. a larger or bold font, to indicate its status as a header
|
||||||
|
/// (simple clients may simply print the line, including its leading #s,
|
||||||
|
/// without any styling at all). However, the main motivation for the
|
||||||
|
/// definition of heading lines is not stylistic but to provide a
|
||||||
|
/// machine-readable representation of the internal structure of the document.
|
||||||
|
/// Advanced clients can use this information to, e.g. display an automatically
|
||||||
|
/// generated and hierarchically formatted "table of contents" for a long
|
||||||
|
/// document in a side-pane, allowing users to easily jump to specific sections
|
||||||
|
/// without excessive scrolling. CMS-style tools automatically generating menus
|
||||||
|
/// or Atom/RSS feeds for a directory of text/gemini files can use first
|
||||||
|
/// heading in the file as a human-friendly title.
|
||||||
|
Heading { level: u8, body: String },
|
||||||
|
|
||||||
|
/// Lines beginning with "* " are unordered list items. This line type exists
|
||||||
|
/// purely for stylistic reasons. The * may be replaced in advanced clients by
|
||||||
|
/// a bullet symbol. Any text after the "* " should be presented to the user as
|
||||||
|
/// if it were a text line, i.e. wrapped to fit the viewport and formatted
|
||||||
|
/// "nicely". Advanced clients can take the space of the bullet symbol into
|
||||||
|
/// account when wrapping long list items to ensure that all lines of text
|
||||||
|
/// corresponding to the item are offset an equal distance from the left of the screen.
|
||||||
|
ListItem(String),
|
||||||
|
|
||||||
|
/// Lines beginning with ">" are quote lines. This line type exists so that
|
||||||
|
/// advanced clients may use distinct styling to convey to readers the important
|
||||||
|
/// semantic information that certain text is being quoted from an external
|
||||||
|
/// source. For example, when wrapping long lines to the the viewport, each
|
||||||
|
/// resultant line may have a ">" symbol placed at the front.
|
||||||
|
Quote(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
pub fn blank() -> Node {
|
||||||
|
Node::Text("".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cheaply estimate the length of this node
|
||||||
|
///
|
||||||
|
/// This measures length in bytes, *not characters*. So if the user includes
|
||||||
|
/// non-ascii characters, a single one of these characters may add several bytes to
|
||||||
|
/// the length, despite only displaying as one character.
|
||||||
|
///
|
||||||
|
/// This does include any newlines, but not any trailing newlines. For example, a
|
||||||
|
/// preformatted text block containing a single line reading "trans rights! 🏳️‍⚧️"
|
||||||
|
/// would have a length of 30: 3 backticks, a newline, the text (including 16 bytes
|
||||||
|
/// for the trans flag), another newline, and another 3 backticks.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use gemtext::Node;
|
||||||
|
/// let simple_text = Node::Text(String::from("Henlo worl"));
|
||||||
|
/// let linky_link = Node::Link { to: "gemini://cetacean.club/maj/".to_string(), name: Some("Maj".to_string()) };
|
||||||
|
/// let human_rights = Node::Preformatted {
|
||||||
|
/// alt: "".to_string(),
|
||||||
|
/// body: "trans rights! 🏳️‍⚧️".to_string(),
|
||||||
|
/// };
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// simple_text.estimate_len(),
|
||||||
|
/// "Henlo worl".as_bytes().len()
|
||||||
|
/// );
|
||||||
|
/// assert_eq!(
|
||||||
|
/// linky_link.estimate_len(),
|
||||||
|
/// "=> gemini://cetacean.club/maj/ Maj".as_bytes().len()
|
||||||
|
/// );
|
||||||
|
/// assert_eq!(
|
||||||
|
/// human_rights.estimate_len(),
|
||||||
|
/// "```\ntrans rights! 🏳️‍⚧️\n```".as_bytes().len()
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
pub fn estimate_len(&self) -> usize {
|
||||||
|
match self {
|
||||||
|
Self::Text(text) => text.len(),
|
||||||
|
Self::Link { to, name } => 3 + to.as_bytes().len() +
|
||||||
|
name.as_ref().map(|n| n.as_bytes().len() + 1).unwrap_or(0),
|
||||||
|
Self::Preformatted { alt, body } => alt.as_bytes().len()
|
||||||
|
+ body.as_bytes().len() + 8,
|
||||||
|
Self::Heading { level, body } => *level as usize + 1 + body.as_bytes().len(),
|
||||||
|
Self::ListItem(item) | Self::Quote(item)=> 2 + item.as_bytes().len(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(doc: &str) -> Vec<Node> {
|
||||||
|
let mut result: Vec<Node> = vec![];
|
||||||
|
let mut collect_preformatted: bool = false;
|
||||||
|
let mut preformatted_buffer: Vec<u8> = vec![];
|
||||||
|
let mut alt = "";
|
||||||
|
|
||||||
|
for line in doc.lines() {
|
||||||
|
if let Some(trailing) = line.strip_prefix("```") {
|
||||||
|
collect_preformatted = !collect_preformatted;
|
||||||
|
if !collect_preformatted {
|
||||||
|
result.push(Node::Preformatted {
|
||||||
|
alt: alt.to_string(),
|
||||||
|
body: String::from_utf8(preformatted_buffer)
|
||||||
|
.unwrap()
|
||||||
|
.trim_end()
|
||||||
|
.to_string(),
|
||||||
|
});
|
||||||
|
preformatted_buffer = vec![];
|
||||||
|
} else {
|
||||||
|
alt = trailing.trim();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if collect_preformatted && line != "```" {
|
||||||
|
write!(preformatted_buffer, "{}\n", line).unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quotes
|
||||||
|
if line.starts_with(">") {
|
||||||
|
result.push(Node::Quote(line[1..].trim().to_string()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// List items
|
||||||
|
if line.starts_with("*") {
|
||||||
|
result.push(Node::ListItem(line[1..].trim().to_string()));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Headings
|
||||||
|
if line.starts_with("###") {
|
||||||
|
result.push(Node::Heading {
|
||||||
|
level: 3,
|
||||||
|
body: line[3..].trim().to_string(),
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if line.starts_with("##") {
|
||||||
|
result.push(Node::Heading {
|
||||||
|
level: 2,
|
||||||
|
body: line[2..].trim().to_string(),
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if line.starts_with("#") {
|
||||||
|
result.push(Node::Heading {
|
||||||
|
level: 1,
|
||||||
|
body: line[1..].trim().to_string(),
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Links
|
||||||
|
if line.starts_with("=>") {
|
||||||
|
let sp = line[2..].split_ascii_whitespace().collect::<Vec<&str>>();
|
||||||
|
|
||||||
|
match sp.len() {
|
||||||
|
1 => result.push(Node::Link {
|
||||||
|
to: sp[0].trim().to_string(),
|
||||||
|
name: None,
|
||||||
|
}),
|
||||||
|
_ => result.push(Node::Link {
|
||||||
|
to: sp[0].trim().to_string(),
|
||||||
|
name: Some(sp[1..].join(" ").trim().to_string()),
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push(Node::Text(line.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn basic() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = include_str!("../../majc/src/help.gmi");
|
||||||
|
let doc = super::parse(msg);
|
||||||
|
assert_ne!(doc.len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn quote() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = ">hi there";
|
||||||
|
let expected: Vec<Node> = vec![Node::Quote("hi there".to_string())];
|
||||||
|
assert_eq!(expected, parse(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = "*hi there";
|
||||||
|
let expected: Vec<Node> = vec![Node::ListItem("hi there".to_string())];
|
||||||
|
assert_eq!(expected, parse(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn preformatted() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = "```hi there\n\
|
||||||
|
obi-wan kenobi\n\
|
||||||
|
```\n\
|
||||||
|
\n\
|
||||||
|
Test\n";
|
||||||
|
let expected: Vec<Node> = vec![
|
||||||
|
Node::Preformatted{ alt: "hi there".to_string(), body: "obi-wan kenobi".to_string() },
|
||||||
|
Node::Text(String::new()),
|
||||||
|
Node::Text("Test".to_string()),
|
||||||
|
];
|
||||||
|
assert_eq!(expected, parse(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn header() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = "#hi\n##there\n### my friends";
|
||||||
|
let expected: Vec<Node> = vec![
|
||||||
|
Node::Heading {
|
||||||
|
level: 1,
|
||||||
|
body: "hi".to_string(),
|
||||||
|
},
|
||||||
|
Node::Heading {
|
||||||
|
level: 2,
|
||||||
|
body: "there".to_string(),
|
||||||
|
},
|
||||||
|
Node::Heading {
|
||||||
|
level: 3,
|
||||||
|
body: "my friends".to_string(),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
assert_eq!(expected, parse(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn link() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = "=>/\n=> / Go home";
|
||||||
|
let expected: Vec<Node> = vec![
|
||||||
|
Node::Link {
|
||||||
|
to: "/".to_string(),
|
||||||
|
name: None,
|
||||||
|
},
|
||||||
|
Node::Link {
|
||||||
|
to: "/".to_string(),
|
||||||
|
name: Some("Go home".to_string()),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
assert_eq!(expected, parse(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ambiguous_preformatted() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let msg = include_str!("../../testdata/ambig_preformatted.gmi");
|
||||||
|
let expected: Vec<Node> = vec![
|
||||||
|
Node::Preformatted { alt: "foo".to_string(), body: "FOO".to_string() },
|
||||||
|
Node::Text("Foo bar".to_string()),
|
||||||
|
];
|
||||||
|
assert_eq!(expected, parse(msg));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ambiguous_text() {
|
||||||
|
let _ = pretty_env_logger::try_init();
|
||||||
|
let original = Node::Text("#1 World's Best Coder".to_string());
|
||||||
|
let expected = " #1 World's Best Coder\n";
|
||||||
|
let mut rendered: Vec<u8> = vec![];
|
||||||
|
render(vec![original], &mut rendered).unwrap();
|
||||||
|
let rendered = String::from_utf8(rendered).unwrap();
|
||||||
|
assert_eq!(expected, rendered)
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ cursive = "0.15"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
url = "2"
|
url = "2"
|
||||||
webpki = "0.21.0"
|
webpki = "0.21.0"
|
||||||
rustls = { version = "0.18", features = ["dangerous_configuration"] }
|
rustls = { version = "0.19", features = ["dangerous_configuration"] }
|
||||||
smol = { version = "0.3", features = ["tokio02"] }
|
smol = { version = "0.3", features = ["tokio02"] }
|
||||||
|
|
||||||
maj = { path = ".." }
|
maj = { path = ".." }
|
||||||
|
|
|
@ -8,6 +8,7 @@ use cursive::{
|
||||||
use maj::{self, Response};
|
use maj::{self, Response};
|
||||||
use rustls::ClientConfig;
|
use rustls::ClientConfig;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
/// The state of the browser.
|
/// The state of the browser.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -153,13 +154,17 @@ pub fn show(siv: &mut Cursive, url: &str, resp: Response) {
|
||||||
TemporaryRedirect => {
|
TemporaryRedirect => {
|
||||||
let st = siv.user_data::<State>().unwrap();
|
let st = siv.user_data::<State>().unwrap();
|
||||||
st.history.pop();
|
st.history.pop();
|
||||||
open(siv, resp.meta.as_str());
|
let u = Url::parse(url).unwrap();
|
||||||
|
let u = u.join(resp.meta.as_str()).unwrap();
|
||||||
|
open(siv, u.as_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
PermanentRedirect => {
|
PermanentRedirect => {
|
||||||
let st = siv.user_data::<State>().unwrap();
|
let st = siv.user_data::<State>().unwrap();
|
||||||
st.history.pop();
|
st.history.pop();
|
||||||
open(siv, resp.meta.as_str());
|
let u = Url::parse(url).unwrap();
|
||||||
|
let u = u.join(resp.meta.as_str()).unwrap();
|
||||||
|
open(siv, u.as_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
Input => {
|
Input => {
|
||||||
|
@ -219,7 +224,7 @@ pub fn render(body: &str) -> StyledString {
|
||||||
styled.append(StyledString::styled(name, Style::from(Effect::Underline)))
|
styled.append(StyledString::styled(name, Style::from(Effect::Underline)))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Preformatted(data) => styled.append(StyledString::plain(data)),
|
Preformatted { body, .. } => styled.append(StyledString::plain(body)),
|
||||||
Heading { level, body } => styled.append(StyledString::styled(
|
Heading { level, body } => styled.append(StyledString::styled(
|
||||||
format!("{} {}", "#".repeat(level as usize), body),
|
format!("{} {}", "#".repeat(level as usize), body),
|
||||||
Style::from(Effect::Bold),
|
Style::from(Effect::Bold),
|
||||||
|
|
|
@ -8,15 +8,14 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
async-std = "1.5"
|
|
||||||
async-trait = "0"
|
async-trait = "0"
|
||||||
atom_syndication = "0.9"
|
atom_syndication = "0.9"
|
||||||
chrono = "*"
|
chrono = "*"
|
||||||
log = "0"
|
log = "0"
|
||||||
pretty_env_logger = "0.4"
|
pretty_env_logger = "0.4"
|
||||||
webpki = "0.21.0"
|
webpki = "0.21.0"
|
||||||
rustls = { version = "0.18", features = ["dangerous_configuration"] }
|
rustls = { version = "0.19", features = ["dangerous_configuration"] }
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
tokio = { version = "0.2", features = ["full"] }
|
tokio = { version = "0.3", features = ["full"] }
|
||||||
|
|
||||||
maj = { path = "../..", features = ["server", "client"], default-features = false }
|
maj = { path = "../..", features = ["server", "client"], default-features = false }
|
||||||
|
|
|
@ -25,7 +25,7 @@ fn gem_to_md(tcana: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
|
||||||
Some(name) => write!(out, "[{}]({})\n\n", name, to)?,
|
Some(name) => write!(out, "[{}]({})\n\n", name, to)?,
|
||||||
None => write!(out, "[{0}]({0})", to)?,
|
None => write!(out, "[{0}]({0})", to)?,
|
||||||
},
|
},
|
||||||
Preformatted(body) => write!(out, "```\n{}\n```\n\n", body)?,
|
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n\n", alt, body)?,
|
||||||
Heading { level, body } => {
|
Heading { level, body } => {
|
||||||
write!(out, "##{} {}\n\n", "#".repeat(level as usize), body)?
|
write!(out, "##{} {}\n\n", "#".repeat(level as usize), body)?
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,6 @@ let
|
||||||
"https://github.com/nix-community/NUR/archive/master.tar.gz") {
|
"https://github.com/nix-community/NUR/archive/master.tar.gz") {
|
||||||
inherit pkgs;
|
inherit pkgs;
|
||||||
};
|
};
|
||||||
tex = with pkgs;
|
|
||||||
texlive.combine { inherit (texlive) scheme-medium bitter titlesec; };
|
|
||||||
in pkgs.mkShell {
|
in pkgs.mkShell {
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
pkgs.latest.rustChannels.stable.rust
|
pkgs.latest.rustChannels.stable.rust
|
||||||
|
@ -15,10 +13,6 @@ in pkgs.mkShell {
|
||||||
|
|
||||||
pkg-config
|
pkg-config
|
||||||
ncurses
|
ncurses
|
||||||
|
|
||||||
kindlegen
|
|
||||||
nur.repos.mic92.pandoc-bin
|
|
||||||
tex
|
|
||||||
];
|
];
|
||||||
|
|
||||||
RUST_LOG="info,majsite=debug,majsite::server=debug";
|
RUST_LOG="info,majsite=debug,majsite::server=debug";
|
||||||
|
|
|
@ -9,7 +9,6 @@ build = "build.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1"
|
anyhow = "1"
|
||||||
async-std = "1.5"
|
|
||||||
async-trait = "0"
|
async-trait = "0"
|
||||||
dnd_dice_roller = "0.3"
|
dnd_dice_roller = "0.3"
|
||||||
env_logger = "0"
|
env_logger = "0"
|
||||||
|
@ -17,13 +16,14 @@ log = "0"
|
||||||
mime = "0.3.0"
|
mime = "0.3.0"
|
||||||
percent-encoding = "2"
|
percent-encoding = "2"
|
||||||
rand = "0"
|
rand = "0"
|
||||||
rustls = { version = "0.18", features = ["dangerous_configuration"] }
|
rustls = { version = "0.19", features = ["dangerous_configuration"] }
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
smol = { version = "0.3", features = ["tokio02"] }
|
smol = { version = "0.3", features = ["tokio02"] }
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
url = "2"
|
url = "2"
|
||||||
warp = "0.2"
|
warp = "0.2"
|
||||||
|
tokio = { version = "0.3", features = ["rt"] }
|
||||||
|
|
||||||
maj = { path = "..", features = ["server"], default-features = false }
|
maj = { path = "..", features = ["server"], default-features = false }
|
||||||
|
|
||||||
|
|
|
@ -80,7 +80,12 @@ fn gemtext_to_html(inp: Vec<u8>) -> (String, impl ToHtml) {
|
||||||
name.as_ref().or(Some(&to.to_string())).unwrap()
|
name.as_ref().or(Some(&to.to_string())).unwrap()
|
||||||
)
|
)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
Preformatted(body) => write!(buf, "<code><pre>{}</pre></code>", body).unwrap(),
|
Preformatted { alt, body } => write!(
|
||||||
|
buf,
|
||||||
|
"<code><pre title = \"{}\">{}</pre></code>",
|
||||||
|
alt.replace("\"", "\\\"").replace("\\", "\\\\"),
|
||||||
|
body
|
||||||
|
).unwrap(),
|
||||||
ListItem(body) => write!(buf, "<li>{}</li>", body).unwrap(),
|
ListItem(body) => write!(buf, "<li>{}</li>", body).unwrap(),
|
||||||
Quote(body) => write!(buf, "<blockquote>{}</blockquote>", body).unwrap(),
|
Quote(body) => write!(buf, "<blockquote>{}</blockquote>", body).unwrap(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use async_std::task;
|
|
||||||
use rustls::{
|
use rustls::{
|
||||||
internal::pemfile::{certs, pkcs8_private_keys},
|
internal::pemfile::{certs, pkcs8_private_keys},
|
||||||
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
|
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, BufReader},
|
io::{self, BufReader},
|
||||||
|
@ -64,7 +64,8 @@ fn load_keys(path: &Path) -> io::Result<Vec<PrivateKey>> {
|
||||||
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
|
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), maj::server::Error> {
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), maj::server::Error> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let opts = Options::from_args();
|
let opts = Options::from_args();
|
||||||
let certs = load_certs(&opts.cert).unwrap();
|
let certs = load_certs(&opts.cert).unwrap();
|
||||||
|
@ -96,7 +97,7 @@ fn main() -> Result<(), maj::server::Error> {
|
||||||
thread::spawn(move || http::run(h.clone(), port));
|
thread::spawn(move || http::run(h.clone(), port));
|
||||||
}
|
}
|
||||||
|
|
||||||
task::block_on(maj::server::serve(h.clone(), config, opts.host, opts.port))?;
|
maj::server::serve(h.clone(), config, opts.host, opts.port).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ async fn dice(req: Request) -> Result<Response, Error> {
|
||||||
.text("")
|
.text("")
|
||||||
.text(format!("You rolled {} and you got:", dice))
|
.text(format!("You rolled {} and you got:", dice))
|
||||||
.text("")
|
.text("")
|
||||||
.preformatted(format!("{}", dice_roll(dice)?))
|
.preformatted("", format!("{}", dice_roll(dice)?))
|
||||||
.text("")
|
.text("")
|
||||||
.link("/dice", Some("Do another roll".to_string()));
|
.link("/dice", Some("Do another roll".to_string()));
|
||||||
|
|
||||||
|
|
341
src/gemini.rs
341
src/gemini.rs
|
@ -1,340 +1 @@
|
||||||
/// This module implements a simple text/gemini parser based on the description
|
pub use gemtext::*;
|
||||||
/// here: https://gemini.circumlunar.space/docs/specification.html
|
|
||||||
use std::io::{self, Write};
|
|
||||||
|
|
||||||
/// Build a gemini document up from a series of nodes.
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Builder {
|
|
||||||
nodes: Vec<Node>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Builder {
|
|
||||||
pub fn new() -> Builder {
|
|
||||||
Builder::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn text<T: Into<String>>(mut self, data: T) -> Builder {
|
|
||||||
self.nodes.push(Node::Text(data.into()));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn link<T: Into<String>>(mut self, to: T, name: Option<String>) -> Builder {
|
|
||||||
self.nodes.push(Node::Link {
|
|
||||||
to: to.into(),
|
|
||||||
name: name,
|
|
||||||
});
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn preformatted<T: Into<String>>(mut self, data: T) -> Builder {
|
|
||||||
self.nodes.push(Node::Preformatted(data.into()));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn heading<T: Into<String>>(mut self, level: u8, body: T) -> Builder {
|
|
||||||
self.nodes.push(Node::Heading {
|
|
||||||
level: level,
|
|
||||||
body: body.into(),
|
|
||||||
});
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn list_item<T: Into<String>>(mut self, item: T) -> Builder {
|
|
||||||
self.nodes.push(Node::ListItem(item.into()));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn quote<T: Into<String>>(mut self, body: T) -> Builder {
|
|
||||||
self.nodes.push(Node::Quote(body.into()));
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn build(self) -> Vec<Node> {
|
|
||||||
self.nodes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Render a set of nodes as a document to a writer.
|
|
||||||
pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
|
|
||||||
use Node::*;
|
|
||||||
|
|
||||||
for node in nodes {
|
|
||||||
match node {
|
|
||||||
Text(body) => write!(out, "{}\n", body)?,
|
|
||||||
Link { to, name } => match name {
|
|
||||||
Some(name) => write!(out, "=> {} {}\n", to, name)?,
|
|
||||||
None => write!(out, "=> {}\n", to)?,
|
|
||||||
},
|
|
||||||
Preformatted(body) => write!(out, "```\n{}\n```\n", body)?,
|
|
||||||
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(level as usize), body)?,
|
|
||||||
ListItem(body) => write!(out, "* {}\n", body)?,
|
|
||||||
Quote(body) => write!(out, "> {}\n", body)?,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Individual nodes of the document. Each node correlates to a line in the file.
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub enum Node {
|
|
||||||
/// Text lines are the most fundamental line type - any line which does not
|
|
||||||
/// match the definition of another line type defined below defaults to
|
|
||||||
/// being a text line. The majority of lines in a typical text/gemini document will be text lines.
|
|
||||||
Text(String),
|
|
||||||
|
|
||||||
/// Lines beginning with the two characters "=>" are link lines, which have the following syntax:
|
|
||||||
///
|
|
||||||
/// ```gemini
|
|
||||||
/// =>[<whitespace>]<URL>[<whitespace><USER-FRIENDLY LINK NAME>]
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// where:
|
|
||||||
///
|
|
||||||
/// * `<whitespace>` is any non-zero number of consecutive spaces or tabs
|
|
||||||
/// * Square brackets indicate that the enclosed content is optional.
|
|
||||||
/// * `<URL>` is a URL, which may be absolute or relative. If the URL
|
|
||||||
/// does not include a scheme, a scheme of `gemini://` is implied.
|
|
||||||
Link { to: String, name: Option<String> },
|
|
||||||
|
|
||||||
/// Any line whose first three characters are "```" (i.e. three consecutive
|
|
||||||
/// back ticks with no leading whitespace) are preformatted toggle lines.
|
|
||||||
/// These lines should NOT be included in the rendered output shown to the
|
|
||||||
/// user. Instead, these lines toggle the parser between preformatted mode
|
|
||||||
/// being "on" or "off". Preformatted mode should be "off" at the beginning
|
|
||||||
/// of a document. The current status of preformatted mode is the only
|
|
||||||
/// internal state a parser is required to maintain. When preformatted mode
|
|
||||||
/// is "on", the usual rules for identifying line types are suspended, and
|
|
||||||
/// all lines should be identified as preformatted text lines (see 5.4.4).
|
|
||||||
///
|
|
||||||
/// Preformatted text lines should be presented to the user in a "neutral",
|
|
||||||
/// monowidth font without any alteration to whitespace or stylistic
|
|
||||||
/// enhancements. Graphical clients should use scrolling mechanisms to present
|
|
||||||
/// preformatted text lines which are longer than the client viewport, in
|
|
||||||
/// preference to wrapping. In displaying preformatted text lines, clients
|
|
||||||
/// should keep in mind applications like ASCII art and computer source
|
|
||||||
/// code: in particular, source code in languages with significant whitespace
|
|
||||||
/// (e.g. Python) should be able to be copied and pasted from the client into
|
|
||||||
/// a file and interpreted/compiled without any problems arising from the
|
|
||||||
/// client's manner of displaying them.
|
|
||||||
Preformatted(String),
|
|
||||||
|
|
||||||
/// Lines beginning with "#" are heading lines. Heading lines consist of one,
|
|
||||||
/// two or three consecutive "#" characters, followed by optional whitespace,
|
|
||||||
/// followed by heading text. The number of # characters indicates the "level"
|
|
||||||
/// of header; #, ## and ### can be thought of as analogous to `<h1>`, `<h2>`
|
|
||||||
/// and `<h3>` in HTML.
|
|
||||||
///
|
|
||||||
/// Heading text should be presented to the user, and clients MAY use special
|
|
||||||
/// formatting, e.g. a larger or bold font, to indicate its status as a header
|
|
||||||
/// (simple clients may simply print the line, including its leading #s,
|
|
||||||
/// without any styling at all). However, the main motivation for the
|
|
||||||
/// definition of heading lines is not stylistic but to provide a
|
|
||||||
/// machine-readable representation of the internal structure of the document.
|
|
||||||
/// Advanced clients can use this information to, e.g. display an automatically
|
|
||||||
/// generated and hierarchically formatted "table of contents" for a long
|
|
||||||
/// document in a side-pane, allowing users to easily jump to specific sections
|
|
||||||
/// without excessive scrolling. CMS-style tools automatically generating menus
|
|
||||||
/// or Atom/RSS feeds for a directory of text/gemini files can use first
|
|
||||||
/// heading in the file as a human-friendly title.
|
|
||||||
Heading { level: u8, body: String },
|
|
||||||
|
|
||||||
/// Lines beginning with "* " are unordered list items. This line type exists
|
|
||||||
/// purely for stylistic reasons. The * may be replaced in advanced clients by
|
|
||||||
/// a bullet symbol. Any text after the "* " should be presented to the user as
|
|
||||||
/// if it were a text line, i.e. wrapped to fit the viewport and formatted
|
|
||||||
/// "nicely". Advanced clients can take the space of the bullet symbol into
|
|
||||||
/// account when wrapping long list items to ensure that all lines of text
|
|
||||||
/// corresponding to the item are offset an equal distance from the left of the screen.
|
|
||||||
ListItem(String),
|
|
||||||
|
|
||||||
/// Lines beginning with ">" are quote lines. This line type exists so that
|
|
||||||
/// advanced clients may use distinct styling to convey to readers the important
|
|
||||||
/// semantic information that certain text is being quoted from an external
|
|
||||||
/// source. For example, when wrapping long lines to the the viewport, each
|
|
||||||
/// resultant line may have a ">" symbol placed at the front.
|
|
||||||
Quote(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Node {
|
|
||||||
pub fn blank() -> Node {
|
|
||||||
Node::Text("".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(doc: &str) -> Vec<Node> {
|
|
||||||
let mut result: Vec<Node> = vec![];
|
|
||||||
let mut collect_preformatted: bool = false;
|
|
||||||
let mut preformatted_buffer: Vec<u8> = vec![];
|
|
||||||
|
|
||||||
for line in doc.lines() {
|
|
||||||
if line.starts_with("```") {
|
|
||||||
collect_preformatted = !collect_preformatted;
|
|
||||||
if !collect_preformatted {
|
|
||||||
result.push(Node::Preformatted(
|
|
||||||
String::from_utf8(preformatted_buffer)
|
|
||||||
.unwrap()
|
|
||||||
.trim_end()
|
|
||||||
.to_string(),
|
|
||||||
));
|
|
||||||
preformatted_buffer = vec![];
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if collect_preformatted && line != "```" {
|
|
||||||
write!(preformatted_buffer, "{}\n", line).unwrap();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Quotes
|
|
||||||
if line.starts_with(">") {
|
|
||||||
result.push(Node::Quote(line[1..].trim().to_string()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// List items
|
|
||||||
if line.starts_with("*") {
|
|
||||||
result.push(Node::ListItem(line[1..].trim().to_string()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Headings
|
|
||||||
if line.starts_with("###") {
|
|
||||||
result.push(Node::Heading {
|
|
||||||
level: 3,
|
|
||||||
body: line[3..].trim().to_string(),
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if line.starts_with("##") {
|
|
||||||
result.push(Node::Heading {
|
|
||||||
level: 2,
|
|
||||||
body: line[2..].trim().to_string(),
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if line.starts_with("#") {
|
|
||||||
result.push(Node::Heading {
|
|
||||||
level: 1,
|
|
||||||
body: line[1..].trim().to_string(),
|
|
||||||
});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Links
|
|
||||||
if line.starts_with("=>") {
|
|
||||||
let sp = line[2..].split_ascii_whitespace().collect::<Vec<&str>>();
|
|
||||||
|
|
||||||
match sp.len() {
|
|
||||||
1 => result.push(Node::Link {
|
|
||||||
to: sp[0].trim().to_string(),
|
|
||||||
name: None,
|
|
||||||
}),
|
|
||||||
_ => result.push(Node::Link {
|
|
||||||
to: sp[0].trim().to_string(),
|
|
||||||
name: Some(sp[1..].join(" ").trim().to_string()),
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
result.push(Node::Text(line.to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
#[test]
|
|
||||||
fn basic() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = include_str!("../majc/src/help.gmi");
|
|
||||||
let doc = super::parse(msg);
|
|
||||||
assert_ne!(doc.len(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn quote() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = ">hi there";
|
|
||||||
let expected: Vec<Node> = vec![Node::Quote("hi there".to_string())];
|
|
||||||
assert_eq!(expected, parse(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn list() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = "*hi there";
|
|
||||||
let expected: Vec<Node> = vec![Node::ListItem("hi there".to_string())];
|
|
||||||
assert_eq!(expected, parse(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn preformatted() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = "```\n\
|
|
||||||
hi there\n\
|
|
||||||
```\n\
|
|
||||||
\n\
|
|
||||||
Test\n";
|
|
||||||
let expected: Vec<Node> = vec![
|
|
||||||
Node::Preformatted("hi there".to_string()),
|
|
||||||
Node::Text(String::new()),
|
|
||||||
Node::Text("Test".to_string()),
|
|
||||||
];
|
|
||||||
assert_eq!(expected, parse(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn header() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = "#hi\n##there\n### my friends";
|
|
||||||
let expected: Vec<Node> = vec![
|
|
||||||
Node::Heading {
|
|
||||||
level: 1,
|
|
||||||
body: "hi".to_string(),
|
|
||||||
},
|
|
||||||
Node::Heading {
|
|
||||||
level: 2,
|
|
||||||
body: "there".to_string(),
|
|
||||||
},
|
|
||||||
Node::Heading {
|
|
||||||
level: 3,
|
|
||||||
body: "my friends".to_string(),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
assert_eq!(expected, parse(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn link() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = "=>/\n=> / Go home";
|
|
||||||
let expected: Vec<Node> = vec![
|
|
||||||
Node::Link {
|
|
||||||
to: "/".to_string(),
|
|
||||||
name: None,
|
|
||||||
},
|
|
||||||
Node::Link {
|
|
||||||
to: "/".to_string(),
|
|
||||||
name: Some("Go home".to_string()),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
assert_eq!(expected, parse(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ambiguous_preformatted() {
|
|
||||||
let _ = pretty_env_logger::try_init();
|
|
||||||
let msg = include_str!("../testdata/ambig_preformatted.gmi");
|
|
||||||
let expected: Vec<Node> = vec![
|
|
||||||
Node::Preformatted("FOO".to_string()),
|
|
||||||
Node::Text("Foo bar".to_string()),
|
|
||||||
];
|
|
||||||
assert_eq!(expected, parse(msg));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ impl MajHandler for Handler {
|
||||||
|
|
||||||
log::debug!("opening file {:?}", path);
|
log::debug!("opening file {:?}", path);
|
||||||
|
|
||||||
match async_std::fs::metadata(&path).await {
|
match tokio::fs::metadata(&path).await {
|
||||||
Ok(stat) => {
|
Ok(stat) => {
|
||||||
if stat.is_dir() {
|
if stat.is_dir() {
|
||||||
if r.url.as_str().ends_with('/') {
|
if r.url.as_str().ends_with('/') {
|
||||||
|
@ -43,9 +43,9 @@ impl MajHandler for Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut file = async_std::fs::File::open(&path).await?;
|
let mut file = tokio::fs::File::open(&path).await?;
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
async_std::io::copy(&mut file, &mut buf).await?;
|
tokio::io::copy(&mut file, &mut buf).await?;
|
||||||
|
|
||||||
// Send header.
|
// Send header.
|
||||||
if path.extension() == Some(OsStr::new("gmi"))
|
if path.extension() == Some(OsStr::new("gmi"))
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
use crate::{Response, StatusCode};
|
use crate::{Response, StatusCode};
|
||||||
use async_std::{
|
use tokio::{
|
||||||
io::prelude::*,
|
prelude::*,
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
stream::StreamExt,
|
|
||||||
task,
|
task,
|
||||||
};
|
};
|
||||||
use async_tls::TlsAcceptor;
|
use tokio_rustls::TlsAcceptor;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use rustls::Certificate;
|
use rustls::Certificate;
|
||||||
use std::{error::Error as StdError, net::SocketAddr, sync::Arc};
|
use std::{error::Error as StdError, net::SocketAddr, sync::Arc};
|
||||||
|
@ -53,12 +52,10 @@ where
|
||||||
{
|
{
|
||||||
let cfg = Arc::new(cfg);
|
let cfg = Arc::new(cfg);
|
||||||
let listener = TcpListener::bind(&format!("{}:{}", host, port)).await?;
|
let listener = TcpListener::bind(&format!("{}:{}", host, port)).await?;
|
||||||
let mut incoming = listener.incoming();
|
|
||||||
let acceptor = Arc::new(TlsAcceptor::from(cfg.clone()));
|
let acceptor = Arc::new(TlsAcceptor::from(cfg.clone()));
|
||||||
while let Some(Ok(stream)) = incoming.next().await {
|
while let Ok((stream, addr)) = listener.accept().await {
|
||||||
let h = h.clone();
|
let h = h.clone();
|
||||||
let acceptor = acceptor.clone();
|
let acceptor = acceptor.clone();
|
||||||
let addr = stream.peer_addr().unwrap();
|
|
||||||
let port = port.clone();
|
let port = port.clone();
|
||||||
|
|
||||||
task::spawn(handle_request(h, stream, acceptor, addr, port));
|
task::spawn(handle_request(h, stream, acceptor, addr, port));
|
||||||
|
@ -83,7 +80,7 @@ async fn handle_request(
|
||||||
if let Some(u_port) = url.port() {
|
if let Some(u_port) = url.port() {
|
||||||
if port != u_port {
|
if port != u_port {
|
||||||
let _ = write_header(
|
let _ = write_header(
|
||||||
&mut stream,
|
stream,
|
||||||
StatusCode::ProxyRequestRefused,
|
StatusCode::ProxyRequestRefused,
|
||||||
"Cannot proxy to that URL",
|
"Cannot proxy to that URL",
|
||||||
)
|
)
|
||||||
|
@ -117,7 +114,7 @@ async fn handle_request(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn write_header<W: Write + Unpin>(
|
pub async fn write_header<W: AsyncWrite + Unpin>(
|
||||||
mut stream: W,
|
mut stream: W,
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
meta: &str,
|
meta: &str,
|
||||||
|
@ -129,7 +126,7 @@ pub async fn write_header<W: Write + Unpin>(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the URL requested by the client.
|
/// Return the URL requested by the client.
|
||||||
async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
|
async fn parse_request<R: AsyncRead + Unpin>(mut stream: R) -> Result<Url> {
|
||||||
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
|
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
|
||||||
// can use a fixed-sized buffer on the stack, avoiding allocations and
|
// can use a fixed-sized buffer on the stack, avoiding allocations and
|
||||||
// copying, and stopping bad clients from making us use too much memory.
|
// copying, and stopping bad clients from making us use too much memory.
|
||||||
|
@ -167,7 +164,7 @@ async fn handle<T>(
|
||||||
stream: &mut T,
|
stream: &mut T,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
) where
|
) where
|
||||||
T: Write + Unpin,
|
T: AsyncWrite + Unpin,
|
||||||
{
|
{
|
||||||
let u = req.url.clone();
|
let u = req.url.clone();
|
||||||
match h.handle(req).await {
|
match h.handle(req).await {
|
||||||
|
|
Loading…
Reference in New Issue