Advertisement
Guest User

Untitled

a guest
Apr 21st, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.93 KB | None | 0 0
  1. use markdown::*;
  2. use rocket::response::content;
  3. use rocket::response::content::Html;
  4. use rocket::*;
  5. use serde_derive::Deserialize;
  6. use std::fs::File;
  7. use std::io::Read;
  8. use std::ops::Add;
  9. use tokio::*;
  10. use toml;
  11.  
  12. #[derive(Deserialize)]
  13. struct Config {
  14.     pubdir: String,
  15.     cssfile: String,
  16. }
  17.  
  18. #[launch]
  19. async fn rocket() -> _ {
  20.     rocket::build().mount("/", routes![getmd, getcssfile])
  21. }
  22.  
  23. #[get("/<path>")]
  24. fn getmd(path: &str) -> Html<String> {
  25.     let mdp = getconf().unwrap().pubdir + path;
  26.     println!("{}", mdp);
  27.     let mut mdf = File::open(&mdp);
  28.     let mut md = String::new();
  29.  
  30.     mdf.unwrap().read_to_string(&mut md);
  31.  
  32.     let html = addcss(gethtm(&md));
  33.     println!("{}", html);
  34.     return content::Html(html);
  35. }
  36.  
  37. #[get("/css.css")]
  38. fn getcssfile() -> String {
  39.     let css = File::open(getconf().unwrap().cssfile);
  40.     let mut csstext = String::new();
  41.     css.unwrap().read_to_string(&mut csstext);
  42.     println!("{}", csstext);
  43.     csstext
  44. }
  45.  
  46. fn getconf() -> Result<Config, toml::de::Error> {
  47.     let mut cFile = File::open("config.toml");
  48.     let mut cString = String::new();
  49.  
  50.     cFile.unwrap().read_to_string(&mut cString);
  51.     println!("a");
  52.     //let config: Result<Config, toml::de::Error> = toml::from_str(&cString);
  53.     return toml::from_str(&cString);
  54. }
  55.  
  56. fn gethtm(md: &str) -> String {
  57.     println!("b");
  58.     return markdown::to_html(md);
  59. }
  60.  
  61. fn addcss(htm: String) -> String {
  62.     println!("{}", htm);
  63.     let mut ohtml = htm.clone();
  64.     let mut h = String::new();
  65.     let hh = h.clone().add(&("
  66.        <head>
  67.            <link rel=\u{0022}stylesheet\u{0022} type=\u{0022}text/css\u{0022} href=\u{0022}css.css\u{0022}>
  68.        </head>
  69.        
  70.        <body>".to_owned()
  71.         +
  72.         &htm
  73.         +
  74.         &"</body>".to_owned()));
  75.     //h.add(&htm);
  76.     //h.add("</body>");
  77.     ohtml.insert_str(htm.len(), "</body>");
  78.     println!("{}", ohtml);
  79.     return hh;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement