Skip to content
This repository was archived by the owner on Aug 9, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions napture/src/b9/html.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate html_parser;

use crate::{lualog, Tab, globals::LUA_TIMEOUTS};
use crate::{globals::LUA_TIMEOUTS, lualog, Tab};

use super::{
css::{self, Styleable},
Expand All @@ -26,7 +26,7 @@ fn decode_html_entities<T: AsRef<str>>(s: T) -> String {
decode_html_entities(s.as_ref()).to_string()
}

async fn parse_html(mut url: String) -> Result<(Node, Node)> {
async fn parse_html(mut url: String) -> Result<(Node, Node, String)> {
let mut is_html = true;
let mut file_name = String::new();

Expand Down Expand Up @@ -79,7 +79,7 @@ async fn parse_html(mut url: String) -> Result<(Node, Node)> {
}
};

Ok((head, body))
Ok((head, body, html))
}

fn find_element_by_name(elements: &Vec<Node>, name: &str) -> Option<Node> {
Expand Down Expand Up @@ -128,14 +128,20 @@ pub async fn build_ui(

let mut css: String = css::reset_css();

let (head, body) = match parse_html(furl.to_string()).await {
let (head, body, source_html) = match parse_html(furl.to_string()).await {
Ok(ok) => ok,
Err(e) => {
eprintln!("Couldn't parse HTML: {}", e);
return Err(html_parser::Error::Parsing(e.to_string()));
}
};

// add html to source here after parse_html()
{
let mut page_source = tab.page_source.borrow_mut();
page_source.add_file("index.html".to_string(), source_html.to_string());
}

let head_elements = match head.element() {
Some(ok) => ok,
None => {
Expand Down Expand Up @@ -166,6 +172,7 @@ pub async fn build_ui(
}
}

//css is pushed to css variable inside render_head()
css.push_str(&html_view.style());

for element in body_elements.children.iter() {
Expand Down Expand Up @@ -210,12 +217,22 @@ pub async fn build_ui(
let tagss = Rc::clone(&tags);

if !src.is_empty() {
let luacode = if src.starts_with("https://") {
fetch_file(src).await

let src_clone = src.clone();

let luacode = if src_clone.starts_with("https://") {
fetch_file(src_clone).await
} else {
fetch_file(format!("{}/{}", furl, src)).await
fetch_file(format!("{}/{}", furl, src_clone)).await
};

//add lua code to page source
{
let src_clone = src.clone();
let mut page_source = tab.page_source.borrow_mut();
page_source.add_file(src_clone.to_string(), luacode.to_string());
}

if let Err(e) = super::lua::run(luacode, tags, tab.url.clone()).await {
println!("ERROR: Failed to run lua: {}", e);
}
Expand Down Expand Up @@ -274,6 +291,11 @@ async fn render_head(element: &Element, contents: Option<&Node>, tab: Rc<RefCell
// todo: a mutex would be better here, since this has to go through async
let css = fetch_file(format!("{}/{}", furl, href)).await;

{
let mut page_source = tab.borrow_mut().page_source.borrow_mut();
page_source.add_file(href.to_string(), css.to_string());
}

css::load_css(css);
}
}
Expand Down
Loading