Compare commits
No commits in common. "main" and "preserve-texthood-of-text-nodes" have entirely different histories.
main
...
preserve-t
13
.drone.yml
13
.drone.yml
|
@ -7,6 +7,9 @@ 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
|
||||||
|
@ -22,10 +25,6 @@ steps:
|
||||||
- push
|
- push
|
||||||
branch:
|
branch:
|
||||||
- main
|
- main
|
||||||
trigger:
|
|
||||||
event:
|
|
||||||
- push
|
|
||||||
- pull_request
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -40,6 +39,6 @@ steps:
|
||||||
environment:
|
environment:
|
||||||
CARGO_TOKEN:
|
CARGO_TOKEN:
|
||||||
from_secret: CARGO_TOKEN
|
from_secret: CARGO_TOKEN
|
||||||
trigger:
|
when:
|
||||||
event:
|
event:
|
||||||
- tag
|
- tag
|
||||||
|
|
14
Cargo.toml
14
Cargo.toml
|
@ -10,6 +10,8 @@ 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"
|
||||||
|
@ -17,11 +19,11 @@ 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.19", optional = true, features = ["dangerous_configuration"] }
|
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
thiserror = "1"
|
thiserror = "1"
|
||||||
tokio-rustls = { version = "0.21", features = ["dangerous_configuration"] }
|
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
|
||||||
tokio = { version = "0.3", features = ["full"] }
|
tokio = { version = "0.2", features = ["full"], optional = true }
|
||||||
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 }
|
||||||
|
@ -35,8 +37,12 @@ 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 = [
|
||||||
|
@ -44,6 +50,8 @@ server = [
|
||||||
"webpki",
|
"webpki",
|
||||||
"webpki-roots",
|
"webpki-roots",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
"async-std",
|
||||||
|
"async-tls/server"
|
||||||
]
|
]
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "gemtext"
|
name = "gemtext"
|
||||||
version = "0.2.1"
|
version = "0.2.0"
|
||||||
authors = ["Christine Dodrill <me@christine.website>"]
|
authors = ["Christine Dodrill <me@christine.website>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "0BSD"
|
license = "0BSD"
|
||||||
|
|
|
@ -18,28 +18,6 @@ impl Builder {
|
||||||
self
|
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 {
|
pub fn link<T: Into<String>>(mut self, to: T, name: Option<String>) -> Builder {
|
||||||
self.nodes.push(Node::Link {
|
self.nodes.push(Node::Link {
|
||||||
to: to.into(),
|
to: to.into(),
|
||||||
|
@ -48,12 +26,8 @@ impl Builder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preformatted<A, T>(mut self, alt_text: A, data: T) -> Builder
|
pub fn preformatted<T: Into<String>>(mut self, data: T) -> Builder {
|
||||||
where
|
self.nodes.push(Node::Preformatted(data.into()));
|
||||||
A: Into<String>,
|
|
||||||
T: Into<String>,
|
|
||||||
{
|
|
||||||
self.nodes.push(Node::Preformatted { alt: alt_text.into(), body: data.into() });
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,53 +54,11 @@ impl Builder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
/// Render a set of nodes as a document to a writer.
|
||||||
pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()> {
|
pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
|
||||||
use Node::*;
|
use Node::*;
|
||||||
|
|
||||||
for node in nodes.as_ref() {
|
for node in nodes {
|
||||||
match node {
|
match node {
|
||||||
Text(body) => {
|
Text(body) => {
|
||||||
let special_prefixes = ["=>", "```", "#", "*", ">"];
|
let special_prefixes = ["=>", "```", "#", "*", ">"];
|
||||||
|
@ -139,8 +71,8 @@ pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()>
|
||||||
Some(name) => write!(out, "=> {} {}\n", to, name)?,
|
Some(name) => write!(out, "=> {} {}\n", to, name)?,
|
||||||
None => write!(out, "=> {}\n", to)?,
|
None => write!(out, "=> {}\n", to)?,
|
||||||
},
|
},
|
||||||
Preformatted { alt, body } => write!(out, "```{}\n{}\n```\n", alt, body)?,
|
Preformatted(body) => write!(out, "```\n{}\n```\n", body)?,
|
||||||
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(*level as usize), body)?,
|
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(level as usize), body)?,
|
||||||
ListItem(body) => write!(out, "* {}\n", body)?,
|
ListItem(body) => write!(out, "* {}\n", body)?,
|
||||||
Quote(body) => write!(out, "> {}\n", body)?,
|
Quote(body) => write!(out, "> {}\n", body)?,
|
||||||
};
|
};
|
||||||
|
@ -150,7 +82,7 @@ pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Individual nodes of the document. Each node correlates to a line in the file.
|
/// Individual nodes of the document. Each node correlates to a line in the file.
|
||||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Node {
|
pub enum Node {
|
||||||
/// Text lines are the most fundamental line type - any line which does not
|
/// Text lines are the most fundamental line type - any line which does not
|
||||||
/// match the definition of another line type defined below defaults to
|
/// match the definition of another line type defined below defaults to
|
||||||
|
@ -191,19 +123,7 @@ pub enum Node {
|
||||||
/// (e.g. Python) should be able to be copied and pasted from the client into
|
/// (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
|
/// a file and interpreted/compiled without any problems arising from the
|
||||||
/// client's manner of displaying them.
|
/// client's manner of displaying them.
|
||||||
///
|
Preformatted(String),
|
||||||
/// 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,
|
/// Lines beginning with "#" are heading lines. Heading lines consist of one,
|
||||||
/// two or three consecutive "#" characters, followed by optional whitespace,
|
/// two or three consecutive "#" characters, followed by optional whitespace,
|
||||||
|
@ -246,73 +166,24 @@ impl Node {
|
||||||
pub fn blank() -> Node {
|
pub fn blank() -> Node {
|
||||||
Node::Text("".to_string())
|
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> {
|
pub fn parse(doc: &str) -> Vec<Node> {
|
||||||
let mut result: Vec<Node> = vec![];
|
let mut result: Vec<Node> = vec![];
|
||||||
let mut collect_preformatted: bool = false;
|
let mut collect_preformatted: bool = false;
|
||||||
let mut preformatted_buffer: Vec<u8> = vec![];
|
let mut preformatted_buffer: Vec<u8> = vec![];
|
||||||
let mut alt = "";
|
|
||||||
|
|
||||||
for line in doc.lines() {
|
for line in doc.lines() {
|
||||||
if let Some(trailing) = line.strip_prefix("```") {
|
if line.starts_with("```") {
|
||||||
collect_preformatted = !collect_preformatted;
|
collect_preformatted = !collect_preformatted;
|
||||||
if !collect_preformatted {
|
if !collect_preformatted {
|
||||||
result.push(Node::Preformatted {
|
result.push(Node::Preformatted(
|
||||||
alt: alt.to_string(),
|
String::from_utf8(preformatted_buffer)
|
||||||
body: String::from_utf8(preformatted_buffer)
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.trim_end()
|
.trim_end()
|
||||||
.to_string(),
|
.to_string(),
|
||||||
});
|
));
|
||||||
preformatted_buffer = vec![];
|
preformatted_buffer = vec![];
|
||||||
} else {
|
|
||||||
alt = trailing.trim();
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -411,13 +282,13 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn preformatted() {
|
fn preformatted() {
|
||||||
let _ = pretty_env_logger::try_init();
|
let _ = pretty_env_logger::try_init();
|
||||||
let msg = "```hi there\n\
|
let msg = "```\n\
|
||||||
obi-wan kenobi\n\
|
hi there\n\
|
||||||
```\n\
|
```\n\
|
||||||
\n\
|
\n\
|
||||||
Test\n";
|
Test\n";
|
||||||
let expected: Vec<Node> = vec![
|
let expected: Vec<Node> = vec![
|
||||||
Node::Preformatted{ alt: "hi there".to_string(), body: "obi-wan kenobi".to_string() },
|
Node::Preformatted("hi there".to_string()),
|
||||||
Node::Text(String::new()),
|
Node::Text(String::new()),
|
||||||
Node::Text("Test".to_string()),
|
Node::Text("Test".to_string()),
|
||||||
];
|
];
|
||||||
|
@ -467,7 +338,7 @@ mod tests {
|
||||||
let _ = pretty_env_logger::try_init();
|
let _ = pretty_env_logger::try_init();
|
||||||
let msg = include_str!("../../testdata/ambig_preformatted.gmi");
|
let msg = include_str!("../../testdata/ambig_preformatted.gmi");
|
||||||
let expected: Vec<Node> = vec![
|
let expected: Vec<Node> = vec![
|
||||||
Node::Preformatted { alt: "foo".to_string(), body: "FOO".to_string() },
|
Node::Preformatted("FOO".to_string()),
|
||||||
Node::Text("Foo bar".to_string()),
|
Node::Text("Foo bar".to_string()),
|
||||||
];
|
];
|
||||||
assert_eq!(expected, parse(msg));
|
assert_eq!(expected, parse(msg));
|
||||||
|
|
|
@ -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.19", features = ["dangerous_configuration"] }
|
rustls = { version = "0.18", features = ["dangerous_configuration"] }
|
||||||
smol = { version = "0.3", features = ["tokio02"] }
|
smol = { version = "0.3", features = ["tokio02"] }
|
||||||
|
|
||||||
maj = { path = ".." }
|
maj = { path = ".." }
|
||||||
|
|
|
@ -224,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 { body, .. } => styled.append(StyledString::plain(body)),
|
Preformatted(data) => styled.append(StyledString::plain(data)),
|
||||||
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,14 +8,15 @@ 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.19", features = ["dangerous_configuration"] }
|
rustls = { version = "0.18", features = ["dangerous_configuration"] }
|
||||||
structopt = "0.3"
|
structopt = "0.3"
|
||||||
tokio = { version = "0.3", features = ["full"] }
|
tokio = { version = "0.2", 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 { alt, body } => write!(out, "```{}\n{}\n```\n\n", alt, body)?,
|
Preformatted(body) => write!(out, "```\n{}\n```\n\n", 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,6 +6,8 @@ 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
|
||||||
|
@ -13,6 +15,10 @@ 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,6 +9,7 @@ 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"
|
||||||
|
@ -16,14 +17,13 @@ log = "0"
|
||||||
mime = "0.3.0"
|
mime = "0.3.0"
|
||||||
percent-encoding = "2"
|
percent-encoding = "2"
|
||||||
rand = "0"
|
rand = "0"
|
||||||
rustls = { version = "0.19", features = ["dangerous_configuration"] }
|
rustls = { version = "0.18", 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,12 +80,7 @@ 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 { alt, body } => write!(
|
Preformatted(body) => write!(buf, "<code><pre>{}</pre></code>", body).unwrap(),
|
||||||
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,8 +64,7 @@ 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"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
fn main() -> Result<(), maj::server::Error> {
|
||||||
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();
|
||||||
|
@ -97,7 +96,7 @@ async fn main() -> Result<(), maj::server::Error> {
|
||||||
thread::spawn(move || http::run(h.clone(), port));
|
thread::spawn(move || http::run(h.clone(), port));
|
||||||
}
|
}
|
||||||
|
|
||||||
maj::server::serve(h.clone(), config, opts.host, opts.port).await?;
|
task::block_on(maj::server::serve(h.clone(), config, opts.host, opts.port))?;
|
||||||
|
|
||||||
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()));
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ impl MajHandler for Handler {
|
||||||
|
|
||||||
log::debug!("opening file {:?}", path);
|
log::debug!("opening file {:?}", path);
|
||||||
|
|
||||||
match tokio::fs::metadata(&path).await {
|
match async_std::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 = tokio::fs::File::open(&path).await?;
|
let mut file = async_std::fs::File::open(&path).await?;
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
tokio::io::copy(&mut file, &mut buf).await?;
|
async_std::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,10 +1,11 @@
|
||||||
use crate::{Response, StatusCode};
|
use crate::{Response, StatusCode};
|
||||||
use tokio::{
|
use async_std::{
|
||||||
prelude::*,
|
io::prelude::*,
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
|
stream::StreamExt,
|
||||||
task,
|
task,
|
||||||
};
|
};
|
||||||
use tokio_rustls::TlsAcceptor;
|
use async_tls::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};
|
||||||
|
@ -52,10 +53,12 @@ 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 Ok((stream, addr)) = listener.accept().await {
|
while let Some(Ok(stream)) = incoming.next().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));
|
||||||
|
@ -80,7 +83,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(
|
||||||
stream,
|
&mut stream,
|
||||||
StatusCode::ProxyRequestRefused,
|
StatusCode::ProxyRequestRefused,
|
||||||
"Cannot proxy to that URL",
|
"Cannot proxy to that URL",
|
||||||
)
|
)
|
||||||
|
@ -114,7 +117,7 @@ async fn handle_request(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn write_header<W: AsyncWrite + Unpin>(
|
pub async fn write_header<W: Write + Unpin>(
|
||||||
mut stream: W,
|
mut stream: W,
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
meta: &str,
|
meta: &str,
|
||||||
|
@ -126,7 +129,7 @@ pub async fn write_header<W: AsyncWrite + Unpin>(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the URL requested by the client.
|
/// Return the URL requested by the client.
|
||||||
async fn parse_request<R: AsyncRead + Unpin>(mut stream: R) -> Result<Url> {
|
async fn parse_request<R: Read + 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.
|
||||||
|
@ -164,7 +167,7 @@ async fn handle<T>(
|
||||||
stream: &mut T,
|
stream: &mut T,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
) where
|
) where
|
||||||
T: AsyncWrite + Unpin,
|
T: Write + 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