Skip to content

Commit b3de586

Browse files
committed
Parse HTML items (events) separately if possible
1 parent 700c900 commit b3de586

File tree

7 files changed

+45
-8
lines changed

7 files changed

+45
-8
lines changed

MMM-OnThisDay.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ const moduleDefinition = {
3333
*/
3434
events: null,
3535

36+
/**
37+
* Separate items parsed from the HTML data.
38+
*/
39+
items: null,
40+
3641
/**
3742
* Module scripts.
3843
*
@@ -85,6 +90,7 @@ const moduleDefinition = {
8590
return {
8691
config: this.config,
8792
events: this.events,
93+
items: this.items,
8894
};
8995
},
9096

@@ -124,6 +130,7 @@ const moduleDefinition = {
124130
// Set content
125131
this.title = payload.title;
126132
this.events = payload.events;
133+
this.items = payload.items;
127134

128135
// Update module
129136
Log.info('Update DOM with new title and events ...');

src/HtmlParser.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,30 @@ module.exports = class {
1717
const eventsSelector = WIKI_CSS_SELECTORS[language].events;
1818
const events = dom.querySelector(eventsSelector);
1919

20+
// Get items
21+
const itemSelector = WIKI_CSS_SELECTORS[language].items;
22+
const items = [];
23+
24+
if (itemSelector !== undefined) {
25+
const domItems = dom.querySelectorAll(itemSelector);
26+
for (const domItem of domItems) {
27+
items.push(this._removeWhitespaces(domItem.textContent));
28+
}
29+
}
30+
2031
// Check data
2132
if (!events) {
2233
return {};
2334
}
2435

2536
return {
26-
title: title ? title.innerHTML : null,
37+
title: title ? this._removeWhitespaces(title.textContent) : null,
2738
events: events.outerHTML,
39+
items: items,
2840
};
2941
}
42+
43+
_removeWhitespaces(text) {
44+
return text.replace(/\s+/g, ' ').trim();
45+
}
3046
};

src/WikiCssSelectors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const WIKI_CSS_SELECTORS = {
1414
de: {
1515
title: '#ereignisse .hauptseite-box-title',
1616
events: '#ereignisse > .hauptseite-box-content > ul:first-of-type',
17+
items: '#ereignisse > .hauptseite-box-content > ul:first-of-type > li',
1718
},
1819

1920
fr: {

template/MMM-OnThisDay.njk

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@
99
{# Wrapper #}
1010
<div class="{{ config.textSize }}" style="max-width: {{ config.maxWidth }};">
1111

12-
{% if not events %}
12+
{% if items %}
1313

14-
{# No data #}
15-
{{ 'TEXT_NODATA' | translate }}
14+
<ul>
15+
{% for item in items %}
16+
<li>{{ item }}</li>
17+
{% endfor %}
18+
</ul>
1619

17-
{% else %}
20+
{% elseif events %}
1821

1922
{# Data loaded #}
2023
{{ events | safe }}
2124

25+
{% else %}
26+
27+
{# No data #}
28+
{{ 'TEXT_NODATA' | translate }}
29+
2230
{% endif %}
2331

2432
</div>

test/functional/WikiSiteTest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('Wikipedia HTML', () => {
1919

2020
const titleSelector = WIKI_CSS_SELECTORS[language].title;
2121
const eventsSelector = WIKI_CSS_SELECTORS[language].events;
22+
const itemsSelector = WIKI_CSS_SELECTORS[language].items;
2223

2324
it(`should return HTML with necessary CSS selectors from Wikipedia (${language})`, async function () {
2425
this.timeout(5000);
@@ -34,6 +35,10 @@ describe('Wikipedia HTML', () => {
3435
// Assert
3536
assert.ok(document.querySelector(titleSelector));
3637
assert.ok(document.querySelector(eventsSelector));
38+
39+
if (itemsSelector) {
40+
assert.ok(document.querySelector(itemsSelector));
41+
}
3742
});
3843
}
3944
});

test/unit/HtmlParserTest.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ describe('HtmlParser', () => {
3030
assert.ok(typeof result.title === 'string');
3131
assert.ok(typeof result.events === 'string');
3232

33-
assert.ok(
34-
result.title.includes('Was geschah am 26.&nbsp;Februar?'),
35-
);
33+
assert.ok(result.title.includes('Was geschah am 26. Februar?'));
3634
assert.ok(result.events.includes('Alexandra Povòrina'));
3735
assert.ok(result.events.includes('Robert Watson-Watt'));
3836
assert.ok(result.events.includes('Nick Leeson'));

test/unit/node_helperTest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('node_helper', () => {
4141
helper.sendSocketNotification.calledWith('EVENTS_LOADED', {
4242
title: 'test title for en',
4343
events: '<ul><li>test events for en</li></ul>',
44+
items: [],
4445
}),
4546
);
4647
});
@@ -55,6 +56,7 @@ describe('node_helper', () => {
5556
assert.deepStrictEqual(events, {
5657
title: 'test title for en',
5758
events: '<ul><li>test events for en</li></ul>',
59+
items: [],
5860
});
5961
});
6062
});

0 commit comments

Comments
 (0)