Scrapbox書籍を作るUserScript@0.2.0
scrapbox書籍を作るUserScript
Scrapbox書籍のformat@0.2.0準拠
必要なもの
scrapbox書籍用目次
Scrapbox書籍のformat@0.2.0に沿ったもの
gyazoの画像リスト
JSON形式
Gyazo APIのAccess Token
コードの一例
for Deno
実装したいこと
helpを書く
deno-cliffyを使う
進捗状況をわかりやすくする
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Scrapbox書籍を作るUserScript@0.2.0/main.ts
$ deno run --allow-read --allow-write --allow-net=api.gyazo.com -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Scrapbox書籍を作るUserScript@0.2.0/main.ts
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.");
テキストのみ
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Scrapbox書籍を作るUserScript@0.2.0/onlyText.ts
$ deno run --allow-read --allow-write -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Scrapbox書籍を作るUserScript@0.2.0/onlyText.ts
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用)
https://scrapbox-bundler.vercel.app/?url=https://scrapbox.io/api/code/takker/Scrapbox書籍を作るUserScript@0.2.0/makeJson.ts&bundle&minify&run&output=newtab&reload
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/Scrapbox書籍を作るUserScript@0.2.0/makeJson.ts
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);
const blob = new Blob(JSON.stringify(textList), { type: "application/json" });
window.open(URL.createObjectURL(blob));
} finally {
setTimeout(() => {
dispose();
}, 1000);
}
};
await main();
#2022-11-03 02:05:05
#2022-08-20 14:10:28 json作成時のエラー処理を追加
#2022-08-12 09:55:26 status-bar (scrapbox)に進捗を表示する
#2022-08-04 14:52:54