Skip to content

Commit

Permalink
Implement fetching of local filesystem resources
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Jul 29, 2024
1 parent 62619fd commit 7de8132
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
20 changes: 16 additions & 4 deletions examples/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
//! Render the readme.md using the gpu renderer
use std::path::Path;

use comrak::{markdown_to_html, ExtensionOptionsBuilder, Options, RenderOptionsBuilder};
use dioxus_blitz::Config;

fn main() {
let contents = std::env::args()
let (base_url, contents) = std::env::args()
.skip(1)
.next()
.map(|path| std::fs::read_to_string(path).unwrap())
.unwrap_or(include_str!("../README.md").to_string());
.map(|path| {
let base_path = std::path::absolute(Path::new(&path)).unwrap();
let base_path = base_path.parent().unwrap().to_string_lossy();
let base_url = format!("file://{}/", base_path);
let contents = std::fs::read_to_string(path).unwrap();
(base_url, contents)
})
.unwrap_or({
let base_url = "https://raw.githubusercontent.com/DioxusLabs/blitz/main/".to_string();
let contents = include_str!("../README.md").to_string();
(base_url, contents)
});

let stylesheet = include_str!("./assets/github-markdown-light.css");
let body_html = markdown_to_html(
Expand Down Expand Up @@ -52,7 +64,7 @@ fn main() {
&html,
Config {
stylesheets: vec![String::from(stylesheet)],
base_url: Some("https://raw.githubusercontent.com/DioxusLabs/blitz/main/".to_string()),
base_url: Some(base_url),
},
);
}
57 changes: 47 additions & 10 deletions packages/dom/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,42 @@ const FILE_SIZE_LIMIT: u64 = 1_000_000_000; // 1GB

static FONT_DB: OnceLock<Arc<usvg::fontdb::Database>> = OnceLock::new();

pub(crate) fn fetch_blob(url: &str) -> Result<Vec<u8>, Box<ureq::Error>> {
pub(crate) enum FetchErr {
UrlParse(url::ParseError),
Ureq(Box<ureq::Error>),
FileIo(std::io::Error),
}
impl From<url::ParseError> for FetchErr {
fn from(value: url::ParseError) -> Self {
Self::UrlParse(value)
}
}
impl From<Box<ureq::Error>> for FetchErr {
fn from(value: Box<ureq::Error>) -> Self {
Self::Ureq(value)
}
}
impl From<std::io::Error> for FetchErr {
fn from(value: std::io::Error) -> Self {
Self::FileIo(value)
}
}

pub(crate) fn fetch_blob(url: &str) -> Result<Vec<u8>, FetchErr> {
// Handle data URIs
if url.starts_with("data:") {
let data_url = data_url::DataUrl::process(url).unwrap();
let decoded = data_url.decode_to_vec().expect("Invalid data url");
return Ok(decoded.0);
}

// Handle file:// URLs
let parsed_url = Url::parse(url)?;
if parsed_url.scheme() == "file" {
let file_content = std::fs::read(parsed_url.path())?;
return Ok(file_content);
}

let resp = ureq::get(url)
.set("User-Agent", USER_AGENT)
.call()
Expand All @@ -37,7 +66,7 @@ pub(crate) fn fetch_blob(url: &str) -> Result<Vec<u8>, Box<ureq::Error>> {
Ok(bytes)
}

pub(crate) fn fetch_string(url: &str) -> Result<String, Box<ureq::Error>> {
pub(crate) fn fetch_string(url: &str) -> Result<String, FetchErr> {
fetch_blob(url).map(|vec| String::from_utf8(vec).expect("Invalid UTF8"))
}

Expand All @@ -55,23 +84,30 @@ pub(crate) enum ImageOrSvg {

#[allow(unused)]
pub(crate) enum ImageFetchErr {
FetchErr(Box<ureq::Error>),
ImageError(image::error::ImageError),
SvgError(usvg::Error),
UrlParse(url::ParseError),
Ureq(Box<ureq::Error>),
FileIo(std::io::Error),
ImageParse(image::error::ImageError),
SvgParse(usvg::Error),
}
impl From<Box<ureq::Error>> for ImageFetchErr {
fn from(value: Box<ureq::Error>) -> Self {
Self::FetchErr(value)

impl From<FetchErr> for ImageFetchErr {
fn from(value: FetchErr) -> Self {
match value {
FetchErr::UrlParse(err) => Self::UrlParse(err),
FetchErr::Ureq(err) => Self::Ureq(err),
FetchErr::FileIo(err) => Self::FileIo(err),
}
}
}
impl From<image::error::ImageError> for ImageFetchErr {
fn from(value: image::error::ImageError) -> Self {
Self::ImageError(value)
Self::ImageParse(value)
}
}
impl From<usvg::Error> for ImageFetchErr {
fn from(value: usvg::Error) -> Self {
Self::SvgError(value)
Self::SvgParse(value)
}
}

Expand Down Expand Up @@ -172,6 +208,7 @@ pub fn walk_tree(indent: usize, node: &Node) {

use peniko::Color as PenikoColor;
use style::color::AbsoluteColor;
use url::Url;

pub trait ToPenikoColor {
fn as_peniko(&self) -> PenikoColor;
Expand Down

0 comments on commit 7de8132

Please sign in to comment.