Skip to content

Commit

Permalink
feat(EventExtractor): Support custom locations per extractor (#51, #48)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiodrg committed Mar 29, 2022
1 parent 06daf73 commit 416f2ce
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
36 changes: 32 additions & 4 deletions src/js/extractors/extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,22 @@ class EventExtractor extends Extractor {
* A format-string that sets calendar event's title for this extractor.
* The format-string is set by the user in the options page and the default
* is set in {@link structure} method.
* @type {string}
*/
title = null;
/**
* A format-string that sets calendar event's description. Similar to
* {@link title}
* @type {string}
*/
description = null;
/**
* A flag indicating wether the title and description format-strings are
* A format-string for the calendar event's location
* @type {string}
*/
location = null;
/**
* A flag indicating whether the title and description format-strings are
* plain text or HTML. It is necessary to handle encoding in specific
* calendars.
*/
Expand Down Expand Up @@ -173,11 +180,12 @@ class EventExtractor extends Extractor {
* @param {Object} childStructure
* @param {string} title Default title format-string
* @param {string} description Default description format-string
* @param {string} location Default location format-string
* @param {boolean} isHTML If the default format-strings are HTML
* @param {CalendarEventStatus} status Default availability for the events,
* i.e. show as 'Busy', or show as 'Free' in calendar clients
*/
structure(childStructure, title, description, isHTML, status) {
structure(childStructure, title, description, location, isHTML, status) {
return {
...childStructure,
storage: {
Expand All @@ -186,6 +194,10 @@ class EventExtractor extends Extractor {
name: "title",
default: title,
},
{
name: "location",
default: location,
},
],
textarea: [
{
Expand Down Expand Up @@ -221,7 +233,6 @@ class EventExtractor extends Extractor {
* depending on user settings
*/
getTitle(params) {
this._validateEventParams(params);
return this._evalFormatString(this.title, params);
}

Expand All @@ -236,10 +247,23 @@ class EventExtractor extends Extractor {
* depending on user settings
*/
getDescription(params) {
this._validateEventParams(params);
return this._evalFormatString(this.description, params);
}

/**
* Build the event's location using the format set by the user in the
* options page.
*
* @param {Object} params An object that sets the value for all parameters
* available in this extractor, as declared in the {@link structure} method.
*
* @returns {String} The formatted string, which may be HTML or plaintext
* depending on user settings
*/
getLocation(params) {
return this._evalFormatString(this.location, params);
}

/**
* Validates the metadata objects used to generate the calendar event title
* and description.
Expand Down Expand Up @@ -275,6 +299,10 @@ class EventExtractor extends Extractor {
* @returns
*/
_evalFormatString(formatStr, params) {
// ensure the metadata object, params, has all the properties
// declated in this extractor's structure
this._validateEventParams(params);

// prepare the format string to be evaluated
let str = "`" + formatStr + "`";
if (this.isHTML) str = str.replace("\n", "<br/>");
Expand Down
13 changes: 7 additions & 6 deletions src/js/extractors/supervisions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class ExamSupervisions extends EventExtractor {
description: "The URL for the exam's information page",
},
{
name: "location",
name: "rooms",
description: "The list of rooms assigned to the exam, if available",
},
],
},
"Exam Supervision - ${acronym} - ${location}",
"Exam Supervision - ${acronym} - ${rooms}",
'Exam Link: <a href="${url}">${name}</a>',
"FEUP: ${rooms}",
true,
CalendarEventStatus.BUSY
);
Expand Down Expand Up @@ -87,7 +88,7 @@ class ExamSupervisions extends EventExtractor {
this.isHTML,
info.startTime,
info.endTime,
info.location
this.getLocation(info)
);
event.status = this.status;
events.push(event);
Expand All @@ -106,7 +107,7 @@ class ExamSupervisions extends EventExtractor {
* name: string,
* acronym: string,
* url: string,
* location: string?,
* rooms: string?,
* }}
*/
parseSupervisionEvent($tr) {
Expand Down Expand Up @@ -137,15 +138,15 @@ class ExamSupervisions extends EventExtractor {
const url = $td[2].querySelector("a").href;

// Parse rooms, if available
const location = $td[3].innerText || null;
const rooms = $td[3].innerText || null;

return {
startTime: new Date(`${date} ${startTime}`),
endTime: new Date(`${date} ${endTime}`),
name,
acronym,
url,
location,
rooms,
};
}
}
Expand Down

0 comments on commit 416f2ce

Please sign in to comment.