Cleanup of wikipedia and download functions

This commit is contained in:
Pieter Vander Vennet 2022-05-26 13:23:25 +02:00
parent 9bedf8e681
commit 48953cf266
4 changed files with 167 additions and 84 deletions

View file

@ -16,18 +16,17 @@ import Link from "../Base/Link";
import WikidataPreviewBox from "./WikidataPreviewBox";
import {Paragraph} from "../Base/Paragraph";
export interface WikipediaBoxOptions {
addHeader: boolean,
firstParagraphOnly: boolean
}
export default class WikipediaBox extends Combine {
public static configuration = {
onlyFirstParagaph: false,
addHeader: false
}
constructor(wikidataIds: string[]) {
constructor(wikidataIds: string[], options?: WikipediaBoxOptions) {
const mainContents = []
const pages = wikidataIds.map(entry => WikipediaBox.createLinkedContent(entry.trim()))
options = options??{addHeader: false, firstParagraphOnly: true};
const pages = wikidataIds.map(entry => WikipediaBox.createLinkedContent(entry.trim(), options))
if (wikidataIds.length == 1) {
const page = pages[0]
mainContents.push(
@ -68,31 +67,29 @@ export default class WikipediaBox extends Combine {
super(mainContents)
this.SetClass("block rounded-xl subtle-background m-1 p-2 flex flex-col")
.SetStyle("max-height: inherit")
}
private static createLinkedContent(entry: string): {
private static createLinkedContent(entry: string, options: WikipediaBoxOptions): {
titleElement: BaseUIElement,
contents: BaseUIElement,
linkElement: BaseUIElement
} {
if (entry.match("[qQ][0-9]+")) {
return WikipediaBox.createWikidatabox(entry)
return WikipediaBox.createWikidatabox(entry, options)
} else {
console.log("Creating wikipedia box for ", entry)
return WikipediaBox.createWikipediabox(entry)
return WikipediaBox.createWikipediabox(entry, options)
}
}
/**
* Given a '<language>:<article-name>'-string, constructs the wikipedia article
* @param wikipediaArticle
* @private
*/
private static createWikipediabox(wikipediaArticle: string): {
private static createWikipediabox(wikipediaArticle: string, options: WikipediaBoxOptions): {
titleElement: BaseUIElement,
contents: BaseUIElement,
linkElement: BaseUIElement
@ -107,12 +104,13 @@ export default class WikipediaBox extends Combine {
linkElement: undefined
}
}
const url = Wikipedia.getPageUrl(article) // `https://${language}.wikipedia.org/wiki/${pagetitle}`
const wikipedia = new Wikipedia({language: article.language})
const url = wikipedia.getPageUrl(article.pageName)
const linkElement = new Link(Svg.pop_out_svg().SetStyle("width: 1.2rem").SetClass("block "), url, true) .SetClass("flex items-center enable-links")
return {
titleElement: new Title(article.pageName, 3),
contents: WikipediaBox.createContents(article.pageName, article.language),
contents: WikipediaBox.createContents(article.pageName, wikipedia, options),
linkElement
}
}
@ -120,7 +118,7 @@ export default class WikipediaBox extends Combine {
/**
* Given a `Q1234`, constructs a wikipedia box or wikidata box
*/
private static createWikidatabox(wikidataId: string): {
private static createWikidatabox(wikidataId: string, options: WikipediaBoxOptions): {
titleElement: BaseUIElement,
contents: BaseUIElement,
linkElement: BaseUIElement
@ -176,8 +174,9 @@ export default class WikipediaBox extends Combine {
}
const [pagetitle, language, wd] = <[string, string, WikidataResponse]>status
const wikipedia = new Wikipedia({language})
const quickFacts = WikidataPreviewBox.QuickFacts(wd);
return WikipediaBox.createContents(pagetitle, language, quickFacts)
return WikipediaBox.createContents(pagetitle, wikipedia, {topBar: quickFacts, ...options})
})
)
@ -223,13 +222,9 @@ export default class WikipediaBox extends Combine {
/**
* Returns the actual content in a scrollable way
*/
private static createContents(pagename: string, language: string, topBar?: BaseUIElement): BaseUIElement {
const wpOptions = {
pageName: pagename,
language: language,
firstParagraphOnly: WikipediaBox.configuration.onlyFirstParagaph
}
const htmlContent = Wikipedia.GetArticle(wpOptions)
private static createContents(pagename: string, wikipedia: Wikipedia, options:{
topBar?: BaseUIElement} & WikipediaBoxOptions): BaseUIElement {
const htmlContent = wikipedia.GetArticle(pagename, options)
const wp = Translations.t.general.wikipedia
const contents: UIEventSource<string | BaseUIElement> = htmlContent.map(htmlContent => {
if (htmlContent === undefined) {
@ -238,11 +233,11 @@ export default class WikipediaBox extends Combine {
}
if (htmlContent["success"] !== undefined) {
let content: BaseUIElement = new FixedUiElement(htmlContent["success"]);
if (WikipediaBox.configuration.addHeader) {
if (options?.addHeader) {
content = new Combine(
[
new Paragraph(
new Link(wp.fromWikipedia, Wikipedia.getPageUrl(wpOptions), true),
new Link(wp.fromWikipedia, wikipedia.getPageUrl(pagename), true),
),
new Paragraph(
content
@ -261,7 +256,7 @@ export default class WikipediaBox extends Combine {
})
return new Combine([
topBar?.SetClass("border-2 border-grey rounded-lg m-1 mb-0"),
options?.topBar?.SetClass("border-2 border-grey rounded-lg m-1 mb-0"),
new VariableUiElement(contents)
.SetClass("block pl-6 pt-2")])
}