Improve frontend
This commit is contained in:
parent
b918498930
commit
0064653cdf
|
@ -47,13 +47,18 @@ impl WordDb {
|
||||||
transaction.execute(&format!("
|
transaction.execute(&format!("
|
||||||
CREATE TABLE {}_words (
|
CREATE TABLE {}_words (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
word TINYTEXT UNIQUE NOT NULL,
|
word TINYTEXT NOT NULL,
|
||||||
type_id INTEGER NOT NULL,
|
type_id INTEGER NOT NULL,
|
||||||
content MEDIUMTEXT NOT NULL,
|
content MEDIUMTEXT NOT NULL,
|
||||||
FOREIGN KEY (type_id)
|
FOREIGN KEY (type_id)
|
||||||
REFERENCES {}_types (id)
|
REFERENCES {}_types (id)
|
||||||
)", &lang.code, &lang.code), []).unwrap();
|
)", &lang.code, &lang.code), []).unwrap();
|
||||||
|
|
||||||
|
transaction.execute(&format!("
|
||||||
|
CREATE INDEX word_index
|
||||||
|
ON {}_words (word)
|
||||||
|
", &lang.code), []).unwrap();
|
||||||
|
|
||||||
transaction.commit().unwrap();
|
transaction.commit().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,8 +107,7 @@ impl WordDb {
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Parsing data...");
|
println!("Parsing data...");
|
||||||
let entries = WiktionaryEntries::parse_data(data)
|
let entries = WiktionaryEntries::parse_data(data);
|
||||||
.merge_duplicates();
|
|
||||||
|
|
||||||
println!("Inserting data...");
|
println!("Inserting data...");
|
||||||
self.insert_entries(lang, entries);
|
self.insert_entries(lang, entries);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
//mod database;
|
//mod database;
|
||||||
use rocket::routes;
|
use rocket::routes;
|
||||||
use rocket::fs::FileServer;
|
use rocket::fs::FileServer;
|
||||||
use rocket::data::{Limits, ToByteUnit};
|
|
||||||
use clap::{App, AppSettings, Arg, SubCommand};
|
use clap::{App, AppSettings, Arg, SubCommand};
|
||||||
//use database::WordDb;
|
//use database::WordDb;
|
||||||
mod database;
|
mod database;
|
||||||
|
@ -83,8 +82,8 @@ async fn main() {
|
||||||
rocket::custom(figment)
|
rocket::custom(figment)
|
||||||
.manage(db)
|
.manage(db)
|
||||||
.mount("/static", FileServer::from("static/"))
|
.mount("/static", FileServer::from("static/"))
|
||||||
.mount("/", routes![routes::get_word,
|
.mount("/", routes![routes::get_entries,
|
||||||
routes::get_word_like,
|
routes::get_entries_like,
|
||||||
routes::frontend])
|
routes::frontend])
|
||||||
.launch()
|
.launch()
|
||||||
.await.unwrap();
|
.await.unwrap();
|
||||||
|
|
|
@ -18,24 +18,36 @@ pub fn frontend() -> Option<content::Html<String>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/langs/<lang>/words/<word>")]
|
#[get("/langs/<lang>/words/<word>")]
|
||||||
pub fn get_word(db: &State<WordDb>, lang: &str, word: &str) -> Option<content::Json<String>> {
|
pub fn get_entries(db: &State<WordDb>, lang: &str, word: &str) -> status::Custom<content::Json<String>> {
|
||||||
let connection = db.connect();
|
let connection = db.connect();
|
||||||
|
|
||||||
let word = connection.query_row(&format!(
|
let mut statement = connection.prepare(&format!(
|
||||||
"SELECT content FROM {}_words WHERE word = ?",
|
"SELECT content
|
||||||
lang),
|
FROM {}_words
|
||||||
[word],
|
WHERE word = ?",
|
||||||
|row| row.get(0)
|
lang)
|
||||||
);
|
).unwrap();
|
||||||
|
|
||||||
match word {
|
let mut rows = statement.query([word]).unwrap();
|
||||||
Ok(w) => Some(content::Json(w)),
|
|
||||||
Err(_) => None
|
let mut words = String::new();
|
||||||
|
|
||||||
|
words.push('[');
|
||||||
|
while let Some(row) = rows.next().unwrap() {
|
||||||
|
let content: String = row.get(0).unwrap();
|
||||||
|
words.push_str(&content);
|
||||||
|
words.push(',');
|
||||||
}
|
}
|
||||||
|
if words.pop().unwrap() == '[' {
|
||||||
|
words.push('[');
|
||||||
|
}
|
||||||
|
words.push(']');
|
||||||
|
|
||||||
|
status::Custom(Status::Ok, content::Json(words))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/langs/<lang>/words?<like>&<limit>&<offset>")]
|
#[get("/langs/<lang>/words?<like>&<limit>&<offset>")]
|
||||||
pub fn get_word_like(db: &State<WordDb>, lang: &str, like: &str, limit: usize, offset: usize) -> Json<Vec<String>> {
|
pub fn get_entries_like(db: &State<WordDb>, lang: &str, like: &str, limit: usize, offset: usize) -> Json<Vec<String>> {
|
||||||
let connection = db.connect();
|
let connection = db.connect();
|
||||||
|
|
||||||
let mut statement = connection.prepare(&format!(
|
let mut statement = connection.prepare(&format!(
|
||||||
|
|
|
@ -31,7 +31,9 @@ function getWord(word) {
|
||||||
success: (data) => {
|
success: (data) => {
|
||||||
$('#ajax-content').html(generateHtml(data))
|
$('#ajax-content').html(generateHtml(data))
|
||||||
searchBar.autocomplete('close');
|
searchBar.autocomplete('close');
|
||||||
}
|
},
|
||||||
|
|
||||||
|
error: err => console.error(err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +92,12 @@ function generateTable(schemas, pos, forms) {
|
||||||
|
|
||||||
let unusedCells = forms.filter(cell => !cell.used);
|
let unusedCells = forms.filter(cell => !cell.used);
|
||||||
|
|
||||||
|
if(schema.ignoreUnused) {
|
||||||
|
unusedCells = unusedCells.filter(cell =>
|
||||||
|
!schema.ignoreUnused.map(tags => tags.every(tag => cell.tags.includes(tag)))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if(unusedCells.length > 0) {
|
if(unusedCells.length > 0) {
|
||||||
html += '<h3>Other</h3>';
|
html += '<h3>Other</h3>';
|
||||||
html += generateList(unusedCells);
|
html += generateList(unusedCells);
|
||||||
|
@ -101,10 +109,6 @@ function generateTable(schemas, pos, forms) {
|
||||||
function generateHtml(data) {
|
function generateHtml(data) {
|
||||||
let html = '';
|
let html = '';
|
||||||
|
|
||||||
if(Object.prototype.toString.call(data) !== '[object Array]') {
|
|
||||||
data = [data];
|
|
||||||
}
|
|
||||||
|
|
||||||
data.forEach(entry => {
|
data.forEach(entry => {
|
||||||
html += `<h1>${entry.word} <span class="pos">(${entry.pos})</span></h1>`
|
html += `<h1>${entry.word} <span class="pos">(${entry.pos})</span></h1>`
|
||||||
if('senses' in entry) {
|
if('senses' in entry) {
|
||||||
|
|
|
@ -142,6 +142,11 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"pos": "adj",
|
"pos": "adj",
|
||||||
|
"ignoreUnused": [
|
||||||
|
["dative", "feminine", "singular"],
|
||||||
|
["instrumental", "feminine", "singular"],
|
||||||
|
["locative", "masculine", "neuter", "singular"]
|
||||||
|
],
|
||||||
"rows": [
|
"rows": [
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue