Skip to content

Commit

Permalink
1.1.1 features were added. closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed May 25, 2024
1 parent 1c125ae commit 8d72602
Show file tree
Hide file tree
Showing 23 changed files with 255 additions and 1,441 deletions.
4 changes: 2 additions & 2 deletions bin/richieMaker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rmSync } from "fs";
import { rmSync, existsSync } from "fs";
import { readFile } from "fs/promises";
import { globSync } from "glob";
import { dirname, join, relative, basename } from "path";
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function makeRichie(
options = { ...richieDefaultOptions, ...options };

//remove previous op dir and files
if (!options.norm) {
if (!options.norm && existsSync(options.destDir as string)) {
rmSync(options.destDir as string, { recursive: true });
}

Expand Down
155 changes: 155 additions & 0 deletions lib/sweeper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { CheerioAPI, load } from "cheerio";
import configuration from "../configLoader";
import { richies } from "./options";

const { reservedNames } = configuration;

export function sweep(richieName: richies, htmlSource: string): string {
const htmlDOM: CheerioAPI = load(htmlSource);

switch (richieName) {
case "article":
return _cleanArticle(htmlDOM);

case "restaurant":
case "crestaurant":
return _cleanRestaurant(htmlDOM);

case "course":
case "ccourse":
return _cleanCourse(htmlDOM);

case "event":
return _cleanEvents(htmlDOM);

case "video":
return _cleanVideo(htmlDOM);

case "localbusiness":
return _cleanLocalBusiness(htmlDOM);

case "product":
case "productwv":
return _cleanProduct(htmlDOM);

case "software":
return _cleanSoftwareApp(htmlDOM);

default:
return "null";
}
}

function _cleanArticle(DOMTree: CheerioAPI): string {
DOMTree("body").removeAttr(`data-${reservedNames.article.articleType}`);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanCourse(DOMTree: CheerioAPI) {
DOMTree(`[data-${reservedNames.course.language}]`).removeAttr(
`data-${reservedNames.course.language}`,
);

DOMTree(`[data-${reservedNames.course.courseFrequency}]`).removeAttr(
`data-${reservedNames.course.courseFrequency}`,
);

DOMTree(`[data-${reservedNames.course.courseRepeatation}]`).removeAttr(
`data-${reservedNames.course.courseRepeatation}`,
);

DOMTree(`[data-${reservedNames.common.currencyDataVar}]`).removeAttr(
`data-${reservedNames.common.currencyDataVar}`,
);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanRestaurant(DOMTree: CheerioAPI): string {
DOMTree(
`[data-${reservedNames.businessEntity.reservationDataVar}]`,
).removeAttr(`data-${reservedNames.businessEntity.reservationDataVar}`);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanSoftwareApp(DOMTree: CheerioAPI): string {
DOMTree(`[data-${reservedNames.common.currencyDataVar}]`).removeAttr(
`data-${reservedNames.common.currencyDataVar}`,
);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanVideo(DOMTree: CheerioAPI): string {
DOMTree(`[data-${reservedNames.video.startOffsetDataVar}]`).removeAttr(
`data-${reservedNames.video.startOffsetDataVar}`,
);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanLocalBusiness(DOMTree: CheerioAPI): string {
DOMTree(
`[data-${reservedNames.businessEntity.reservationDataVar}]`,
).removeAttr(`data-${reservedNames.businessEntity.reservationDataVar}`);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanEvents(DOMTree: CheerioAPI): string {
DOMTree(`[data-${reservedNames.common.MO}]`).removeAttr(
`data-${reservedNames.common.MO}`,
);

DOMTree(`[data-${reservedNames.events.status}]`).removeAttr(
`data-${reservedNames.events.status}`,
);

DOMTree(`[data-${reservedNames.common.currencyDataVar}]`).removeAttr(
`data-${reservedNames.common.currencyDataVar}`,
);

const htmlString = DOMTree.html();
return htmlString;
}

function _cleanProduct(DOMTree: CheerioAPI): string {
DOMTree(`[data-${reservedNames.common.currencyDataVar}]`).removeAttr(
`data-${reservedNames.common.currencyDataVar}`,
);

DOMTree(`[data-${reservedNames.product.offer.availability}]`).removeAttr(
`data-${reservedNames.product.offer.availability}`,
);

DOMTree(
`[data-${reservedNames.product.offer.itemCondition}]`,
).removeAttr(`data-${reservedNames.product.offer.itemCondition}`);

DOMTree(
`[data-${reservedNames.product.offer.shippingDetails.deliveryOver}]`,
).removeAttr(
`data-${reservedNames.product.offer.shippingDetails.deliveryOver}`,
);

DOMTree(
`[data-${reservedNames.product.offer.shippingDetails.rangeDataVar}]`,
).removeAttr(
`data-${reservedNames.product.offer.shippingDetails.rangeDataVar}`,
);

DOMTree(`[data-${reservedNames.product.variesByDataVar}]`).removeAttr(
`data-${reservedNames.product.variesByDataVar}`,
);

const htmlString = DOMTree.html();
return htmlString;
}
4 changes: 3 additions & 1 deletion richie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
richies,
} from "./lib/options";
import { createJsonLD, writeOutput } from "./lib/utilities";
import { sweep } from "./lib/sweeper";

const functionMap: Record<richies, richieOPS> = {
article: {
Expand Down Expand Up @@ -150,8 +151,9 @@ export async function richie(

const serializedData = serializer(...serializerParams);
const richResultSnippet = createJsonLD(serializedData);
const cleanSource = sweep(richieName, source);

writeOutput(source, destinationFile, richResultSnippet)
writeOutput(cleanSource, destinationFile, richResultSnippet)
.then(() => {
resolve();
})
Expand Down
2 changes: 1 addition & 1 deletion test/outputs/OP_Of_(1) article.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</script>
</head>

<body data-articletype="NewsArticle">
<body>
<!-- article type - Article, NewsArticle, BlogPosting-->
<!-- published date -->
<p class="rjs-article-pdt">2024-02-06 08:37 PM</p>
Expand Down
28 changes: 9 additions & 19 deletions test/outputs/OP_Of_(10) courses.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
<!-- course instance 1 -->
<div id="rjs-course-1">
<!-- name of course with language as data attribute-->
<p class="rjs-course-1-name" data-lang="English">Python Crash Course</p>
<p class="rjs-course-1-name">Python Crash Course</p>

<!-- instructor name -->
<!-- "By<space>Instructor" can be added or straight forward name in instructor inner text-->
Expand All @@ -141,13 +141,11 @@
</p>

<!-- course duration -->
<p class="rjs-course-1-period" data-freq="monthly" data-rep="8">
68 Hours
</p>
<p class="rjs-course-1-period">68 Hours</p>

<!-- cost of course with currency as data-->
<!-- digit from innertext is only be taken -->
<p class="rjs-course-1-cost" data-currency="usd">100 USD</p>
<p class="rjs-course-1-cost">100 USD</p>

<!-- publisher url with publisher name as innnertext -->
<a class="rjs-course-1-purl" href="https://lms.com/">
Expand All @@ -163,9 +161,7 @@
<!-- course instance 2 -->
<div id="rjs-course-2">
<!-- name of course with language as data attribute-->
<p class="rjs-course-2-name" data-lang="Tamil,French">
Node.Js introduction
</p>
<p class="rjs-course-2-name">Node.Js introduction</p>

<!-- instructor name -->
<!-- "By<space>Instructor" can be added or straight forward name in instructor inner text-->
Expand All @@ -190,13 +186,11 @@
</p>

<!-- course duration -->
<p class="rjs-course-2-period" data-freq="daily" data-rep="4">
24 Hours
</p>
<p class="rjs-course-2-period">24 Hours</p>

<!-- cost of course with currency as data-->
<!-- digit from innertext is only be taken -->
<p class="rjs-course-2-cost" data-currency="usd">2000 USD</p>
<p class="rjs-course-2-cost">2000 USD</p>

<!-- publisher url with publisher name as innnertext -->
<a class="rjs-course-2-purl" href="https://edutube.com/">
Expand All @@ -207,9 +201,7 @@
<!-- course instance 2 -->
<div id="rjs-course-3">
<!-- name of course with language as data attribute-->
<p class="rjs-course-3-name" data-lang="Tamil">
Java Intermediate to Advanced
</p>
<p class="rjs-course-3-name">Java Intermediate to Advanced</p>

<!-- lang as id able elem -->
<p class="rjs-course-3-lang">English</p>
Expand Down Expand Up @@ -237,13 +229,11 @@
</p>

<!-- course duration -->
<p class="rjs-course-3-period" data-freq="daily" data-rep="7">
1 Months
</p>
<p class="rjs-course-3-period">1 Months</p>

<!-- cost of course with currency as data-->
<!-- digit from innertext is only be taken -->
<p class="rjs-course-3-cost" data-currency="inr">2000 RS</p>
<p class="rjs-course-3-cost">2000 RS</p>

<!-- publisher url with publisher name as innnertext -->
<a class="rjs-course-3-purl" href="https://notoracle.com/">
Expand Down
44 changes: 9 additions & 35 deletions test/outputs/OP_Of_(11) events.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,14 @@
</p>

<!-- Event mode -->
<p>
Attendance Mode:
<span class="rjs-event-1-mode" data-mode="online">Online</span>
</p>
<p>Attendance Mode: <span class="rjs-event-1-mode">Online</span></p>

<!-- event status -->
<!-- <p>Event Status: <span class="rjs-event-1-sts" data-sts="postponed">Postponed</span></p> -->
<!-- <p>Event Status: <span class="rjs-event-1-sts" data-sts="toonline">Moved to Online</span></p> -->
<!-- <p>Event Status: <span class="rjs-event-1-sts" data-sts="cancelled">Cancelled</span></p> -->
<!-- <p>Event Status: <span class="rjs-event-1-sts" data-sts="rescheduled">Rescheduled</span></p> -->
<p>
Event Status:
<span class="rjs-event-1-sts" data-sts="scheduled">Scheduled</span>
</p>
<p>Event Status: <span class="rjs-event-1-sts">Scheduled</span></p>

<!-- location -->
<section class="rjs-event-1-loc">
Expand Down Expand Up @@ -205,10 +199,7 @@

<!-- offers -->
<!-- <p>Event Fee: <span class="rjs-event-1-cost">Free</span></p> -->
<p>
Event Fee:
<span class="rjs-event-1-cost" data-currency="free">FREE</span>
</p>
<p>Event Fee: <span class="rjs-event-1-cost">FREE</span></p>
<!-- booking link -->
<a
class="rjs-event-1-rlink"
Expand Down Expand Up @@ -250,18 +241,12 @@
</p>

<!-- Event mode -->
<p>
Attendance Mode:
<span class="rjs-event-2-mode" data-mode="offline">Offline</span>
</p>
<p>Attendance Mode: <span class="rjs-event-2-mode">Offline</span></p>

<!-- event status -->
<!-- <p>Event Status: <span class="rjs-event-2-sts" data-sts="postponed">Postponed</span></p> -->
<p>
Event Status:
<span class="rjs-event-2-sts" data-sts="toonline"
>Moved to Online</span
>
Event Status: <span class="rjs-event-2-sts">Moved to Online</span>
</p>
<!-- <p>Event Status: <span class="rjs-event-2-sts" data-sts="cancelled">Cancelled</span></p> -->
<!-- <p>Event Status: <span class="rjs-event-2-sts" data-sts="rescheduled">Rescheduled</span></p> -->
Expand Down Expand Up @@ -313,10 +298,7 @@

<!-- offers -->
<!-- <p>Event Fee: <span class="rjs-event-2-cost">Free</span></p> -->
<p>
Event Fee:
<span class="rjs-event-2-cost" data-currency="inr">200 INR</span>
</p>
<p>Event Fee: <span class="rjs-event-2-cost">200 INR</span></p>
<!-- booking link -->
<a
class="rjs-event-2-rlink"
Expand Down Expand Up @@ -356,16 +338,11 @@
<!-- Event mode -->
<p>
Attendance Mode:
<span class="rjs-event-3-mode" data-mode="mixed"
>Online and Offline</span
>
<span class="rjs-event-3-mode">Online and Offline</span>
</p>

<!-- event status -->
<p>
Event Status:
<span class="rjs-event-3-sts" data-sts="cancelled">Cancelled</span>
</p>
<p>Event Status: <span class="rjs-event-3-sts">Cancelled</span></p>

<!-- location -->
<section class="rjs-event-3-loc">
Expand Down Expand Up @@ -423,10 +400,7 @@

<!-- offers -->
<!-- <p>Event Fee: <span class="rjs-event-3-cost">Free</span></p> -->
<p>
Event Fee:
<span class="rjs-event-3-cost" data-currency="USD">10 USD</span>
</p>
<p>Event Fee: <span class="rjs-event-3-cost">10 USD</span></p>
<!-- booking link -->
<a
class="rjs-event-3-rlink"
Expand Down
Loading

0 comments on commit 8d72602

Please sign in to comment.