Skip to content

Commit

Permalink
Less parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
rberenguel committed Nov 11, 2024
1 parent 7810426 commit ab0ef57
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 163 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</body>
<script src="libs/luxon.js"></script>
<script src="lib/common.js"></script>
<script src="lib/parser.js"></script>
<script src="lib/taskUtils.js"></script>
<script src="lib/quoteUtils.js"></script>
<script src="lib/weatherUtils.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const numMojis = [

const linkRegex = /\[(.*?)\]\((.*?)\)/;
const linkPlusRegex = /\[(.*?)\]\((.*?)\)(\s*.*)/;
const linkWithShortcut = /`(.*?)`\s*\[(.*?)\]\((.*?)\)/;


function toTop(div) {
const allDivs = document.querySelectorAll("div");
Expand Down
4 changes: 3 additions & 1 deletion lib/linkUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Use different files for different columns, and prefix secondary URL title by *

window.hasKeys = ""; // Store the first key pressed

const linkWithShortcut = /`(.*?)`\s*\[(.*?)\]\((.*?)\)/;
// This parser would be a bit annoying to merge with the one in parser.js:
// This is stateful (parses the whole doc at once) but the other parses
// just blocks of a predefined shape. It's easier to keep them separate.

function textToLinkObjects(text) {
let links = [];
Expand Down
120 changes: 120 additions & 0 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
function textToObject(text, hPos = 0, filepath) {
const lines = text.split("\n");
let obj = {};
obj._hPos = hPos;
obj._filepath = filepath;
obj.lines = []; // Only used (for now) for quotes
let settings = false;
let projects = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Extract the task. If it has a link, it is the main link.
// Handle settings and projects inline, breaking.
if (line.startsWith("# ")) {
let cleaned = line.replace("# ", "").trim();
if (cleaned == "Settings") {
settings = true;
obj._valid = true;
obj.settings = {};
continue;
}
if (cleaned == "Projects") {
projects = true;
obj._valid = true;
obj.projects = [];
continue;
}
if (cleaned == "hr") {
obj.hr = true;
obj._valid = true;
continue;
}
if (cleaned.startsWith("[x]") || cleaned.startsWith("[X]")) {
obj.done = true;
cleaned = cleaned.slice(3);
}
if (cleaned.endsWith("(someday)") || cleaned.endsWith("(Someday)")) {
obj.someday = true;
obj.prio = -1000;
cleaned = cleaned.replace(/\s+\(.omeday\)/, "");
}
const linkMatch = cleaned.match(linkPlusRegex);
if (linkMatch) {
obj.text = linkMatch[1] + (linkMatch[3] ?? "");
obj.link = linkMatch[2];
} else {
obj.text = cleaned;
}
obj._valid = true;
continue;
}
if (line == "[x]" || line == "[X]") {
obj.done = true;
continue;
}
if (line.startsWith("> ")) {
obj.lines.push(line.replace(/^> /, ""));
obj._valid = true;
continue;
}
if (line.startsWith("prio:") ) {
if(!obj.someday){
obj.prio = parseInt(line.replace("prio:", ""));
}
continue;
}
if (line.startsWith("color:")) {
obj.color = line.replace("color:", "").trim();
continue;
}
if (line.startsWith("extracolor:")) {
obj.extraColor = line.replace("extracolor:", "").trim();
continue;
}

// Get the lists of links or properties if it's a settings list
if (line.startsWith("- ")) {
const cleaned = line.replace("- ", "").trim();
if (settings) {
const [prop, val] = cleaned.split(":");
obj.settings[prop.trim()] = val.trim();
continue;
}
if (projects) {
const thingy = cleaned.split(",");
if (thingy.length == 1) {
obj.projects.push(thingy[0]);
} else {
obj.projects.push(thingy.map((p) => p.trim()));
}
continue;
}
const linkMatch = cleaned.match(linkRegex);
if (linkMatch) {
if (obj.links) {
obj.links.push([linkMatch[1], linkMatch[2]]);
} else {
obj.links = [[linkMatch[1], linkMatch[2]]];
}
continue;
} else {
// If we are in a list, haven't matched a known property,
// and match no link, we are in a log message.
if (obj.msg) {
obj.msg.push(cleaned);
} else {
obj.msg = [cleaned];
}
}
} else {
// If the line does not start a list and we have not processed a known command…
const cleaned = line.trim();
if (cleaned) {
// Could be empty, so…
obj.extra = cleaned;
}
}
}
return obj;
}

77 changes: 34 additions & 43 deletions lib/quoteUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// TODO(me) Just a copypaste of pieces of taskUtils to get this working. Needs cleanup, and might not merge settings correctly
// TODO(me) This might need cleanup, it has been hacked quickly into the parser

/*
- Handles settings as in task lists
- Start the text by adding a blockquote block > (can be multiple lines)
- Add a line at the end with the author or reference.
Example quotes file:
# Settings
Expand All @@ -11,50 +15,23 @@ Example quotes file:
- borderRadius: 0.2em
- width: 20em
- padding: 1em
- margin-bottom: 2em
---
> I love deadlines. I love the whooshing noise they make as they go by
Douglas Adams
---
> Everyone has a plan: until they get punched in the face
*/
Mike Tyson
function textToQuoteObject(text) {
const lines = text.split("\n");
let obj = {};
obj._valid = true;
obj.lines = [];
let settings = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line == "# Settings") {
settings = true;
obj._valid = true;
obj.settings = {};
continue;
}
if (line.startsWith("- ") && settings) {
const cleaned = line.replace("- ", "").trim();
const [prop, val] = cleaned.split(":");
obj.settings[prop.trim()] = val.trim();
continue;
}
console.log(line);
if (line.startsWith("> ")) {
obj.lines.push(line.replace(/^> /, ""));
}
if (line.length === 0) {
continue;
}
obj.author = line;
}
return obj;
}
*/

