Skip to content

Commit

Permalink
cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
ingiestein committed Jul 17, 2024
1 parent b2afd55 commit c7e90da
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 219 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ The original plug-in by @pacokwon required local installation of the scriptures,

General Conference support essentially scrapes the given webpage for the talk title, the author information, and then the paragraph's selected. This is highly dependent on programmers keeping consistent IDs for the different HTML elements. Even between 2023 and 2023 conferences there were differences. I believe I was able to account for the differences between the different years, thanks in large part to other in the LDS GitHub community. I cannot remember whose code had solved this problem, but I'm grateful.

# NeoVim
# NeoVim Plugin

If you like to use NeoVim and want similar functionality check out [LDSLibrary.nvim](https://github.com/ingiestein/LDSLibrary.nvim), and [obsidian.nvim](https://github.com/epwalsh/obsidian.nvim).

Expand Down
79 changes: 0 additions & 79 deletions src/env-paths.ts

This file was deleted.

6 changes: 1 addition & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Plugin } from "obsidian";
import { BookOfMormonSettings, DEFAULT_SETTINGS } from "./settings";
import { GenConSuggester, VerseSuggester } from './suggestion/suggester';
import { installTranslation } from "./translation";
import { BookOfMormonSettingTab } from './ui/BookOfMormonSettingTab';

// import {fetchGenConTalk} from './utils/generalconference'

