Compare commits
37 Commits
Author | SHA1 | Date | |
---|---|---|---|
343c36a9f0 | |||
123bf51001 | |||
252863c813 | |||
a858ea4b60 | |||
cca6de1877 | |||
b43a837c6a | |||
c0ce69af6f | |||
041129dc83 | |||
89a3df17e6 | |||
da80c4690b | |||
001b90744b | |||
93df271f79 | |||
cdeef54f68 | |||
90f1547e02 | |||
e5b173155a | |||
19eaf2d664 | |||
9573291582 | |||
1a1431c85e | |||
711880a670 | |||
4f043d2bd7 | |||
43d5916e52 | |||
51aaf27fda | |||
d5b9ef8f04 | |||
22487224f2 | |||
6cc772bbf2 | |||
430130cdaf | |||
9b11f14c84 | |||
25378d2d3c | |||
237d4c4349 | |||
b0fe668133 | |||
7ee7d5f291 | |||
4f4aee6a3e | |||
fa44985594 | |||
2113f930a7 | |||
969de4aab7 | |||
593f853143 | |||
7972e679ab |
@ -1,32 +0,0 @@
|
||||
name: Build and Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build and release binaries
|
||||
uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/deno-publish@main
|
||||
with:
|
||||
entrypoint: main.ts
|
||||
compile-flags: "--allow-read --allow-write --allow-env --allow-net"
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GIT_PAT }}
|
||||
|
||||
publish:
|
||||
needs: build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Deno
|
||||
uses: denoland/setup-deno@v1
|
||||
with:
|
||||
deno-version: 2.3.1
|
||||
|
||||
- name: Publish to JSR
|
||||
run: deno publish --token ${{ secrets.JSR_TOKEN }}
|
48
.gitea/workflows/release.yml
Normal file
48
.gitea/workflows/release.yml
Normal file
@ -0,0 +1,48 @@
|
||||
name: Create Version Tag
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- "prerelease-*"
|
||||
|
||||
jobs:
|
||||
version-check:
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
tag_created: ${{ steps.tag.outputs.tag_created }}
|
||||
tag_name: ${{ steps.tag.outputs.tag_name }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Run version check
|
||||
id: tag
|
||||
uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/version-check@v1
|
||||
|
||||
build-release:
|
||||
if: needs.version-check.outputs.tag_created == 'true'
|
||||
needs: version-check
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Build and release binaries
|
||||
uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/deno-release@main
|
||||
with:
|
||||
entrypoint: main.ts
|
||||
compile-flags: "--allow-read --allow-write --allow-env --allow-net --include asciiart.txt"
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.GIT_PAT }}
|
||||
|
||||
publish-release:
|
||||
needs: build-release
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Set up Deno
|
||||
uses: denoland/setup-deno@v1
|
||||
with:
|
||||
deno-version: 2.3.1
|
||||
|
||||
- name: Publish to JSR
|
||||
run: deno publish --token ${{ secrets.JSR_PAT }}
|
@ -1,13 +0,0 @@
|
||||
name: Create Version Tag
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: https://git.cyborggrizzly.com/bearmetal/ci-actions/version-check@main
|
39
CHANGELOG.md
Normal file
39
CHANGELOG.md
Normal file
@ -0,0 +1,39 @@
|
||||
# Changelog
|
||||
|
||||
## v1.0.2 (2025-05-20)
|
||||
|
||||
<!-- auto-changelog -->
|
||||
|
||||
## v1.0.1 (2025-05-7)
|
||||
|
||||
<!-- auto-changelog -->
|
||||
|
||||
### Known Issues
|
||||
|
||||
- help flags can cause issues
|
||||
|
||||
## v1.0.0 (2025-05-7)
|
||||
|
||||
### Features
|
||||
|
||||
- Check Code Tool
|
||||
- reads supplied code files to see if form fields are present and represented
|
||||
in a switch statement
|
||||
- Field Rename Tool
|
||||
- provide a search and replace pattern to bulk rename form fields
|
||||
- List Form Fields
|
||||
- Sometimes you just need to see what fields there are
|
||||
|
||||
### Known Issues
|
||||
|
||||
- Field rename does not represent full change applied to the field, only the
|
||||
replaced text
|
||||
- help flags can cause issues
|
||||
- ascii art is broken (sad face)
|
||||
- banana doesn't work
|
||||
|
||||
## v0.0.0 (never)
|
||||
|
||||
this is just here for a reference to the auto-changelog
|
||||
|
||||
<!-- auto-changelog -->
|
66
README.md
66
README.md
@ -1,7 +1,16 @@
|
||||
# Emma's Simple Form Field Checker
|
||||
# BearMetal PDF Tools
|
||||
|
||||
Compares a PDF form to a list of CS class files to see if all field names are
|
||||
present, excluding signature fields.
|
||||
A collection of tools for working with PDF forms.
|
||||
|
||||
## Features
|
||||
|
||||
- Check Code Tool
|
||||
- reads supplied code files to see if form fields are present and represented
|
||||
in a switch statement
|
||||
- Field Rename Tool
|
||||
- provide a search and replace pattern to bulk rename form fields
|
||||
- List Form Fields
|
||||
- Sometimes you just need to see what fields there are
|
||||
|
||||
## Prereqs
|
||||
|
||||
@ -11,7 +20,8 @@ Deno >=2.2 (not required if downloading .exe)
|
||||
|
||||
### Deno install
|
||||
|
||||
`deno task install` -> installs as global command `checkfields`
|
||||
`deno install -g --allow-read --allow-write --allow-net --allow-env jsr:@bearmetal/pdf-tools`
|
||||
-> installs as global command `pdf-tools`
|
||||
|
||||
### Compile
|
||||
|
||||
@ -20,17 +30,51 @@ Deno >=2.2 (not required if downloading .exe)
|
||||
> If you want it to be a global command, create an executables directory and add
|
||||
> it to the PATH
|
||||
|
||||
### Precompiled
|
||||
|
||||
Download the latest release from the
|
||||
[releases page](https://git.cyborggrizzly.com/BearMetal/pdf-tools/releases)
|
||||
|
||||
## Usage
|
||||
|
||||
`checkfields <path to PDF> <comma-separated list of paths to CS class files>`
|
||||
-OR- `checkfields` and follow prompts.
|
||||
`pdf-tools <tool> <args>` -> `<tool>` is one of the following
|
||||
|
||||
### Output
|
||||
- check-code
|
||||
- field-rename
|
||||
- list-form-fields
|
||||
|
||||
> All form fields present!
|
||||
## Contributing
|
||||
|
||||
-OR-
|
||||
Contributions are welcome!
|
||||
|
||||
> The following field names are not present in the CS code
|
||||
## License
|
||||
|
||||
> \<list of missing form fields\>
|
||||
GPL 3.0
|
||||
|
||||
---
|
||||
|
||||
### About this project
|
||||
|
||||
BearMetal PDF Tools is a collection of tools made to fix the current state of
|
||||
PDF form editing. Adobe Acrobat is a great tool, but it's not always the easiest
|
||||
to use, nor is it free. It also lacks some features that I find useful, such as
|
||||
bulk renaming of form fields. There's also a lack of powerful, free, and open
|
||||
source tools for PDF editing.
|
||||
|
||||
This project aims to fill that gap by providing a set of tools that can be used
|
||||
to edit PDF forms. The tools are written in Deno, a modern and secure runtime
|
||||
for JavaScript and TypeScript. They are designed to be easy to use and to
|
||||
provide a great user experience.
|
||||
|
||||
The tools are designed to be used in a terminal, and are not designed to be
|
||||
integrated into other applications. They are also not designed to be used as a
|
||||
library, but rather as a command line tool.
|
||||
|
||||
### About BearMetal
|
||||
|
||||
BearMetal is a project that aims to decrapify modern web development. It is a
|
||||
collection of tools, libraries, and resources that aim to make web development
|
||||
more accessible and less intimidating. The project is open source and free to
|
||||
use, and is designed to be used by anyone, regardless of skill level or
|
||||
experience. You can find a list of libraries and tools on
|
||||
[JSR](https://jsr.io/@bearmetal).
|
||||
|
@ -44,7 +44,9 @@ export class TerminalLayout {
|
||||
clearTimeout(this.debounceTimer);
|
||||
}
|
||||
this.debounceTimer = setTimeout(
|
||||
() => this.renderLayout(),
|
||||
() => {
|
||||
this.renderLayout();
|
||||
},
|
||||
this.debounceDelay,
|
||||
);
|
||||
}
|
||||
@ -76,6 +78,10 @@ export class TerminalLayout {
|
||||
block.renderInternal(usedLines + 1);
|
||||
usedLines += lines.length;
|
||||
}
|
||||
for (const name of this.layoutOrder) {
|
||||
const block = this.blocks[name];
|
||||
block.runPostRenderAction?.();
|
||||
}
|
||||
}
|
||||
|
||||
clearAll() {
|
||||
@ -111,6 +117,8 @@ export class TerminalBlock {
|
||||
private renderHeight: number = 0;
|
||||
private lastRenderRow = 1;
|
||||
|
||||
private lastRendered: string[] = [];
|
||||
|
||||
private preserveHistory = false;
|
||||
|
||||
constructor(private prepend: string = "") {}
|
||||
@ -193,27 +201,39 @@ export class TerminalBlock {
|
||||
}
|
||||
|
||||
renderInternal(startRow?: number) {
|
||||
this.lastRenderRow = startRow ?? this.lastRenderRow;
|
||||
this.clear(); // uses old renderedLineCount
|
||||
const outputLines: string[] = [];
|
||||
|
||||
const outputLines = this.renderLines.map((line) =>
|
||||
`${this.prepend}${line}\x1b[K`
|
||||
);
|
||||
const output = outputLines.join("\n");
|
||||
if (startRow !== undefined) {
|
||||
const moveCursor = `\x1b[${startRow};1H`;
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(moveCursor + output));
|
||||
} else {
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(output));
|
||||
for (let i = 0; i < this.renderLines.length; i++) {
|
||||
const line = `${this.prepend}${this.renderLines[i]}`;
|
||||
const previous = this.lastRendered[i];
|
||||
if (line !== previous) {
|
||||
const moveToLine = `\x1b[${(startRow ?? this.lastRenderRow) + i};1H`;
|
||||
outputLines.push(moveToLine + line + "\x1b[K");
|
||||
}
|
||||
}
|
||||
|
||||
// update rendered line count *after* rendering
|
||||
this.renderedLineCount = outputLines.reduce(
|
||||
(count, line) =>
|
||||
count +
|
||||
Math.ceil((line.length) / (Deno.consoleSize().columns || 80)),
|
||||
0,
|
||||
);
|
||||
if (this.lastRendered.length > this.renderLines.length) {
|
||||
const baseRow = startRow ?? this.lastRenderRow;
|
||||
for (let i = this.renderLines.length; i < this.lastRendered.length; i++) {
|
||||
const moveToLine = `\x1b[${baseRow + i};1H\x1b[2K`;
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(moveToLine));
|
||||
}
|
||||
}
|
||||
|
||||
const baseRow = startRow ?? this.lastRenderRow;
|
||||
const excessLines = this.renderHeight - this.renderLines.length;
|
||||
for (let i = 0; i < excessLines; i++) {
|
||||
const moveToLine = `\x1b[${
|
||||
baseRow + this.renderLines.length + i
|
||||
};1H\x1b[2K`;
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(moveToLine));
|
||||
}
|
||||
|
||||
this.lastRendered = [...this.renderLines];
|
||||
this.renderedLineCount = this.renderHeight;
|
||||
this.lastRenderRow = baseRow;
|
||||
const output = outputLines.join("\n");
|
||||
Deno.stdout.writeSync(new TextEncoder().encode(output));
|
||||
}
|
||||
|
||||
clear() {
|
||||
@ -242,6 +262,17 @@ export class TerminalBlock {
|
||||
return this.fixedHeight ?? 0;
|
||||
}
|
||||
|
||||
private _postRenderAction?: () => void;
|
||||
setPostRenderAction(action: (this: TerminalBlock) => void) {
|
||||
this._postRenderAction = action;
|
||||
}
|
||||
runPostRenderAction() {
|
||||
if (this._postRenderAction) {
|
||||
this._postRenderAction.call(this);
|
||||
this._postRenderAction = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
get lineCount() {
|
||||
return this.renderLines.length;
|
||||
}
|
||||
|
22
cli/index.ts
22
cli/index.ts
@ -6,8 +6,6 @@ import { selectMenuInteractive } from "./selectMenu.ts";
|
||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||
import { cliAlert, cliLog } from "./prompts.ts";
|
||||
import type { ITool } from "../types.ts";
|
||||
import { join, toFileUrl } from "@std/path";
|
||||
import { log } from "util/logfile.ts";
|
||||
|
||||
// Register tools here (filename, no extension)
|
||||
const toolRegistry: [string, Promise<{ default: ITool }>][] = [
|
||||
@ -23,6 +21,7 @@ export class PdfToolsCli {
|
||||
|
||||
private args = ArgParser.parse(Deno.args).setFlagDefs({
|
||||
help: ["-h", "--help"],
|
||||
banana: ["-b", "--banana"],
|
||||
});
|
||||
|
||||
async importTools() {
|
||||
@ -43,9 +42,7 @@ export class PdfToolsCli {
|
||||
|
||||
private async banana() {
|
||||
const asciiArt = await getAsciiArt("banana");
|
||||
const body = this.terminalLayout.getBlock("body");
|
||||
body.clearAll();
|
||||
cliLog(colorize(asciiArt, "yellow"), body);
|
||||
this.closeMessage = colorize(asciiArt, "yellow");
|
||||
}
|
||||
|
||||
private async help() {
|
||||
@ -64,6 +61,11 @@ export class PdfToolsCli {
|
||||
this.terminalLayout.register("title", titleBlock);
|
||||
const bodyBlock = new TerminalBlock();
|
||||
this.terminalLayout.register("body", bodyBlock);
|
||||
if (this.args.getFlag("banana")) {
|
||||
titleBlock.setFixedHeight(0);
|
||||
await this.banana();
|
||||
return;
|
||||
}
|
||||
if (this.args.getFlag("help") && !this.args.task) {
|
||||
await this.help();
|
||||
return;
|
||||
@ -75,12 +77,16 @@ export class PdfToolsCli {
|
||||
await this.runTool(toCase(task, "title"));
|
||||
}
|
||||
} finally {
|
||||
this.terminalLayout.clearAll();
|
||||
Deno.stdin.setRaw(false);
|
||||
if (this.closeMessage) console.log(this.closeMessage);
|
||||
this.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
private cleanup() {
|
||||
this.terminalLayout.clearAll();
|
||||
Deno.stdin.setRaw(false);
|
||||
if (this.closeMessage) console.log(this.closeMessage);
|
||||
}
|
||||
|
||||
private async toolMenu() {
|
||||
const tools = this.tools.keys().toArray();
|
||||
const bodyBlock = this.terminalLayout.getBlock("body");
|
||||
|
100
cli/prompts.ts
100
cli/prompts.ts
@ -1,4 +1,5 @@
|
||||
// deno-lint-disable-must-await-calls
|
||||
import { log } from "util/logfile.ts";
|
||||
import { Cursor } from "./cursor.ts";
|
||||
import { colorize } from "./style.ts";
|
||||
import { TerminalBlock, TerminalLayout } from "./TerminalLayout.ts";
|
||||
@ -9,6 +10,7 @@ export async function cliPrompt(
|
||||
): Promise<string> {
|
||||
const encoder = new TextEncoder();
|
||||
const input: string[] = [];
|
||||
let cursorPos = 0;
|
||||
|
||||
await Deno.stdin.setRaw(true);
|
||||
|
||||
@ -22,33 +24,84 @@ export async function cliPrompt(
|
||||
Deno.stdout.writeSync(encoder.encode(message + " "));
|
||||
}
|
||||
|
||||
const buf = new Uint8Array(1);
|
||||
const render = () => {
|
||||
const line = message + " " + input.join("");
|
||||
const moveTo = `\x1b[${message.length + 2 + cursorPos}G`;
|
||||
|
||||
if (block) {
|
||||
block.setPostRenderAction(function () {
|
||||
Deno.stdout.writeSync(
|
||||
encoder.encode(`\x1b[${this["lastRenderRow"]};1H`),
|
||||
);
|
||||
Deno.stdout.writeSync(encoder.encode(moveTo));
|
||||
});
|
||||
range = block.setLines([line], range);
|
||||
} else {
|
||||
Deno.stdout.writeSync(encoder.encode("\x1b[K" + line + moveTo));
|
||||
}
|
||||
};
|
||||
|
||||
render();
|
||||
|
||||
const buf = new Uint8Array(64); // large enough for most pastes
|
||||
inputLoop:
|
||||
while (true) {
|
||||
const n = await Deno.stdin.read(buf);
|
||||
if (n === null) break;
|
||||
const byte = buf[0];
|
||||
|
||||
if (byte === 3) { // Ctrl+C
|
||||
Deno.stdin.setRaw(false);
|
||||
block?.["layout"]?.clearAll();
|
||||
block?.clear();
|
||||
Deno.exit(130);
|
||||
for (let i = 0; i < n; i++) {
|
||||
const byte = buf[i];
|
||||
|
||||
// Ctrl+C
|
||||
if (byte === 3) {
|
||||
block?.clear();
|
||||
block?.["layout"]?.clearAll();
|
||||
await Deno.stdin.setRaw(false);
|
||||
Deno.exit(130);
|
||||
}
|
||||
|
||||
if (byte === 13) { // Enter
|
||||
break inputLoop;
|
||||
}
|
||||
|
||||
// Escape sequence?
|
||||
if (byte === 27 && i + 1 < n && buf[i + 1] === 91) {
|
||||
const third = buf[i + 2];
|
||||
if (third === 68 && cursorPos > 0) cursorPos--; // Left
|
||||
else if (third === 67 && cursorPos < input.length) cursorPos++; // Right
|
||||
else if (third === 51 && i + 3 < n && buf[i + 3] === 126) { // Delete
|
||||
if (cursorPos < input.length) input.splice(cursorPos, 1);
|
||||
i += 1; // consume tilde
|
||||
}
|
||||
i += 2; // consume ESC [ X
|
||||
continue;
|
||||
}
|
||||
|
||||
// Backspace
|
||||
if (byte === 127 || byte === 8) {
|
||||
if (cursorPos > 0) {
|
||||
input.splice(cursorPos - 1, 1);
|
||||
cursorPos--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Delete (ASCII 46)
|
||||
if (byte === 46 && cursorPos < input.length) {
|
||||
input.splice(cursorPos, 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Printable
|
||||
if (byte >= 32 && byte <= 126) {
|
||||
input.splice(cursorPos, 0, String.fromCharCode(byte));
|
||||
cursorPos++;
|
||||
}
|
||||
|
||||
// Other cases: ignore
|
||||
}
|
||||
|
||||
if (byte === 13) { // Enter
|
||||
break;
|
||||
} else if (byte === 127 || byte === 8) { // Backspace
|
||||
input.pop();
|
||||
} else if (byte >= 32 && byte <= 126) { // Printable chars
|
||||
input.push(String.fromCharCode(byte));
|
||||
}
|
||||
|
||||
const line = message + " " + input.join("");
|
||||
if (block) {
|
||||
range = block.setLines([line], range);
|
||||
} else {
|
||||
Deno.stdout.writeSync(encoder.encode("\r\x1b[K" + line));
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
await Deno.stdin.setRaw(false);
|
||||
@ -92,13 +145,18 @@ if (import.meta.main) {
|
||||
const layout = new TerminalLayout();
|
||||
const title = new TerminalBlock();
|
||||
const block = new TerminalBlock();
|
||||
const footer = new TerminalBlock();
|
||||
block.setPreserveHistory(true);
|
||||
// ScrollManager.enable(block);
|
||||
title.setLines(["Hello, World!"]);
|
||||
title.setFixedHeight(1);
|
||||
|
||||
footer.setLines(["Press Ctrl+C to exit"]);
|
||||
footer.setFixedHeight(1);
|
||||
|
||||
layout.register("title", title);
|
||||
layout.register("block", block);
|
||||
layout.register("footer", footer);
|
||||
|
||||
Deno.addSignalListener("SIGINT", () => {
|
||||
layout.clearAll();
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "@bearmetal/pdf-tools",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.7",
|
||||
"license": "GPL 3.0",
|
||||
"tasks": {
|
||||
"dev": "deno run -A --env-file=.env --watch main.ts",
|
||||
"compile": "deno compile -o compare-form-fields.exe --target x86_64-pc-windows-msvc -R ./main.ts",
|
||||
"dev": "deno run -A --env-file=.env main.ts",
|
||||
"compile": "deno compile -o pdf-tools.exe --target x86_64-pc-windows-msvc --include ./asciiart.txt -A ./main.ts",
|
||||
"install": "deno install -fgq --import-map ./deno.json -n checkfields -R ./main.ts",
|
||||
"debug": "deno run -A --env-file=.env --inspect-wait --watch main.ts"
|
||||
},
|
||||
@ -12,7 +12,8 @@
|
||||
"@std/assert": "jsr:@std/assert@1",
|
||||
"@std/path": "jsr:@std/path@^1.0.9",
|
||||
"pdf-lib": "npm:pdf-lib@^1.17.1",
|
||||
"util/": "./util/"
|
||||
"util/": "./util/",
|
||||
"@/": "./"
|
||||
},
|
||||
"exports": {
|
||||
".": "./main.ts"
|
||||
|
@ -7,6 +7,7 @@ import { colorize } from "../cli/style.ts";
|
||||
import { cliAlert, cliLog, cliPrompt } from "../cli/prompts.ts";
|
||||
import { multiSelectMenuInteractive } from "../cli/selectMenu.ts";
|
||||
import type { callback, ITool } from "../types.ts";
|
||||
import { toCase } from "util/caseManagement.ts";
|
||||
|
||||
async function renameFields(
|
||||
path: string,
|
||||
@ -69,13 +70,42 @@ function applyRename(
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* Evaluates the change string with the match array
|
||||
*
|
||||
* @description The change string can include the following variables:
|
||||
*
|
||||
* - $<int> - capture groups, indexed from 1
|
||||
* - $<int>i - capture groups, indexed from 1, transforming an integer to an index
|
||||
* - $<int>s - capture groups, indexed from 1, transforming a string to snake case
|
||||
* - $<int>c - capture groups, indexed from 1, transforming a string to camel case
|
||||
* - $<int>l - capture groups, indexed from 1, transforming a string to lower case
|
||||
* - $<int>u - capture groups, indexed from 1, transforming a string to upper case
|
||||
* - $<int>t - capture groups, indexed from 1, transforming a string to title case
|
||||
*/
|
||||
function evaluateChange(change: string, match: RegExpExecArray) {
|
||||
return change.replace(
|
||||
/\$(\d+)(i?)/g,
|
||||
(_, i, indexed) =>
|
||||
indexed
|
||||
? (parseInt(match[i]) ? (parseInt(match[i]) - 1).toString() : match[i])
|
||||
: match[i],
|
||||
/\$(\d+)([icslut]?)/g,
|
||||
(_, i, indexed) => {
|
||||
switch (indexed) {
|
||||
case "i":
|
||||
return (parseInt(match[i])
|
||||
? (parseInt(match[i]) - 1).toString()
|
||||
: match[i]);
|
||||
case "s":
|
||||
return toCase(match[i], "snake");
|
||||
case "c":
|
||||
return toCase(match[i], "camel");
|
||||
case "t":
|
||||
return toCase(match[i], "title");
|
||||
case "l":
|
||||
return match[i].toLowerCase();
|
||||
case "u":
|
||||
return match[i].toUpperCase();
|
||||
default:
|
||||
return match[i];
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@ -103,49 +133,63 @@ class RenameFields implements ITool {
|
||||
[pdfPath, pattern, change] = await forceArgs(
|
||||
[pdfPath, pattern, change],
|
||||
[
|
||||
["Please provide path to PDF:", (p) => !!p && p.endsWith(".pdf")],
|
||||
[
|
||||
"Please provide path to PDF (comma separated for multiple):",
|
||||
(p) => !!p && p.endsWith(".pdf"),
|
||||
],
|
||||
"Please provide search string:",
|
||||
"Please provide requested change:",
|
||||
],
|
||||
this.block,
|
||||
);
|
||||
|
||||
const patternRegex = new RegExp(pattern);
|
||||
const paths = pdfPath.split(",");
|
||||
|
||||
const pdf = await loadPdf(pdfPath);
|
||||
const form = pdf.getForm();
|
||||
const fields = form.getFields();
|
||||
for (const pdfPath of paths) {
|
||||
const patternRegex = new RegExp(pattern);
|
||||
|
||||
const foundUpdates: [string, callback][] = [];
|
||||
const pdf = await loadPdf(pdfPath);
|
||||
const form = pdf.getForm();
|
||||
const fields = form.getFields();
|
||||
|
||||
for (const field of fields) {
|
||||
const name = field.getName();
|
||||
const match = patternRegex.exec(name);
|
||||
if (match) {
|
||||
const toChange = evaluateChange(change, match);
|
||||
foundUpdates.push([
|
||||
`${colorize(name, "red")} -> ${colorize(toChange, "green")}`,
|
||||
() => {
|
||||
applyRename(field, name, patternRegex, toChange);
|
||||
},
|
||||
]);
|
||||
const foundUpdates: [string, callback][] = [];
|
||||
let changesMade = false;
|
||||
|
||||
for (const field of fields) {
|
||||
const name = field.getName();
|
||||
const match = patternRegex.exec(name);
|
||||
if (match) {
|
||||
const toChange = evaluateChange(change, match);
|
||||
const preview = name.replace(new RegExp(patternRegex), toChange);
|
||||
foundUpdates.push([
|
||||
`${colorize(name, "red")} -> ${colorize(preview, "green")}`,
|
||||
() => {
|
||||
applyRename(field, name, patternRegex, toChange);
|
||||
changesMade = true;
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (foundUpdates.length) {
|
||||
cliLog("Found updates:", this.block);
|
||||
await multiSelectMenuInteractive(
|
||||
"Please select an option to apply",
|
||||
foundUpdates,
|
||||
{ terminalBlock: this.block },
|
||||
);
|
||||
}
|
||||
|
||||
if (changesMade) {
|
||||
const path = await cliPrompt(
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
);
|
||||
await savePdf(pdf, path || pdfPath);
|
||||
} else {
|
||||
cliLog("No changes made, skipping", this.block);
|
||||
}
|
||||
}
|
||||
|
||||
if (foundUpdates.length) {
|
||||
cliLog("Found updates:", this.block);
|
||||
await multiSelectMenuInteractive(
|
||||
"Please select an option to apply",
|
||||
foundUpdates,
|
||||
{ terminalBlock: this.block },
|
||||
);
|
||||
}
|
||||
|
||||
const path = await cliPrompt(
|
||||
"Save to path (or hit enter to keep current):",
|
||||
this.block,
|
||||
);
|
||||
await savePdf(pdf, path || pdfPath);
|
||||
}
|
||||
}
|
||||
export default new RenameFields();
|
||||
|
@ -1,30 +1,31 @@
|
||||
export async function getAsciiArt(art: string) {
|
||||
const artFilePath = Deno.env.get("BEARMETAL_ASCII_PATH") ||
|
||||
getBearmetalAsciiPath();
|
||||
if (!artFilePath) return art;
|
||||
let artFileText: string;
|
||||
if (artFilePath.startsWith("http")) {
|
||||
artFileText = await fetch(artFilePath).then((res) => res.text());
|
||||
} else {
|
||||
artFileText = await Deno.readTextFile(artFilePath);
|
||||
}
|
||||
const parserRX = /begin\s+(\w+)\s*\n([\s\S]*?)\s*end\s*/g;
|
||||
let result = parserRX.exec(artFileText);
|
||||
import { log } from "./logfile.ts";
|
||||
import { join } from "@std/path";
|
||||
|
||||
while (result !== null) {
|
||||
const [_, name, artText] = result;
|
||||
if (name === art) return artText;
|
||||
result = parserRX.exec(artFileText);
|
||||
export async function getAsciiArt(art: string) {
|
||||
try {
|
||||
const artFilePath =
|
||||
Deno.env.get("BEARMETAL_ASCII_PATH") || import.meta.dirname
|
||||
? join(import.meta.dirname || "", "../asciiart.txt")
|
||||
: "https://git.cyborggrizzly.com/BearMetal/pdf-tools/raw/branch/main/asciiart.txt";
|
||||
let artFileText: string;
|
||||
if (artFilePath?.startsWith("http")) {
|
||||
artFileText = await fetch(artFilePath).then((res) => res.text());
|
||||
} else {
|
||||
artFileText = await Deno.readTextFile(
|
||||
artFilePath,
|
||||
);
|
||||
}
|
||||
const parserRX = /begin\s+(\w+)\s*\n([\s\S]*?)\s*end\s*/g;
|
||||
let result = parserRX.exec(artFileText);
|
||||
|
||||
while (result !== null) {
|
||||
const [_, name, artText] = result;
|
||||
if (name === art) return artText;
|
||||
result = parserRX.exec(artFileText);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
alert();
|
||||
}
|
||||
return art;
|
||||
}
|
||||
|
||||
function getBearmetalAsciiPath() {
|
||||
const filenameRX = /asciiarts?\.txt$/;
|
||||
for (const filename of Deno.readDirSync(".")) {
|
||||
if (filename.isFile && filenameRX.test(filename.name)) {
|
||||
return filename.name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -11,9 +11,45 @@ function lowerToTrainCase(str: string) {
|
||||
);
|
||||
}
|
||||
|
||||
function lowerToCamelCase(str: string) {
|
||||
return str.trim().replace(/(?:\s)\w/g, (match) => match.toUpperCase())
|
||||
.replaceAll(" ", "");
|
||||
/**
|
||||
* @param str
|
||||
* @returns camelCased string (single letter words are lower cased, e.g. SSN -> ssn)
|
||||
*/
|
||||
function lowerToCamelCase(str: string): string {
|
||||
const words = str.trim().split(/\s+/);
|
||||
const result: string[] = [];
|
||||
let i = 0;
|
||||
|
||||
while (i < words.length) {
|
||||
if (words[i].length === 1) {
|
||||
// We’ve hit the start of a chain of single-letter words
|
||||
let j = i;
|
||||
while (j < words.length && words[j].length === 1) {
|
||||
j++;
|
||||
}
|
||||
const chainIsAtStart = i === 0;
|
||||
// Process that entire chain
|
||||
for (let k = i; k < j; k++) {
|
||||
result[k] = chainIsAtStart
|
||||
? words[k].toLowerCase()
|
||||
: words[k].toUpperCase();
|
||||
}
|
||||
i = j;
|
||||
} else {
|
||||
// Normal multi-letter word
|
||||
if (i === 0) {
|
||||
// first word: all lower
|
||||
result[i] = words[i].toLowerCase();
|
||||
} else {
|
||||
// subsequent words: capitalize first letter
|
||||
result[i] = words[i][0].toUpperCase() +
|
||||
words[i].slice(1).toLowerCase();
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return result.join("");
|
||||
}
|
||||
|
||||
function lowerToSnakeCase(str: string) {
|
||||
@ -88,10 +124,10 @@ function coerceCaseToLower(str: string, caseType: CaseType) {
|
||||
case "macro":
|
||||
case "snake":
|
||||
case "upper":
|
||||
return str.replace("_", " ").toLowerCase();
|
||||
return str.replaceAll("_", " ").toLowerCase();
|
||||
case "train":
|
||||
case "kebab":
|
||||
return str.replace("-", " ").toLowerCase();
|
||||
return str.replaceAll("-", " ").toLowerCase();
|
||||
default:
|
||||
return str.toLowerCase();
|
||||
}
|
||||
@ -124,3 +160,7 @@ export function toCase(str: string, toCase: CaseType) {
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
console.log(toCase("SSN", "camel"));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user