Skip to content

Commit

Permalink
Fix large gaps before and after lists (#151)
Browse files Browse the repository at this point in the history
Changes made:
* fix: fix large gaps before and after lists
  • Loading branch information
Builditluc authored Feb 15, 2023
2 parents 6169891 + f721708 commit 8aa164d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/wiki/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn action_parse(params: Vec<(&str, String)>) -> Result<Response> {
.context("failed sending the request")
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ElementType {
Text,
Newline,
Expand Down Expand Up @@ -389,14 +389,14 @@ impl<I, P> ArticleBuilder<I, P> {
.and_then(|x| x.get("title"))
.and_then(|x| x.as_str())
.map(|x| x.to_string())
.ok_or(anyhow!("missing the title"))?;
.ok_or_else(|| anyhow!("missing the title"))?;

let pageid = res_json
.get("parse")
.and_then(|x| x.get("pageid"))
.and_then(|x| x.as_u64())
.map(|x| x as usize)
.ok_or(anyhow!("missing the pageid"))?;
.ok_or_else(|| anyhow!("missing the pageid"))?;

let content = res_json
.get("parse")
Expand Down
37 changes: 25 additions & 12 deletions src/wiki/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::config;
use super::article::{Element, ElementType};

const SHOW_UNSUPPORTED: bool = false;
const LIST_MARKER: char = '-';

pub struct Parser {
elements: Vec<Element>,
Expand Down Expand Up @@ -79,6 +80,23 @@ impl Parser {
style
}

fn push_newline(&mut self) {
self.elements.push(Element::new(
self.next_id(),
ElementType::Newline,
"",
Style::none(),
HashMap::new(),
));
}

fn is_last_newline(&self) -> bool {
self.elements
.last()
.map(|x| x.kind() == ElementType::Newline)
.unwrap_or(false)
}

fn parse_header(&mut self, node: Node) {
if let Some(headline_node) = node.find(Class("mw-headline")).into_selection().first() {
let mut attributes = HashMap::new();
Expand Down Expand Up @@ -157,11 +175,16 @@ impl Parser {
.children()
.filter(|x| x.name().unwrap_or_default() == "li")
{
self.push_newline();
// to avoid having large gaps between lists and other elements, we only want to add a
// newline when there isn't another one already added
if !self.is_last_newline() {
self.push_newline();
}

self.elements.push(Element::new(
self.next_id(),
ElementType::Text,
"\t-".to_string(),
format!("\t{}", LIST_MARKER),
self.combine_effects(Style::from(config::CONFIG.theme.text)),
HashMap::new(),
));
Expand All @@ -170,14 +193,4 @@ impl Parser {
self.push_newline();
self.push_newline();
}

fn push_newline(&mut self) {
self.elements.push(Element::new(
self.next_id(),
ElementType::Newline,
"",
Style::none(),
HashMap::new(),
));
}
}
2 changes: 1 addition & 1 deletion src/wiki/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl SearchBuilder<Query> {
res_json
.get("query")
.and_then(|x| x.get("search"))
.ok_or(anyhow!("missing the search results"))?
.ok_or_else(|| anyhow!("missing the search results"))?
.to_owned(),
)
.context("failed deserializing the search results")?;
Expand Down

0 comments on commit 8aa164d

Please sign in to comment.