@penskemediacorp/twig-to-php-parser 1.10.0-alpha.0 → 1.26.0

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.
Files changed (35) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -4
  3. package/__tests__/fixtures/larva.config.js +7 -0
  4. package/__tests__/fixtures/src/patterns/04-components/c-byline/c-byline.twig +1 -1
  5. package/__tests__/fixtures/src/patterns/04-components/c-heading/c-heading.twig +0 -1
  6. package/__tests__/fixtures/src/patterns/04-components/c-logo/c-logo.twig +3 -0
  7. package/__tests__/fixtures/src/patterns/08-modules/array-key-in-loop/array-key-in-loop.twig +11 -0
  8. package/__tests__/fixtures/src/patterns/08-modules/escaped-output-in-loop/escaped-output-in-loop.twig +5 -0
  9. package/__tests__/fixtures/src/patterns/08-modules/html-tag-interpolation/html-tag-interpolation.twig +7 -0
  10. package/__tests__/fixtures/src/patterns/08-modules/trending/trending.twig +1 -0
  11. package/__tests__/fixtures/src/patterns/99-objects/o-nav/o-nav.twig +4 -0
  12. package/__tests__/fixtures/template-parts/.gitkeep +1 -0
  13. package/__tests__/fixtures/template-parts-expected/patterns/components/c-button.php +7 -1
  14. package/__tests__/fixtures/template-parts-expected/patterns/components/c-byline.php +7 -1
  15. package/__tests__/fixtures/template-parts-expected/patterns/components/c-heading.php +7 -2
  16. package/__tests__/fixtures/template-parts-expected/patterns/components/c-logo.php +12 -0
  17. package/__tests__/fixtures/template-parts-expected/patterns/components/c-nav-link.php +8 -2
  18. package/__tests__/fixtures/template-parts-expected/patterns/modules/array-key-in-loop.php +20 -0
  19. package/__tests__/fixtures/template-parts-expected/patterns/modules/breadcrumbs.php +8 -2
  20. package/__tests__/fixtures/template-parts-expected/patterns/modules/escaped-output-in-loop.php +5 -0
  21. package/__tests__/fixtures/template-parts-expected/patterns/modules/html-tag-interpolation.php +16 -0
  22. package/__tests__/fixtures/template-parts-expected/patterns/modules/trending.php +10 -3
  23. package/__tests__/fixtures/template-parts-expected/patterns/objects/o-crap.php +7 -1
  24. package/__tests__/fixtures/template-parts-expected/patterns/objects/o-nav.php +12 -2
  25. package/__tests__/fixtures/template-parts-expected/patterns/objects/o-story-list.php +8 -2
  26. package/__tests__/parser-methods.test.js +107 -32
  27. package/__tests__/twig-to-php-parser.test.js +44 -28
  28. package/index.js +140 -49
  29. package/lib/getConfig.js +48 -0
  30. package/lib/twig-to-php-parser.php +218 -61
  31. package/package.json +3 -3
  32. package/.DS_Store +0 -0
  33. package/__tests__/.DS_Store +0 -0
  34. package/__tests__/fixtures/src/patterns/.DS_Store +0 -0
  35. package/__tests__/fixtures/template-parts/some-template.php +0 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Penske Media Corporation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -20,7 +20,7 @@ This package is intended to work with Larva Patterns to both parse patterns into
20
20
 
21
21
  ~When parsing includes, the parser looks for the `@larva` namespace, and will point includes to `PMC_CORE_PATH` when it finds it. Otherwise, includes will point to CHILD_THEME_PATH.~ This is not in effect yet. Everything is contained to the child theme.
22
22
 
23
- The parser supports a *very limited amount of Twig* and relies on keywords in data names in order to parse escaping correctly. Please refer to the Confluence documentation for Twig authoring guidelies: https://confluence.pmcdev.io/x/JoGJAg
23
+ The parser supports a *very limited amount of Twig* and relies on keywords in data names in order to parse escaping correctly. Please refer to the Confluence documentation for Twig authoring guidelines: https://confluence.pmcdev.io/x/JoGJAg
24
24
 
25
25
  ## Usage
26
26
 
@@ -30,13 +30,60 @@ module.exports = {
30
30
  // Other config
31
31
 
32
32
  parser: {
33
- relativeSrcOverride: false, // The default will look in src/patterns for the source path, but if you want to parse from a different directory, you can add that path here.
34
- isCore: false // Or true, if it is the core theme and should parse everything to PMC_CORE_PATH
35
33
  }
36
34
  }
37
35
  ```
38
36
 
37
+ The parser object supports the following configuration:
38
+ ```language-js
39
+ parser: {
40
+ twigDir: path.resolve( './new-path/to-twig/' ), // Default /theme/assets/src/patterns.
41
+ phpDir: path.resolve( './new-path/to-php/' ), // Default /theme/template-parts/patterns.
42
+ isUsingPlugin: true, // Default false.
43
+ }
44
+ ```
45
+
39
46
  Then, run the script from the assets directory by executing the package binary like so:
40
47
  ```
41
- $ node_modules/.bin/twig-to-php-parser
48
+ ./node_modules/.bin/twig-to-php-parser
49
+ ```
50
+
51
+ Supported wp action in twig markup:
52
+ ```language-twig
53
+ {{ wp_action( 'action_name', arg1, arg2, ... ) }}
54
+ ```
55
+ would translate to
56
+ ```language-php
57
+ <?php do_action( 'action_name', $arg1, $arg2, ... ); ?>
42
58
  ```
59
+
60
+ ## How To
61
+ TODO: Standardize and improve code structure for future improvement
62
+
63
+ ### Add a new function
64
+
65
+ - New function add must follow twig function syntax: function_name( p1, p2, ... )
66
+ - The new `function_name` must be added to larva/lib/server.js to support twig linting
67
+
68
+ // Add custom function support for doing wp action: {{ function_name( ... ) }}
69
+ twing.addFunction(new TwingFunction('function_name',() => {
70
+ return Promise.resolve(''); // must return a promise result
71
+ }));
72
+
73
+ - Add a new code block function/class to translate twig markup into php code block to twig-to-php-parse.php
74
+ - Code block function should take a twig markup string as input
75
+ - Return the translated php code block
76
+
77
+ function parse_my_new_function( $twig_markup ) {
78
+ // Code to parse and translate $twig_markup
79
+ return $twig_patterns;
80
+ }
81
+
82
+ - Modify function twig_to_php_parser and call to the new parser function to process $twig_markup_replace_main variable
83
+
84
+ $twig_markup_replace_main = parse_my_new_function( $twig_markup_replace_main )
85
+
86
+ - Add unit test
87
+ - Add js function to index.js to load the php code
88
+ - Add unit test to test the new parser function
89
+
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ parser: {
3
+ twigDir: `${ __dirname }/src/patterns/`,
4
+ phpDir: `${ __dirname }/template-parts/patterns/`,
5
+ isUsingPlugin: true,
6
+ },
7
+ };
@@ -1,5 +1,5 @@
1
1
  <p class="c-byline {{ modifier_class }} {{ c_byline_classes }}">
2
2
  {{ c_byline_by_text }}
3
3
 
4
- {{ c_byline_markup }}
4
+ {{ c_byline_markup|raw }}
5
5
  </p>
@@ -1,2 +1 @@
1
1
  <h2 class="c-heading {{ modifier_class }}">{{ c_heading_text }}</h2>
2
- {% include "../modules/trending.twig" with item %}
@@ -0,0 +1,3 @@
1
+ <div>
2
+ {% include '/assets/build/svg/' ~ c_logo_svg ~ '.svg' %}
3
+ </div>
@@ -0,0 +1,11 @@
1
+ {% for item in list_items %}
2
+ <li class="list__item larva // {{ list_item_classes }}">
3
+ {% if item['list_items'] %}
4
+ {% include "@larva/modules/list/list.twig" with item %}
5
+ {% else %}
6
+ <div class="{{ item['list_item_classes'] }}">
7
+ {{ item['list_item_markup'] }}
8
+ </div>
9
+ {% endif %}
10
+ </li>
11
+ {% endfor %}
@@ -0,0 +1,5 @@
1
+ {% for item in slides %}
2
+ <div>
3
+ {{ item|markup }}
4
+ </div>
5
+ {% endfor %}
@@ -0,0 +1,7 @@
1
+ <{{ list_type_name }}l class="list larva // {{ list_classes }}">
2
+ {% for item in list_items %}
3
+ <li class="list__item larva // {{ list_item_classes }}">
4
+ {% include "@larva/modules/list/list.twig" with item %}
5
+ </li>
6
+ {% endfor %}
7
+ </{{ list_type_name }}l>
@@ -5,4 +5,5 @@
5
5
  <div class="pmc-u-margin-b-0 u-border-bottom-2">
6
6
  {% include "../../05-objects/o-story-list/o-story-list.twig" with o_story_list %}
7
7
  </div>
8
+ {{ trending_ads_wp_action }}
8
9
  </section>
@@ -8,6 +8,10 @@
8
8
  {% for item in o_nav_list_items %}
9
9
  <li class="o-nav__list-item {{ o_nav_list_item_classes }}" data-dropdown-list-item="{{ o_nav_list_item_data_attributes }}">
10
10
  {% include "../../04-components/c-nav-link/c-nav-link.twig" with item %}
11
+
12
+ {% if item['newsletter_dropdown'] %}
13
+ {% include "@project/modules/newsletter-dropdown/newsletter-dropdown.twig" with item['newsletter_dropdown'] %}
14
+ {% endif %}
11
15
  </li>
12
16
  {% endfor %}
13
17
  </ul>
@@ -0,0 +1 @@
1
+ Directory is required for tests.
@@ -1,6 +1,12 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <a class="c-button <?php echo esc_attr( $modifier_class ?? '' ); ?>" href="<?php echo esc_url( $c_button_url ?? '' ); ?>">
5
11
  <?php echo esc_html( $c_button_text ?? '' ); ?>
6
12
  </a>
@@ -1,6 +1,12 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <p class="c-byline <?php echo esc_attr( $modifier_class ?? '' ); ?> <?php echo esc_attr( $c_byline_classes ?? '' ); ?>">
5
11
  <?php echo esc_html( $c_byline_by_text ?? '' ); ?>
6
12
 
@@ -1,5 +1,10 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <h2 class="c-heading <?php echo esc_attr( $modifier_class ?? '' ); ?>"><?php echo esc_html( $c_heading_text ?? '' ); ?></h2>
5
- <?php \PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/modules/trending.php', $item, true ); ?>
@@ -0,0 +1,12 @@
1
+ <?php
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
8
+ ?>
9
+
10
+ <div>
11
+ <?php \PMC::render_template( \PMC\Larva\Config::get_instance()->get( 'brand_directory' ) . '/build/svg/' . ( $c_logo_svg ?? '' ) . '.svg', [], true ); ?>
12
+ </div>
@@ -1,6 +1,12 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <a class="c-nav-link <?php echo esc_attr( $c_nav_link_classes ?? '' ); ?>" href="<?php echo esc_url( $c_nav_link_url ?? '' ); ?>">
5
11
  <?php echo esc_html( $c_nav_link_text ?? '' ); ?>
6
- </a>
12
+ </a>
@@ -0,0 +1,20 @@
1
+ <?php
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
8
+ ?>
9
+
10
+ <?php foreach ( $list_items ?? [] as $item ) { ?>
11
+ <li class="list__item larva // <?php echo esc_attr( $list_item_classes ?? '' ); ?>">
12
+ <?php if ( ! empty( $item['list_items'] ) ) { ?>
13
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'modules/list', $item, true ); ?>
14
+ <?php } else { ?>
15
+ <div class="<?php echo esc_attr( $item['list_item_classes'] ?? '' ); ?>">
16
+ <?php echo wp_kses_post( $item['list_item_markup'] ?? '' ); ?>
17
+ </div>
18
+ <?php } ?>
19
+ </li>
20
+ <?php } ?>
@@ -1,4 +1,10 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
4
- <?php \PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/objects/o-nav.php', $o_nav, true ); ?>
9
+
10
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'objects/o-nav', $o_nav, true ); ?>
@@ -0,0 +1,5 @@
1
+ <?php foreach ( $slides ?? [] as $item ) { ?>
2
+ <div>
3
+ <?php echo wp_kses_post( $item ?? '' ); ?>
4
+ </div>
5
+ <?php } ?>
@@ -0,0 +1,16 @@
1
+ <?php
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
8
+ ?>
9
+
10
+ <<?php echo esc_attr( $list_type_name ?? '' ); ?>l class="list larva // <?php echo esc_attr( $list_classes ?? '' ); ?>">
11
+ <?php foreach ( $list_items ?? [] as $item ) { ?>
12
+ <li class="list__item larva // <?php echo esc_attr( $list_item_classes ?? '' ); ?>">
13
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'modules/list', $item, true ); ?>
14
+ </li>
15
+ <?php } ?>
16
+ </<?php echo esc_attr( $list_type_name ?? '' ); ?>l>
@@ -1,11 +1,18 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <section class="u-max-width-300">
5
11
  <header>
6
- <?php \PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/components/c-heading.php', $c_heading, true ); ?>
12
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'components/c-heading', $c_heading, true ); ?>
7
13
  </header>
8
14
  <div class="pmc-u-margin-b-0 u-border-bottom-2">
9
- <?php \PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/objects/o-story-list.php', $o_story_list, true ); ?>
15
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'objects/o-story-list', $o_story_list, true ); ?>
10
16
  </div>
17
+ <?php do_action( 'trending_ads_wp_action' ); ?>
11
18
  </section>
@@ -1,4 +1,10 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <p class="o-crap <?php echo esc_attr( $modifier_class ?? '' ); ?> <?php echo esc_attr( $o_crap_classes ?? '' ); ?>"><?php echo esc_html( $o_crap_text ?? '' ); ?></p>
@@ -1,6 +1,12 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <nav class="o-nav <?php echo esc_attr( $modifier_class ?? '' ); ?> <?php echo esc_attr( $o_nav_classes ?? '' ); ?>" data-dropdown="<?php echo esc_attr( $o_nav_data_attributes ?? '' ); ?>">
5
11
 
6
12
  <?php if ( ! empty( $o_nav_title_text ) ) { ?>
@@ -10,7 +16,11 @@
10
16
  <ul class="o-nav__list <?php echo esc_attr( $o_nav_list_classes ?? '' ); ?>" data-dropdown-list="<?php echo esc_attr( $o_nav_list_data_attributes ?? '' ); ?>">
11
17
  <?php foreach ( $o_nav_list_items ?? [] as $item ) { ?>
12
18
  <li class="o-nav__list-item <?php echo esc_attr( $o_nav_list_item_classes ?? '' ); ?>" data-dropdown-list-item="<?php echo esc_attr( $o_nav_list_item_data_attributes ?? '' ); ?>">
13
- <?php \PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/components/c-nav-link.php', $item, true ); ?>
19
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'components/c-nav-link', $item, true ); ?>
20
+
21
+ <?php if ( ! empty( $item['newsletter_dropdown'] ) ) { ?>
22
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'modules/newsletter-dropdown', $item['newsletter_dropdown'], true ); ?>
23
+ <?php } ?>
14
24
  </li>
15
25
  <?php } ?>
16
26
  </ul>
@@ -1,10 +1,16 @@
1
1
  <?php
2
- // This is a generated file. Refer to the relevant Twig file for adjusting this markup.
2
+ /**
3
+ * Generated file.
4
+ *
5
+ * Refer to the relevant Twig file for adjusting this markup.
6
+ */
7
+
3
8
  ?>
9
+
4
10
  <ul class="o-story-list <?php echo esc_attr( $modifier_class ?? '' ); ?> <?php echo esc_attr( $o_story_list_classes ?? '' ); ?>">
5
11
  <?php foreach ( $o_story_list_teases ?? [] as $item ) { ?>
6
12
  <li class="o-story-list__item <?php echo esc_attr( $o_story_list_item_classes ?? '' ); ?>">
7
- <?php \PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/objects/o-tease.php', $item, true ); ?>
13
+ <?php \PMC\Larva\Pattern::get_instance()->render_pattern_template( 'objects/o-tease', $item, true ); ?>
8
14
  </li>
9
15
  <?php } ?>
10
16
  </ul>
@@ -1,43 +1,118 @@
1
1
  const assert = require( 'assert' );
2
- ( path = require( 'path' ) );
3
- ( fs = require( 'fs' ) );
4
2
 
5
3
  const parserMethods = require( '../index.js' ).methods;
6
- const exec = require( 'child_process' ).exec;
7
-
8
- const fixture = path.join( __dirname, 'fixtures' );
9
4
 
10
5
  const expectations = {
11
- childInclude: '<?php \\PMC::render_template( CHILD_THEME_PATH . \'/template-parts/patterns/objects/o-nav.php\', $o_nav, true ); ?>',
12
- larvaInclude: '<?php \\PMC::render_template( CHILD_THEME_PATH . \'/template-parts/patterns/objects/o-nav.php\', $o_nav, true ); ?>'
13
- // larvaInclude: '<?php \\PMC::render_template( PMC_CORE_PATH . \'/template-parts/patterns/objects/o-nav.php\', $o_nav, true ); ?>' - uncomment when core logic is reactivated
6
+ childInclude:
7
+ "<?php \\PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/objects/o-nav.php', $o_nav, true ); ?>",
8
+ larvaInclude:
9
+ "<?php \\PMC::render_template( PMC_CORE_PATH . '/template-parts/patterns/objects/o-nav.php', $o_nav, true ); ?>",
10
+ pluginEnabled:
11
+ "<?php \\PMC\\Larva\\Pattern::get_instance()->render_pattern_template( 'objects/o-nav', $o_nav, true ); ?>",
14
12
  };
15
13
 
16
- describe( 'parse include statements', function() {
17
-
14
+ describe( 'parse include statements', function () {
18
15
  it( 'twig includes are parsed to PMC::render_template calls', ( done ) => {
19
- parserMethods.parseIncludePath(
20
- '{% include "o-nav/o-nav.twig" with o_nav %}',
21
- 'o-nav',
22
- 'o_nav' )
23
- .catch( e => console.log( e ) )
24
- .then( ( result ) => {
25
- assert.equal( result, expectations.childInclude );
26
- done();
27
- });
28
- });
16
+ parserMethods
17
+ .parseIncludePath(
18
+ '{% include "o-nav/o-nav.twig" with o_nav %}',
19
+ 'o-nav',
20
+ 'o_nav'
21
+ )
22
+ .catch( ( e ) => console.log( e ) )
23
+ .then( ( result ) => {
24
+ assert.equal( result, expectations.childInclude );
25
+ done();
26
+ } );
27
+ } );
29
28
 
30
29
  it( 'if larva namespace, parsed path references the parent theme', ( done ) => {
31
- parserMethods.parseIncludePath(
32
- '{% include "@larva/objects/o-nav.twig" with o_nav %}',
33
- 'o-nav',
34
- 'o_nav' )
35
- .catch( e => console.log( e ) )
36
- .then( ( result ) => {
37
- assert.equal( result, expectations.larvaInclude );
38
- done();
39
- });
40
- });
41
-
42
- });
30
+ parserMethods
31
+ .parseIncludePath(
32
+ '{% include "@larva/objects/o-nav.twig" with o_nav %}',
33
+ 'o-nav',
34
+ 'o_nav'
35
+ )
36
+ .catch( ( e ) => console.log( e ) )
37
+ .then( ( result ) => {
38
+ assert.equal( result, expectations.larvaInclude );
39
+ done();
40
+ } );
41
+ } );
42
+
43
+ it( 'if plugin is enabled, use the render_pattern_template method from Larva', ( done ) => {
44
+ parserMethods
45
+ .parseIncludePath(
46
+ '{% include "@project/objects/o-nav.twig" with o_nav %}',
47
+ 'o-nav',
48
+ 'o_nav',
49
+ true
50
+ )
51
+ .catch( ( e ) => console.log( e ) )
52
+ .then( ( result ) => {
53
+ assert.equal( result, expectations.pluginEnabled );
54
+ done();
55
+ } );
56
+ } );
57
+ } );
58
+
59
+ describe( 'parse SVG includes', function () {
60
+ it( 'parses includes to brand_directory when using plugin', ( done ) => {
61
+ const expectedPluginEnabled =
62
+ "<?php \\PMC::render_template( \\PMC\\Larva\\Config::get_instance()->get( 'brand_directory' ) . '/build/svg/' . ( $c_logo_svg ?? '' ) . '.svg', [], true ); ?>";
63
+
64
+ parserMethods
65
+ .parseSvgPath(
66
+ "{% include '../../build/svg/' ~ c_logo_svg ~ '.svg' %}",
67
+ 'c_logo_svg',
68
+ true
69
+ )
70
+ .catch( ( e ) => console.log( e ) )
71
+ .then( ( result ) => {
72
+ assert.equal( result, expectedPluginEnabled );
73
+ done();
74
+ } );
75
+ } );
76
+ it( 'parses includes to CHILD_THEME_PATH when not using plugin', ( done ) => {
77
+ const expected =
78
+ "<?php \\PMC::render_template( CHILD_THEME_PATH . '/assets/build/svg/' . ( $c_logo_svg ?? '' ) . '.svg', [], true ); ?>";
79
+
80
+ parserMethods
81
+ .parseSvgPath(
82
+ "{% include '../../build/svg/' ~ c_logo_svg ~ '.svg' %}",
83
+ 'c_logo_svg'
84
+ )
85
+ .catch( ( e ) => console.log( e ) )
86
+ .then( ( result ) => {
87
+ assert.equal( result, expected );
88
+ done();
89
+ } );
90
+ } );
91
+ } );
92
+
93
+ describe( 'parse custom functions', function () {
94
+ it( 'parses wp_action function', ( done ) => {
95
+ parserMethods
96
+ .parseWpAction( "{{wp_action('test','param',variable)}}" )
97
+ .catch( ( e ) => console.log( e ) )
98
+ .then( ( result ) => {
99
+ assert.equal(
100
+ result,
101
+ "<?php do_action( 'test', 'param', $variable ); ?>"
102
+ );
103
+ done();
104
+ } );
43
105
 
106
+ // Test spacing
107
+ parserMethods
108
+ .parseWpAction( "{{ wp_action( 'test', 'param', variable ) }}" )
109
+ .catch( ( e ) => console.log( e ) )
110
+ .then( ( result ) => {
111
+ assert.equal(
112
+ result,
113
+ "<?php do_action( 'test', 'param', $variable ); ?>"
114
+ );
115
+ done();
116
+ } );
117
+ } );
118
+ } );
@@ -5,59 +5,75 @@ const twigToPhpParser = require( '../index' ).twigToPhpParser;
5
5
  const exec = require( 'child_process' ).exec;
6
6
 
7
7
  const fixture = path.join( __dirname, 'fixtures' );
8
+ const expectedDir = `${ fixture }/template-parts-expected/patterns/`;
8
9
 
9
- const pathConfig = {
10
- twigDir: `${fixture}/src/patterns/`,
11
- phpDir: `${fixture}/template-parts/patterns/`,
12
- expectedDir: `${fixture}/template-parts-expected/patterns/`,
13
- };
10
+ const getAppConfiguration = require( '../lib/getConfig' );
11
+ const appConfiguration = getAppConfiguration( 'parser' );
14
12
 
15
13
  const patternShortPaths = [
16
14
  'components/c-byline.php',
17
15
  'components/c-heading.php',
18
16
  'components/c-button.php',
17
+ 'components/c-logo.php',
19
18
  'objects/o-nav.php',
20
19
  'objects/o-story-list.php',
21
20
  'modules/trending.php',
21
+ 'modules/array-key-in-loop.php',
22
+ 'modules/html-tag-interpolation.php',
22
23
  ];
23
24
 
24
- describe( 'twig to php parser', function() {
25
-
25
+ describe( 'twig to php parser', function () {
26
26
  beforeEach( ( done ) => {
27
- exec( 'mkdir ' + pathConfig.phpDir, ( err ) => {
27
+ exec( 'mkdir ' + appConfiguration.phpDir, ( err ) => {
28
28
  if ( err ) {
29
29
  console.error( err );
30
30
  }
31
- });
31
+ } );
32
32
 
33
- return twigToPhpParser( pathConfig.twigDir, pathConfig.phpDir )
34
- .catch( ( e ) => console.log( e ) )
35
- .then( ( result ) => done() ); // Catch PHP errors.
36
- });
33
+ return twigToPhpParser( appConfiguration )
34
+ .catch( ( e ) => console.log( e ) )
35
+ .then( ( result ) => done() ); // Catch PHP errors.
36
+ } );
37
37
 
38
38
  it( 'creates new files and directories for objects, components, and modules', ( done ) => {
39
- assert.equal( fs.existsSync( pathConfig.phpDir + 'objects/o-nav.php' ), true );
40
- assert.equal( fs.existsSync( pathConfig.phpDir + 'components/c-nav-link.php' ), true );
41
- assert.equal( fs.existsSync( pathConfig.phpDir + 'modules/breadcrumbs.php' ), true );
39
+ assert.equal(
40
+ fs.existsSync( appConfiguration.phpDir + 'objects/o-nav.php' ),
41
+ true
42
+ );
43
+ assert.equal(
44
+ fs.existsSync(
45
+ appConfiguration.phpDir + 'components/c-nav-link.php'
46
+ ),
47
+ true
48
+ );
49
+ assert.equal(
50
+ fs.existsSync(
51
+ appConfiguration.phpDir + 'modules/breadcrumbs.php'
52
+ ),
53
+ true
54
+ );
42
55
  done();
43
- });
56
+ } );
44
57
 
45
58
  it( 'parses patterns as expected', ( done ) => {
46
59
  patternShortPaths.forEach( ( shortpath ) => {
47
- let expectedContents = fs.readFileSync( pathConfig.expectedDir + shortpath ).toString();
48
- let actualContents = fs.readFileSync( pathConfig.phpDir + shortpath ).toString();
49
-
50
- assert.equal( expectedContents, actualContents );
51
- });
60
+ const expectedContents = fs
61
+ .readFileSync( expectedDir + shortpath )
62
+ .toString();
63
+ const actualContents = fs
64
+ .readFileSync( appConfiguration.phpDir + shortpath )
65
+ .toString();
66
+
67
+ assert.equal( actualContents, expectedContents );
68
+ } );
52
69
  done();
53
- });
70
+ } );
54
71
 
55
72
  afterEach( () => {
56
- exec( 'rm -r ' + pathConfig.phpDir, ( err ) => {
73
+ exec( 'rm -r ' + appConfiguration.phpDir, ( err ) => {
57
74
  if ( err ) {
58
75
  console.error( err );
59
76
  }
60
- });
61
- });
62
-
63
- });
77
+ } );
78
+ } );
79
+ } );