Scrapbox書籍を作るUserScript@0.2.0
必要なもの
gyazoの画像リスト
JSON形式
コードの一例
for Deno
実装したいこと
helpを書く
進捗状況をわかりやすくする
code:main.ts
import { getOCRs } from "../GyazoのURLリストからOCRテキストを一括取得するscript/mod.ts";
import { parse, sort, stringify, Page } from "../Scrapbox書籍のformat@0.2.0/mod.ts";
import { getGyazoToken } from "../scrapbox-userscript-std/rest.ts";
import Parser from "../papaparse/mod.ts";
import type { ImportedLightPage } from "../scrapbox-jp%2Ftypes/rest.ts";
const gyazoList = JSON.parse(await Deno.readTextFile(Deno.args0)) as string[]; const accessToken = Deno.args2; // OCRを取り込む
const images: string[] = [];
const ocrs: string[] = [];
let counter = 0;
const errors = [] as number[];
for await (const result of getOCRs(gyazoList, accessToken)) {
counter++;
if (!result.success) {
if (!(result.reason instanceof Error)) throw result.reason;
console.error(result.reason);
errors.push(counter);
} else {
images.push(result.value.url);
ocrs.push(result.value.text);
console.info(${result.value.text.slice(0, 10)}...);
}
console.log(${counter - errors.length}/${gyazoList.length} got, ${errors.length} failed);
}
console.log(creating the json file...);
// 目次を取り込む
const csv = await Deno.readTextFile(Deno.args1); const { data } = Parser.parse<string[]>(csv);
// JSONを作る
const pages: ImportedLightPage[] = [];
for (const page of sort(parse(data))) {
const lines = stringify(page, images, ocrs).split("\n");
pages.push({ title: lines0, lines }); }
await Deno.writeTextFile("scrapbox.json", JSON.stringify({ pages }));
console.log("created the json file.");
テキストのみ
code:onlyText.ts
import { parse, sort, stringify, Page } from "../Scrapbox書籍のformat@0.2.0/mod.ts";
import Parser from "../papaparse/mod.ts";
import type { ImportedLightPage } from "../scrapbox-jp%2Ftypes/rest.ts";
const ocrs = JSON.parse(await Deno.readTextFile(Deno.args0)) as string[]; console.log(creating the json file...);
// 目次を取り込む
const csv = await Deno.readTextFile(Deno.args1); const { data } = Parser.parse<string[]>(csv);
// JSONを作る
const pages: ImportedLightPage[] = [];
for (const page of sort(parse(data))) {
const lines = stringify(page, new Array<string>(ocrs.length).fill(""), ocrs).split("\n");
pages.push({ title: lines0, lines }); }
await Deno.writeTextFile("scrapbox.json", JSON.stringify({ pages }));
console.log("created the json file.");
text fileをjsonにする (web browser用)
code:makeJson.ts
import { useStatusBar } from "../scrapbox-userscript-std/dom.ts";
import { upload } from "../scrapbox-file-uploader/mod.ts";
const main = async () => {
const textList = [] as string[];
const { render, dispose } = useStatusBar();
try {
const fileList = await upload({ accept: "text/plain", multiple: true });
if (!fileList) return;
const compare = new Intl.Collator().compare;
const files = Array.from(fileList)
.sort((a, b) => compare(a.name, b.name));
let counter = 0;
for (const file of files) {
textList.push(await file.text());
render(
{ type: "spinner" },
{
type: "text",
text: ${files.length} files, ${counter} proceed,
},
);
}
render(
{ type: "check-circle" },
{ type: "text", text: Finish process. },
);
console.log(textList);
window.open(URL.createObjectURL(blob));
} finally {
setTimeout(() => {
dispose();
}, 1000);
}
};
await main();