texcreate_lib/Web/
tc.rs

1use std::io::Write;
2use rocket::post;
3use zip::write::{FileOptions, ZipWriter};
4use zip::CompressionMethod::Stored;
5use rocket::form::{FromForm, Form};
6use rocket::fs::NamedFile;
7use crate::Config::consts::{DATE, AUTHOR, TITLE};
8
9#[derive(FromForm)]
10pub struct Texcreate<'r>{
11    pub author: &'r str,
12    pub title: &'r str,
13    pub date: &'r str,
14    pub project_name: &'r str,
15    pub template: &'r str,
16    pub paper_size: &'r  str,
17    pub font_size: &'r str,
18    pub document_class: &'r str
19}
20
21#[post("/", data="<input>")]
22pub async fn tex_create(input: Form<Texcreate<'_>>) -> Option<NamedFile>{
23    let f_path = std::path::Path::new("tc_files");
24    if !f_path.exists(){
25        std::fs::create_dir(&f_path).unwrap();
26    }
27    let rand = "tc_files/9999999sisjsj.zip";
28    let mut zip = ZipWriter::new(std::fs::File::create(rand).unwrap());
29    let options = FileOptions::default().compression_method(Stored);
30    let path = std::path::Path::new("tc_files").join(&input.project_name);
31    std::fs::create_dir(&path).unwrap();
32    std::fs::File::create(&path.clone().join("main.tex")).unwrap();
33    std::fs::File::create(&path.clone().join("structure.tex")).unwrap();
34    let (mut m, s) = crate::load(input.template);
35    // Replace step
36    let title = format!("\\title{{{}}}", input.title);
37    let author = format!("\\author{{{}}}", input.author);
38    let date = format!("\\date{{{}}}", input.date);
39
40    m = m.replace(TITLE, &title);
41    m = m.replace(AUTHOR, &author);
42    m = m.replace(DATE, &date);
43
44    m = m.replace("letterpaper", input.paper_size);
45    m = m.replace("11pt", &format!("{}pt", input.font_size));
46    m = m.replace("article", input.document_class);
47    //Zip step
48    zip.start_file(path.clone().join("main.tex").to_str().unwrap(), options).unwrap();
49    zip.write_all(m.as_bytes()).unwrap();
50    zip.start_file(path.clone().join("structure.tex").to_str().unwrap(), options).unwrap();
51    zip.write_all(s.as_bytes()).unwrap();
52    zip.finish().unwrap();
53    //Remove step
54    std::fs::remove_file(&path.clone().join("main.tex")).unwrap();
55    std::fs::remove_file(&path.clone().join("structure.tex")).unwrap();
56    std::fs::remove_dir(&path).unwrap();
57    NamedFile::open(rand).await.ok()
58}
59
60
61pub const TC_HTML: &str = r#"<!DOCTYPE html>
62<html>
63<title>TexCreate</title>
64<meta charset="UTF-8">
65<meta name="viewport" content="width=device-width, initial-scale=1">
66<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
67<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
68<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
69<style>
70body, h1,h2,h3,h4,h5,h6 {font-family: "Montserrat", sans-serif}
71.w3-row-padding img {margin-bottom: 12px}
72/* Set the width of the sidebar to 120px */
73.w3-sidebar {width: 120px;background: #222;}
74/* Add a left margin to the "page content" that matches the width of the sidebar (120px) */
75#main {margin-left: 120px}
76/* Remove margins from "page content" on small screens */
77@media only screen and (max-width: 600px) {#main {margin-left: 0}}
78</style>
79<body class="w3-black">
80<!-- Page Content -->
81<div class="w3-padding-large" id="main">
82    <!-- Header/Home -->
83    <header class="w3-container w3-padding-32 w3-center w3-black" id="home">
84        <h1 class="w3-jumbo"><span class="w3-hide-small">TexCreate</span></h1>
85    <!-- <img src="Banner.png" alt="boy" class="w3-image" width="*" height="0.25*"> -->
86    </header>
87
88    <!-- About Section -->
89    <div class="w3-content w3-justify w3-text-grey w3-padding-64" id="about">
90        <h2 class="w3-text-light-grey">BUILD A LaTeX PROJECT</h2>
91        <hr style="width:200px" class="w3-opacity">
92            <div class="w3-container w3-center">
93<form method="post" action="/">
94    <h3>Project</h3><br>
95    <label for="author">Author</label><br>
96    <input  class="w3-teal w3-button" type="text" name="author" id="author" placeholder="Author"><br>
97    <label for="title">Title</label><br>
98    <input class="w3-teal w3-button" type="text" name="title" id="title" placeholder="Title"><br>
99    <label for="date">Date</label><br>
100    <input class="w3-teal w3-button" type="text" name="date" id="date" placeholder="Date"><br>
101    <label for="project_name">Project Name</label><br>
102    <input class="w3-teal w3-button" type="text" name="project_name" id="project_name" placeholder="Project Name"><br>
103    <label for="template">Template</label><br>
104    <select class="w3-teal w3-button" type="text" name="template" id="template" placeholder="Template">
105    <option value="Basic">Basic</option>
106    <option value="Math">Math</option>
107    <option value="Code">Code</option>
108    <option value="Theatre">Theatre</option>
109    <option value="Novel">Novel</option>
110    <option value="Beamer">Beamer</option>
111    <option value="Lachaise">Lachaise</option>
112    </select>
113    <br>
114    <h3>Document</h3>
115    <label for="paper_size">Paper Size</label><br>
116    <input class="w3-teal w3-button" type="text" name="paper_size" id="paper_size" placeholder="Paper Size"><br>
117    <label for="font_size">Font Size</label><br>
118    <input class="w3-teal w3-button" type="text" name="font_size" id="font_size" placeholder="Font Size"><br>
119    <label for="document_class">Document Class</label><br>
120    <input class="w3-teal w3-button" type="text" name="document_class" id="document_class" placeholder="Document Class"><br>
121    <br>
122    <input class="w3-btn w3-teal" type="submit" value="Submit">
123</form>
124</div>
125
126    </div>
127
128    <!-- Footer -->
129    <footer class="w3-content w3-padding-64 w3-text-grey w3-xlarge">
130        <p class="w3-medium">Built by  <a href="https://github.com/MKProj/" target="_blank" class="w3-hover-text-green">MKProjects</a></p>
131        <!-- End footer -->
132    </footer>
133    <!-- END PAGE CONTENT -->
134</div>
135</body>
136</html>
137"#;