function quotesFromMarkdown(paths, cb, basepath) {
const promises = [];
Expand All @@ -73,13 +50,13 @@ function quotesFromMarkdown(paths, cb, basepath) {
const blocks = markdown.split(/---/);
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i];
let obj = textToQuoteObject(block);
let obj = textToObject(block);
if (basepath) {
obj = textToQuoteObject(block);
obj = textToObject(block);
}

if (!obj._valid) {
tasks.push({
quotes.push({
text: `Error loading quote ${i} from file ${path}`,
error: true,
});
Expand All @@ -101,38 +78,52 @@ function quotesFromMarkdown(paths, cb, basepath) {
.catch((error) => console.error("Error loading markdown file:", error));
}

const getSettings = (objs) => {
return objs.filter(o => o.settings)
}

function addQuotesToDiv(_quotes, targetDivId) {
const quotes = shuffleArray(_quotes);
const settings = getSettings(_quotes)
const quotes = settings.concat(shuffleArray(_quotes)); // Just place them at the beginning and call it a day
const d = () => document.createElement("DIV");
const targetDiv = document.getElementById(targetDivId);
if (!targetDiv) {
console.error("Target div not found:", targetDivId);
return;
}
let wrapperNode = d();
wrapperNode.id = "quoteWrapper";
let rendered = false;
for (let i = 0; i < quotes.length; i++) {
const quote = quotes[i];
if (quote.settings) {
for (const key in quote.settings) {
targetDiv.style[key] = quote.settings[key];
wrapperNode.style[key] = quote.settings[key];
}
continue;
}
if(rendered){
// Just show one quote.
return;
}
const div = d();
const color = colors[i % colors.length];
const color = colors[Math.floor((Math.random()*colors.length) % colors.length)];
div.style.color = `var(${color})`;
div.classList.add("quote-wrapper");
div.classList.add("quote");
const q = d();
q.classList.add("quote");
q.classList.add("text");
const a = d();
a.classList.add("author");
for (const line of quote.lines) {
const p = document.createElement("p");
p.textContent = line;
q.appendChild(p);
}
a.textContent = quote.author;
a.textContent = quote.extra;
div.appendChild(q);
div.appendChild(a);
targetDiv.appendChild(div);
wrapperNode.appendChild(div);
targetDiv.appendChild(wrapperNode)
rendered = true;
}
}
Loading

0 comments on commit ab0ef57

Please sign in to comment.