export default class BookOfMormonPlugin extends Plugin {
settings: BookOfMormonSettings;

Expand All @@ -14,7 +11,7 @@ export default class BookOfMormonPlugin extends Plugin {
this.addSettingTab(new BookOfMormonSettingTab(this.app, this));
this.registerEditorSuggest(new VerseSuggester(this));
this.registerEditorSuggest(new GenConSuggester(this))
// console.log("GenConSuggester Loaded");

}

onunload() {}
Expand All @@ -29,7 +26,6 @@ export default class BookOfMormonPlugin extends Plugin {

async saveSettings() {
await this.saveData(this.settings);
await installTranslation(this.manifest.id, this.settings.language);
}
}

7 changes: 0 additions & 7 deletions src/metadata.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/suggestion/VerseSuggestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class VerseSuggestion {
[this.book_title_short, this.volume_title_short] =
this.getShortenedName(this.book);
this.url = this.getUrl();
// console.log(`Scripture URL: ${this.url}`);

let scriptdata: ScriptureData = await fetchScripture(this.url, "GET");
this.book_title_in_language = scriptdata.in_language_book
this.chapter_data.push(scriptdata);
Expand Down
20 changes: 10 additions & 10 deletions src/suggestion/suggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ export class VerseSuggester extends EditorSuggest<VerseSuggestion> {

if (fullMatch === null) return [];

// console.log("Got Match: ", fullMatch)


const book = fullMatch[1];
const chapter = Number(fullMatch[2]);
const verses:number[] = this.parseVerses(fullMatch[3]);
// console.log("Book: %s\nChapter: %s\nVerses: %s",book,chapter,verses.join(','))


const suggestion = new VerseSuggestion(
this.plugin.manifest.id,
Expand Down Expand Up @@ -95,15 +95,15 @@ export class VerseSuggester extends EditorSuggest<VerseSuggestion> {

expandRange(range: string): number[] {
const [s, e] = range.split('-');
// console.log("Expand range strings: %s, %s",s,e);

let start = Number(s.trim());
let end = Number(e.trim());
// console.log("Expand range ints: %s, %s",start,end);

const result = [];

for (let i = start; i <= end; i++) {
result.push(i);
// console.log("integer: %s", i);

}
return result;
}
Expand All @@ -115,7 +115,7 @@ export class VerseSuggester extends EditorSuggest<VerseSuggestion> {
for (const item of items) {
if (item.includes('-')) {
result = result.concat(this.expandRange(item));
// console.log("Result from range concat: ", result)

} else {
result.push(Number(item));
}
Expand Down Expand Up @@ -145,7 +145,7 @@ export class GenConSuggester extends EditorSuggest<GenConSuggestion> {
if (!match) {
return null;
}
// console.log("Found GenCon Match");

return {
start: {
line: cursor.line,
Expand All @@ -164,10 +164,10 @@ export class GenConSuggester extends EditorSuggest<GenConSuggestion> {
const { language, linkType, createChapterLink } = this.plugin.settings;

if (fullMatch === null) {
// console.log("getSuggestion didn't match");

return [];
}
// console.log(`getSuggestion matched: ${fullMatch}`);


const talk = fullMatch[0].replace(/^\:MC /, "");

Expand All @@ -177,7 +177,7 @@ export class GenConSuggester extends EditorSuggest<GenConSuggestion> {
linkType,
);
await suggestion.loadTalk();
// console.log(`Suggestion: ${suggestion}`);

return [suggestion];
}

Expand Down
41 changes: 0 additions & 41 deletions src/translation.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/ui/BookOfMormonSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class BookOfMormonSettingTab extends PluginSettingTab {

containerEl.createEl("h2", { text: "About" });
containerEl.createSpan({}, (span) => {
span.innerHTML = `<a href="https://github.com/ingiestein/obsidian-lds-scriptures-plugin">Github</a>`;

span.createEl('a', {
href: 'https://github.com/ingiestein/obsidian-lds-scriptures-plugin',
text: 'Github'});

});
}
}
12 changes: 2 additions & 10 deletions src/utils/apiutils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Some of the following code is adapted from @epeters3 in his repository https://github.com/epeters3/gospel-search



export function cheerioFind($: cheerio.Root, queries: any[]): cheerio.Cheerio | null {
for (const query of queries) {
const elements = $(query.name).filter((_, el) => {
Expand All @@ -18,20 +16,14 @@ export function cheerioFind($: cheerio.Root, queries: any[]): cheerio.Cheerio |
const genconregex = /https:\/\/www\.churchofjesuschrist\.org\/study(\/[\w-]+\/\d{4}\/\d{2}\/[\w-]+)/;
const scriptureregex = /https:\/\/www\.churchofjesuschrist\.org\/study(\/[\w-]+\/[\w-]+\/[\w-]+\/[\w-]+)/;
const match = url.match(genconregex) ? url.match(genconregex): url.match(scriptureregex)?url.match(scriptureregex):null ;
// console.log(`REGEX MATCH: ${match}`)

return match ? match[1] : null;
}

// export function extractGenConURLPath(url: string): string | null {
// const regex = /https:\/\/www\.churchofjesuschrist\.org\/study(\/general-conference\/\d{4}\/\d{2}\/[\w-]+)/;
// const match = url.match(regex);
// console.log(`REGEX MATCH: ${match}`)
// return match ? match[1] : null;
// }

export function buildAPIURL(lang:string,url:string){
let path = extractURLPath(url)
// console.log(`Path extracted: ${path}`)

return `https://www.churchofjesuschrist.org/study/api/v3/language-pages/type/content?lang=${lang}&uri=` + path;
}

Expand Down
45 changes: 4 additions & 41 deletions src/utils/generalconference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ export async function fetchGenConTalk(url:string,method: 'GET' | 'POST' | 'PATCH
throw new Error('This can only refernce talks from General Conference.');
}

var talkurl = buildAPIURL(lang,url)

console.log(`Parsed Data from URL: `, parsedData);
console.log(`Starting AXIOS request of ${talkurl}`)
// const response = await axios.get(talkurl);
let talkurl = buildAPIURL(lang,url)

const response = await requestUrl({
url: talkurl,
Expand All @@ -34,7 +30,7 @@ export async function fetchGenConTalk(url:string,method: 'GET' | 'POST' | 'PATCH
}
});
if (response.status === 401 || response.status === 402) {
console.log(response.status);

return {
title,
author,
Expand All @@ -55,32 +51,7 @@ export async function fetchGenConTalk(url:string,method: 'GET' | 'POST' | 'PATCH
let authorrole = authorRoleElement ? authorRoleElement.text().trim() : "Author role not found";
author.push(authorname);
author.push(authorrole);
console.log(`Athor array: ${author}`)
// let body = $(PARAGRAPHS_IN_BODY_QUERY.name).map((_, el) => $(el).text().trim()).get();
// console.log(`Body?: ${body}`)

// if (parsedData.paragraphs){
// const {start,end} = parsedData.paragraphs;
// const paragraphEnd = end !== undefined ? end : start;
// content = body.slice(start-1, end);
// }


// title = $('#title1').text().trim();

// if (title.length === 0){
// title = $('h1').first().text().trim();


// // title = $('#p1').text().trim();
// }
// // author = [$('#author1').text().trim(), $('#author2').text().trim()].filter(Boolean);
// // author = [$('#author-name').text().trim(), $('#author-role').text().trim()].filter(Boolean);
// author = [$('.author-name').text().trim(), $('.author-role').text().trim()].filter(Boolean);

// if (author.length === 0) {
// let author = [$('#author1').text().trim(), $('#author2').text().trim()].filter(Boolean);
// }
if (parsedData.paragraphs) {
const { start, end } = parsedData.paragraphs;
const paragraphEnd = end !== undefined ? end : start;
Expand All @@ -99,18 +70,10 @@ export async function fetchGenConTalk(url:string,method: 'GET' | 'POST' | 'PATCH
month = parsedData.pathParts[3];
setting = "General Conference";

console.log(
title,
author,
content,
year,
month,
setting
)


if (!title || !content) {
console.log(`title error: ${title}`);
console.log(`content error: ${content}`);

throw new Error('Unable to extract the necessary data from the webpage.');
}

Expand Down
Loading

0 comments on commit c7e90da

Please sign in to comment.