@ideasonpurpose/build-tools-wordpress 2.1.2 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ #### v2.1.2
8
+
9
+ > 14 April 2025
10
+
11
+ - bump deps
12
+ - experimental html & php formatter
13
+
7
14
  #### v2.1.1
8
15
 
9
16
  > 9 April 2025
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ideasonpurpose/build-tools-wordpress
2
2
 
3
- #### Version 2.1.2
3
+ #### Version 2.1.3
4
4
 
5
5
  [![NPM Version](https://img.shields.io/npm/v/%40ideasonpurpose%2Fbuild-tools-wordpress?logo=npm)](https://www.npmjs.com/package/@ideasonpurpose/build-tools-wordpress)
6
6
  [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/ideasonpurpose/build-tools-wordpress/npm-publish.yml?logo=github&logoColor=white)](https://github.com/ideasonpurpose/build-tools-wordpress#readme)
@@ -23,25 +23,54 @@ const phpOptions = prettierConfig.overrides.find(
23
23
  (o) => o.files === "*.php",
24
24
  )?.options;
25
25
 
26
- function tokenizeHTML(htmlContent) {
26
+ /**
27
+ * Replaces PHP Code Blocks with tokens, returns tokenized HTML and an object containing
28
+ * PHP Code Blocks
29
+ *
30
+ * For code blocks between tags (bounded by > & <) tokens will be self-closing HTML
31
+ * tags, similar to this: <php_14______ /> PHP Code blocks at the beginning and end of
32
+ * the file will be tokenized as self-closing if they preceed or follow an HTML tag.
33
+ *
34
+ * All other PHP Code Blocks are represented as HTML attribute-safe padded strings, up
35
+ * to 80 characters long.
36
+ *
37
+ * NOTE: Because Prettier's HTML formatter will always add a space before self-closing
38
+ * tags' closing slash, we just include the space in the token to prevent it from
39
+ * being mutilated by the HTML formatting step. Cleaner than adding a string.replace
40
+ * to unTokenizeHTML().
41
+ */
42
+ export function tokenizeHTML(htmlContent) {
27
43
  const phpCodeBlocks = {};
28
44
  let tokenCount = 0;
29
45
 
30
- // Tokens are end-padded to the length of the span, up to 80 characters
46
+ // const pattern = /<\?(?:php|=)[\s\S]*?\?>/gs;
47
+ // const pattern =
48
+ // /(?<before>(?:[^\s]|\s|^)\s*)(?<php><\?(?:php|=).*?(?:\?>|$))(?<after>(?:\s*)[^\s]|$)/gs;
49
+ const pattern =
50
+ /((?:[^\s]|\s|^)\s*)(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gs;
51
+
31
52
  const tokenizedHTML = htmlContent.replace(
32
- /<\?(?:php|=)[\s\S]*?\?>/gs,
33
- (phpCodeBlock) => {
34
- const codeLength = Math.min(phpCodeBlock.length, 80);
35
- const token = `__php_${tokenCount++}__`.padEnd(codeLength, "_");
53
+ pattern,
54
+ (string, before, phpCodeBlock, after, offset) => {
55
+ const start = [">", ""].includes(before.trim()) ? "<" : "_";
56
+ const end = ["<", ""].includes(after.trim()) ? " />" : "___";
57
+
58
+ console.log({offset, string, before, phpCodeBlock, after, offset });
59
+
60
+ // end-pad the token to the lengh of the span, up to 80 characters
61
+ const codeLength = Math.min(phpCodeBlock.length, 80 - end.length);
62
+ const token =
63
+ `${start}php_${tokenCount++}__`.padEnd(codeLength, "_") + end;
36
64
  phpCodeBlocks[token] = phpCodeBlock;
37
- return token;
65
+ return `${before}${token}${after}`;
38
66
  },
39
67
  );
40
68
 
69
+ console.log({ tokenizedHTML, phpCodeBlocks });
41
70
  return { tokenizedHTML, phpCodeBlocks };
42
71
  }
43
72
 
44
- function unTokenizeHTML(htmlContent, tokens) {
73
+ export function unTokenizeHTML(htmlContent, tokens) {
45
74
  let phpContent = htmlContent;
46
75
  for (const token in tokens) {
47
76
  phpContent = phpContent.replace(new RegExp(token, "g"), tokens[token]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ideasonpurpose/build-tools-wordpress",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "Build scripts and dependencies for IOP's WordPress development environments.",
5
5
  "homepage": "https://github.com/ideasonpurpose/build-tools-wordpress#readme",
6
6
  "bugs": {
@@ -0,0 +1,7 @@
1
+ <?php $a = 1; ?>
2
+
3
+ <div>
4
+ <a href="<?= $href ?>"> <?php echo $text; ?></a>
5
+ </div>
6
+
7
+ <?php $end = 1;
@@ -0,0 +1,51 @@
1
+ //@ts-check
2
+
3
+ import { describe, expect, test } from "vitest";
4
+
5
+ import { readFile } from "node:fs/promises";
6
+
7
+ import { tokenizeHTML } from "../bin/format-php-prettier.js";
8
+
9
+ describe("HTML-PHP Prettier", () => {
10
+ test("All tokens exist", async () => {
11
+ const input = (
12
+ await readFile("./test/fixtures/format-php-prettier/basic-html.php")
13
+ ).toString();
14
+
15
+ const { tokenizedHTML, phpCodeBlocks } = tokenizeHTML(input);
16
+
17
+ Object.keys(phpCodeBlocks).forEach((token) =>
18
+ expect(tokenizedHTML.includes(token)).toBe(true),
19
+ );
20
+ });
21
+
22
+ test("make tokens self-closing tags", async () => {
23
+ const input = (
24
+ await readFile("./test/fixtures/format-php-prettier/basic-html.php")
25
+ ).toString();
26
+
27
+ const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
28
+
29
+ const tokens = Object.keys(codeBlocks);
30
+
31
+ expect(tokens[0]).toMatch(/^<php_\d+_* \/>$/);
32
+ expect(tokens[1]).toMatch(/^_php_\d+_*$/);
33
+ expect(tokens[2]).toMatch(/^<php_\d+_* \/>$/);
34
+ expect(tokens[3]).toMatch(/^<php_\d+_* \/>$/);
35
+ });
36
+
37
+ test("single open PHP code block #11"),
38
+ async () => {
39
+ const input = (
40
+ await readFile(
41
+ "./test/fixtures/format-php-prettier/single-open-php-block.php",
42
+ )
43
+ ).toString();
44
+
45
+ const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
46
+
47
+ const tokens = Object.keys(codeBlocks);
48
+
49
+ expect(tokens).toHaveLength(1);
50
+ };
51
+ });