Also load wikipedia articla from 'wikipedia' tag if no wikidata is given, fixes #780
This commit is contained in:
parent
7f972d4512
commit
3a2b163128
4 changed files with 94 additions and 21 deletions
|
@ -51,6 +51,23 @@ export default class Wikipedia {
|
||||||
public static getPageUrl(options: {language?: "en" | string, pageName: string}): string{
|
public static getPageUrl(options: {language?: "en" | string, pageName: string}): string{
|
||||||
return `https://${options.language ?? "en"}.wikipedia.org/wiki/` + options.pageName
|
return `https://${options.language ?? "en"}.wikipedia.org/wiki/` + options.pageName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tries to extract the language and article name from the given string
|
||||||
|
*
|
||||||
|
* Wikipedia.extractLanguageAndName("qsdf") // => undefined
|
||||||
|
* Wikipedia.extractLanguageAndName("nl:Warandeputten") // => {lang: "nl", article: "Warandeputten"}
|
||||||
|
*/
|
||||||
|
public static extractLanguageAndName(input: string):{language: string, pageName: string} {
|
||||||
|
const matched = input.match("([^:]+):(.*)")
|
||||||
|
if(matched === undefined || matched === null){
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
const [_ , language, pageName] = matched
|
||||||
|
return {
|
||||||
|
language, pageName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static async GetArticleAsync(options: {
|
public static async GetArticleAsync(options: {
|
||||||
pageName: string,
|
pageName: string,
|
||||||
|
|
|
@ -244,20 +244,25 @@ export default class SpecialVisualizations {
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
name: "keyToShowWikipediaFor",
|
name: "keyToShowWikipediaFor",
|
||||||
doc: "Use the wikidata entry from this key to show the wikipedia article for",
|
doc: "Use the wikidata entry from this key to show the wikipedia article for. Multiple keys can be given (separated by ';'), in which case the first matching value is used",
|
||||||
defaultValue: "wikidata"
|
defaultValue: "wikidata;wikipedia"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
example: "`{wikipedia()}` is a basic example, `{wikipedia(name:etymology:wikidata)}` to show the wikipedia page of whom the feature was named after. Also remember that these can be styled, e.g. `{wikipedia():max-height: 10rem}` to limit the height",
|
example: "`{wikipedia()}` is a basic example, `{wikipedia(name:etymology:wikidata)}` to show the wikipedia page of whom the feature was named after. Also remember that these can be styled, e.g. `{wikipedia():max-height: 10rem}` to limit the height",
|
||||||
constr: (_, tagsSource, args) =>
|
constr: (_, tagsSource, args) => {
|
||||||
new VariableUiElement(
|
const keys = args[0].split(";").map(k => k.trim())
|
||||||
tagsSource.map(tags => tags[args[0]])
|
return new VariableUiElement(
|
||||||
|
tagsSource.map(tags => {
|
||||||
|
const key = keys.find(k => tags[k] !== undefined && tags[k] !== "")
|
||||||
|
return tags[key];
|
||||||
|
})
|
||||||
.map(wikidata => {
|
.map(wikidata => {
|
||||||
const wikidatas: string[] =
|
const wikidatas: string[] =
|
||||||
Utils.NoEmpty(wikidata?.split(";")?.map(wd => wd.trim()) ?? [])
|
Utils.NoEmpty(wikidata?.split(";")?.map(wd => wd.trim()) ?? [])
|
||||||
return new WikipediaBox(wikidatas)
|
return new WikipediaBox(wikidatas)
|
||||||
})
|
})
|
||||||
)
|
);
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,23 +27,23 @@ export default class WikipediaBox extends Combine {
|
||||||
|
|
||||||
const mainContents = []
|
const mainContents = []
|
||||||
|
|
||||||
const pages = wikidataIds.map(wdId => WikipediaBox.createLinkedContent(wdId.trim()))
|
const pages = wikidataIds.map(entry => WikipediaBox.createLinkedContent(entry.trim()))
|
||||||
if (wikidataIds.length == 1) {
|
if (wikidataIds.length == 1) {
|
||||||
const page = pages[0]
|
const page = pages[0]
|
||||||
mainContents.push(
|
mainContents.push(
|
||||||
new Combine([
|
new Combine([
|
||||||
new Combine([
|
new Combine([
|
||||||
Svg.wikipedia_svg().SetStyle("width: 1.5rem").SetClass("inline-block mr-3"),
|
Svg.wikipedia_ui().SetStyle("width: 1.5rem").SetClass("inline-block mr-3"),
|
||||||
page.titleElement]).SetClass("flex"),
|
page.titleElement]).SetClass("flex"),
|
||||||
page.linkElement
|
page.linkElement
|
||||||
]).SetClass("flex justify-between align-middle"),
|
]).SetClass("flex justify-between align-middle"),
|
||||||
)
|
)
|
||||||
mainContents.push(page.contents)
|
mainContents.push(page.contents.SetClass("overflow-auto normal-background rounded-lg"))
|
||||||
} else if (wikidataIds.length > 1) {
|
} else if (wikidataIds.length > 1) {
|
||||||
|
|
||||||
const tabbed = new TabbedComponent(
|
const tabbed = new TabbedComponent(
|
||||||
pages.map(page => {
|
pages.map(page => {
|
||||||
const contents = page.contents.SetClass("block").SetStyle("max-height: inherit; height: inherit; padding-bottom: 3.3rem")
|
const contents = page.contents.SetClass("overflow-auto normal-background rounded-lg block").SetStyle("max-height: inherit; height: inherit; padding-bottom: 3.3rem")
|
||||||
return {
|
return {
|
||||||
header: page.titleElement.SetClass("pl-2 pr-2"),
|
header: page.titleElement.SetClass("pl-2 pr-2"),
|
||||||
content: new Combine([
|
content: new Combine([
|
||||||
|
@ -74,7 +74,53 @@ export default class WikipediaBox extends Combine {
|
||||||
.SetStyle("max-height: inherit")
|
.SetStyle("max-height: inherit")
|
||||||
}
|
}
|
||||||
|
|
||||||
private static createLinkedContent(wikidataId: string): {
|
private static createLinkedContent(entry: string): {
|
||||||
|
titleElement: BaseUIElement,
|
||||||
|
contents: BaseUIElement,
|
||||||
|
linkElement: BaseUIElement
|
||||||
|
} {
|
||||||
|
if (entry.match("[qQ][0-9]+")) {
|
||||||
|
return WikipediaBox.createWikidatabox(entry)
|
||||||
|
} else {
|
||||||
|
console.log("Creating wikipedia box for ", entry)
|
||||||
|
return WikipediaBox.createWikipediabox(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a '<language>:<article-name>'-string, constructs the wikipedia article
|
||||||
|
* @param wikipediaArticle
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
private static createWikipediabox(wikipediaArticle: string): {
|
||||||
|
titleElement: BaseUIElement,
|
||||||
|
contents: BaseUIElement,
|
||||||
|
linkElement: BaseUIElement
|
||||||
|
} {
|
||||||
|
const wp = Translations.t.general.wikipedia;
|
||||||
|
|
||||||
|
const article = Wikipedia.extractLanguageAndName(wikipediaArticle)
|
||||||
|
if (article === undefined) {
|
||||||
|
return {
|
||||||
|
titleElement: undefined,
|
||||||
|
contents: wp.noWikipediaPage,
|
||||||
|
linkElement: undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const url = Wikipedia.getPageUrl(article) // `https://${language}.wikipedia.org/wiki/${pagetitle}`
|
||||||
|
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),
|
||||||
|
linkElement
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a `Q1234`, constructs a wikipedia box or wikidata box
|
||||||
|
*/
|
||||||
|
private static createWikidatabox(wikidataId: string): {
|
||||||
titleElement: BaseUIElement,
|
titleElement: BaseUIElement,
|
||||||
contents: BaseUIElement,
|
contents: BaseUIElement,
|
||||||
linkElement: BaseUIElement
|
linkElement: BaseUIElement
|
||||||
|
@ -130,11 +176,11 @@ export default class WikipediaBox extends Combine {
|
||||||
}
|
}
|
||||||
|
|
||||||
const [pagetitle, language, wd] = <[string, string, WikidataResponse]>status
|
const [pagetitle, language, wd] = <[string, string, WikidataResponse]>status
|
||||||
return WikipediaBox.createContents(pagetitle, language, wd)
|
const quickFacts = WikidataPreviewBox.QuickFacts(wd);
|
||||||
|
return WikipediaBox.createContents(pagetitle, language, quickFacts)
|
||||||
|
|
||||||
})
|
})
|
||||||
).SetClass("overflow-auto normal-background rounded-lg")
|
)
|
||||||
|
|
||||||
|
|
||||||
const titleElement = new VariableUiElement(wikiLink.map(state => {
|
const titleElement = new VariableUiElement(wikiLink.map(state => {
|
||||||
if (typeof state !== "string") {
|
if (typeof state !== "string") {
|
||||||
|
@ -145,8 +191,7 @@ export default class WikipediaBox extends Combine {
|
||||||
}
|
}
|
||||||
return new Title(pagetitle, 3)
|
return new Title(pagetitle, 3)
|
||||||
}
|
}
|
||||||
//return new Title(Translations.t.general.wikipedia.wikipediaboxTitle.Clone(), 2)
|
return new Link(new Title(wikidataId, 3), "https://www.wikidata.org/wiki/" + wikidataId, true)
|
||||||
return new Link(new Title(wikidataId, 3), "https://www.wikidata.org/wiki/"+wikidataId, true)
|
|
||||||
|
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
@ -178,7 +223,7 @@ export default class WikipediaBox extends Combine {
|
||||||
/**
|
/**
|
||||||
* Returns the actual content in a scrollable way
|
* Returns the actual content in a scrollable way
|
||||||
*/
|
*/
|
||||||
private static createContents(pagename: string, language: string, wikidata: WikidataResponse): BaseUIElement {
|
private static createContents(pagename: string, language: string, topBar?: BaseUIElement): BaseUIElement {
|
||||||
const wpOptions = {
|
const wpOptions = {
|
||||||
pageName: pagename,
|
pageName: pagename,
|
||||||
language: language,
|
language: language,
|
||||||
|
@ -186,7 +231,6 @@ export default class WikipediaBox extends Combine {
|
||||||
}
|
}
|
||||||
const htmlContent = Wikipedia.GetArticle(wpOptions)
|
const htmlContent = Wikipedia.GetArticle(wpOptions)
|
||||||
const wp = Translations.t.general.wikipedia
|
const wp = Translations.t.general.wikipedia
|
||||||
const quickFacts = WikidataPreviewBox.QuickFacts(wikidata);
|
|
||||||
const contents: UIEventSource<string | BaseUIElement> = htmlContent.map(htmlContent => {
|
const contents: UIEventSource<string | BaseUIElement> = htmlContent.map(htmlContent => {
|
||||||
if (htmlContent === undefined) {
|
if (htmlContent === undefined) {
|
||||||
// Still loading
|
// Still loading
|
||||||
|
@ -194,11 +238,11 @@ export default class WikipediaBox extends Combine {
|
||||||
}
|
}
|
||||||
if (htmlContent["success"] !== undefined) {
|
if (htmlContent["success"] !== undefined) {
|
||||||
let content: BaseUIElement = new FixedUiElement(htmlContent["success"]);
|
let content: BaseUIElement = new FixedUiElement(htmlContent["success"]);
|
||||||
if(WikipediaBox.configuration.addHeader){
|
if (WikipediaBox.configuration.addHeader) {
|
||||||
content = new Combine(
|
content = new Combine(
|
||||||
[
|
[
|
||||||
new Paragraph(
|
new Paragraph(
|
||||||
new Link(wp.fromWikipedia, Wikipedia.getPageUrl(wpOptions), true),
|
new Link(wp.fromWikipedia, Wikipedia.getPageUrl(wpOptions), true),
|
||||||
),
|
),
|
||||||
new Paragraph(
|
new Paragraph(
|
||||||
content
|
content
|
||||||
|
@ -217,7 +261,7 @@ export default class WikipediaBox extends Combine {
|
||||||
})
|
})
|
||||||
|
|
||||||
return new Combine([
|
return new Combine([
|
||||||
quickFacts?.SetClass("border-2 border-grey rounded-lg m-1 mb-0"),
|
topBar?.SetClass("border-2 border-grey rounded-lg m-1 mb-0"),
|
||||||
new VariableUiElement(contents)
|
new VariableUiElement(contents)
|
||||||
.SetClass("block pl-6 pt-2")])
|
.SetClass("block pl-6 pt-2")])
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,13 @@
|
||||||
"zh_Hans": "在Wikidata上对应的实体是什么?"
|
"zh_Hans": "在Wikidata上对应的实体是什么?"
|
||||||
},
|
},
|
||||||
"mappings": [
|
"mappings": [
|
||||||
|
{
|
||||||
|
"if": "wikipedia~*",
|
||||||
|
"then": {
|
||||||
|
"*": "{wikipedia():max-height:25rem}"
|
||||||
|
},
|
||||||
|
"hideInAnswer": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"if": "wikidata=",
|
"if": "wikidata=",
|
||||||
"then": {
|
"then": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue