@ideasonpurpose/build-tools-wordpress 2.1.3 → 2.1.5

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,19 @@ 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.4
8
+
9
+ > 16 April 2025
10
+
11
+ - use look-behind and look-forward to allow overlaps, simplify replacements
12
+ - fix README
13
+
14
+ #### v2.1.3
15
+
16
+ > 16 April 2025
17
+
18
+ - Use self-closing html tags as tokens. Closes #11
19
+
7
20
  #### v2.1.2
8
21
 
9
22
  > 14 April 2025
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ideasonpurpose/build-tools-wordpress
2
2
 
3
- #### Version 2.1.3
3
+ #### Version 2.1.5
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)
@@ -44,7 +44,7 @@ Running a simple watch script to re-install on changes will make things somewhat
44
44
  ```sh
45
45
  cd dev-project-working-dir
46
46
  npm chokidar-cli "../../build-tools-wordpress/**/*" -c "npm install"
47
-
47
+ ```
48
48
 
49
49
  ### Additional Notes
50
50
 
@@ -46,34 +46,61 @@ export function tokenizeHTML(htmlContent) {
46
46
  // const pattern = /<\?(?:php|=)[\s\S]*?\?>/gs;
47
47
  // const pattern =
48
48
  // /(?<before>(?:[^\s]|\s|^)\s*)(?<php><\?(?:php|=).*?(?:\?>|$))(?<after>(?:\s*)[^\s]|$)/gs;
49
+ // const pattern =
50
+ // /((?:[^\s]|\s|^)\s*)(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gms;
51
+ // // const pattern = /([^\s]+)\s*(<\?(?:php|=).*?(?:\?>|$))\s*([^\s]*)/gms;
52
+ // const pattern =
53
+ // /([^\s]?\s*)?(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gms;
49
54
  const pattern =
50
- /((?:[^\s]|\s|^)\s*)(<\?(?:php|=).*?(?:\?>|$))((?:\s*)[^\s]|$)/gs;
55
+ /(?<=((?:[^\s]|\s|^)\s*))(<\?(?:php|=).*?\?>)(?=((?:\s*)[^\s]|$))/gms;
51
56
 
52
- const tokenizedHTML = htmlContent.replace(
57
+ let tokenizedHTML = htmlContent.replace(
53
58
  pattern,
54
59
  (string, before, phpCodeBlock, after, offset) => {
55
60
  const start = [">", ""].includes(before.trim()) ? "<" : "_";
56
61
  const end = ["<", ""].includes(after.trim()) ? " />" : "___";
57
62
 
58
- console.log({offset, string, before, phpCodeBlock, after, offset });
63
+ // end-pad the token to the length of the span, up to 80 characters
64
+ const codeLength = Math.min(phpCodeBlock.length, 80 - end.length);
65
+ const token =
66
+ `${start}php_${tokenCount++}__`.padEnd(codeLength, "_") + end;
67
+ phpCodeBlocks[token] = phpCodeBlock;
68
+
69
+ return token;
70
+ },
71
+ );
72
+
73
+ /**
74
+ * special case followup for open-ended PHP tags at the end of the document
75
+ * TODO: Merge this back up into a single pattern
76
+ * Q. Is the 'm' flag breaking the meaning of ^ and $?
77
+ */
78
+ tokenizedHTML = tokenizedHTML.replace(
79
+ /(?<=((?:[^\s]|\s|^)\s*))(<\?(?:php|=).*$)/gms,
80
+
81
+ (string, before, phpCodeBlock, offset) => {
82
+ const start = [">", ""].includes(before.trim()) ? "<" : "_";
83
+ const end = start === "<" ? " />" : "___";
59
84
 
60
- // end-pad the token to the lengh of the span, up to 80 characters
61
85
  const codeLength = Math.min(phpCodeBlock.length, 80 - end.length);
62
86
  const token =
63
87
  `${start}php_${tokenCount++}__`.padEnd(codeLength, "_") + end;
64
88
  phpCodeBlocks[token] = phpCodeBlock;
65
- return `${before}${token}${after}`;
89
+
90
+ return token;
66
91
  },
67
92
  );
68
93
 
69
- console.log({ tokenizedHTML, phpCodeBlocks });
70
94
  return { tokenizedHTML, phpCodeBlocks };
71
95
  }
72
96
 
73
- export function unTokenizeHTML(htmlContent, tokens) {
74
- let phpContent = htmlContent;
75
- for (const token in tokens) {
76
- phpContent = phpContent.replace(new RegExp(token, "g"), tokens[token]);
97
+ export function unTokenizeHTML(tokenizedHTML, phpCodeBlocks) {
98
+ let phpContent = tokenizedHTML;
99
+ for (const token in phpCodeBlocks) {
100
+ phpContent = phpContent.replace(
101
+ new RegExp(token, "g"),
102
+ phpCodeBlocks[token].replace(/\$/g, "$$$$"),
103
+ );
77
104
  }
78
105
  return phpContent;
79
106
  }
@@ -54,7 +54,7 @@ export class AfterDoneReporterPlugin {
54
54
 
55
55
  setTimeout(() =>
56
56
  // console.log("\n" + this.config.prefix + " " + messages),
57
- console.log( "⚙️ " + messages),
57
+ console.log( "🚧 " + messages),
58
58
  );
59
59
 
60
60
  setTimeout(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ideasonpurpose/build-tools-wordpress",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
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,20 @@
1
+
2
+
3
+
4
+ <!-- START template-parts/items/card.php -->
5
+
6
+ <a
7
+ href="<?php the_permalink(); ?>"
8
+ <?php post_class('card col-12 col-md-6'); ?>
9
+ data-ga-action="Article"
10
+ >
11
+ <?php get_template_part('template-parts/partials/card-media'); ?>
12
+ <div class="card__details">
13
+ <h4 class="card__title"><?= $cardTitle ?></h4>
14
+ <h5 class="card__desc"><?= $cardDesc ?></h5>
15
+ </div>
16
+ </a>
17
+
18
+ <!-- END template-parts/items/card.php -->
19
+
20
+
@@ -0,0 +1,4 @@
1
+ <?php
2
+ $a = 'a $';
3
+ $b = 'b $&';
4
+ $c = '$0 $1 $2 $3';
@@ -4,7 +4,7 @@ import { describe, expect, test } from "vitest";
4
4
 
5
5
  import { readFile } from "node:fs/promises";
6
6
 
7
- import { tokenizeHTML } from "../bin/format-php-prettier.js";
7
+ import { tokenizeHTML, unTokenizeHTML } from "../bin/format-php-prettier.js";
8
8
 
9
9
  describe("HTML-PHP Prettier", () => {
10
10
  test("All tokens exist", async () => {
@@ -34,18 +34,55 @@ describe("HTML-PHP Prettier", () => {
34
34
  expect(tokens[3]).toMatch(/^<php_\d+_* \/>$/);
35
35
  });
36
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();
37
+ test("single open PHP code block #11", async () => {
38
+ const input = (
39
+ await readFile(
40
+ "./test/fixtures/format-php-prettier/single-open-php-block.php",
41
+ )
42
+ ).toString();
43
+
44
+ const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
45
+
46
+ const tokens = Object.keys(codeBlocks);
47
+
48
+ expect(tokens).toHaveLength(1);
49
+ });
50
+
51
+ test("bare attribute in tag", async () => {
52
+ const input = (
53
+ await readFile(
54
+ "./test/fixtures/format-php-prettier/card-attribute-bug.php",
55
+ )
56
+ ).toString();
57
+
58
+ const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
44
59
 
45
- const { phpCodeBlocks: codeBlocks } = tokenizeHTML(input);
60
+ const tokens = Object.keys(codeBlocks);
46
61
 
47
- const tokens = Object.keys(codeBlocks);
62
+ expect(tokens[0]).toMatch(/^_php_\d+_*$/);
63
+ expect(tokens[1]).toMatch(/^_php_\d+_*$/);
64
+ expect(tokens[2]).toMatch(/^<php_\d+_* \/>$/);
65
+ expect(tokens[3]).toMatch(/^<php_\d+_* \/>$/);
66
+ expect(tokens[4]).toMatch(/^<php_\d+_* \/>$/);
48
67
 
49
- expect(tokens).toHaveLength(1);
50
- };
68
+ expect(tokens).toHaveLength(5);
69
+ });
70
+
71
+ /**
72
+ * If code blocks contain JS capture-group replacement strings
73
+ * those strings will vanish from the output.
74
+ */
75
+ test("$ capture group references bug", async () => {
76
+ const input = (
77
+ await readFile("./test/fixtures/format-php-prettier/regex-string-bug.php")
78
+ ).toString();
79
+
80
+ const { tokenizedHTML, phpCodeBlocks } = tokenizeHTML(input);
81
+
82
+ const formattedContent = unTokenizeHTML(tokenizedHTML, phpCodeBlocks);
83
+
84
+ expect(formattedContent).toContain("'a $'");
85
+ expect(formattedContent).toContain("'b $&'");
86
+ expect(formattedContent).toContain("'$0 $1 $2 $3'");
87
+ });
51
88
  });