import { getSyllabus } from "./syllabus.ts"; import { getCourseCode, getLetusEditon } from "./letus.ts"; import { encodeTitleURI } from "../scrapbox-userscript-std/dom.ts"; import { lightFormat } from "../date-fns/lightFormat.ts"; export const scrap = async (project: string) => { if (!location.href.includes("https://letus.ed.tus.ac.jp/course/view.php")) return; const pageTitle = window.prompt("page title"); if (!pageTitle) return; const courseCode = getCourseCode(document); const year = getLetusEditon(document); const { title, englishTitle, instructors, semester, hours, credits, descriptions, objectives, outcomes, prerequisites, preparationAndReview, evaluation, references, plan, } = await getSyllabus(courseCode, year); const letusURL = location.href; const body = [ "table:basic information", ` Course title\t${format(title)}`, ` Instructor\t${ instructors.map((instructor) => `[${instructor.replace(" ", "")}]`) }`, ` Schedule\t${semester} ${hours.join(" ")}`, ` Course credits\t${credits}`, ` Course code\t${courseCode}`, `[LETUS ${letusURL}]`, `[syllabus https://tus-class-api.vercel.app/v1/${year}/${courseCode}]`, "", "Descriptions", ...descriptions.map((text) => ` ${format(text)}`), "", "Objectives", ...objectives.map((text) => ` ${format(text)}`), "", "Outcomes", ...outcomes.map((text) => ` ${format(text)}`), "", "Course notes prerequisites", ...prerequisites.map((text) => ` ${format(text)}`), "", "Preparation and review", ...preparationAndReview.map((text) => ` ${format(text)}`), "", "Evaluation", ...evaluation.map((text) => ` ${format(text)}`), "", "Materials", ...references.map((text) => ` ${format(text)}`), "", "Plan", ...plan.map((text) => ` ${format(text)}`), "", `#${lightFormat(new Date(), "yyyy-MM-dd HH:mm:ss")}`, ].join("\n"); window.open( `https://scrapbox.io/${project}/${encodeTitleURI(pageTitle)}?body=${ encodeURIComponent(body) }`, "_self", ); }; const format = (text: string): string => text .replace(/\s+$/, "") // 末尾の余計な空白を消す .replace(/^(\s*)・/, "$1 ") // ・を箇条書きに変える .replace( /[A-Za-z0-9]/g, (s) => String.fromCharCode(s.charCodeAt(0) - 0xFEE0), ) // 全角英数を半角に直す .replace(/\s?\[/g, "[") // リンク記法をescapeする .replace(/\s?\[/g, "[") .replace(/(\d+).\s*/g, "$1. ") // 番号付き箇条書きにする .replaceAll(".", "。") // 句読点を変換する .replaceAll(",", "、");