Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot keep format source code in output file #7

Open
NeoHoang opened this issue Jan 13, 2022 · 2 comments
Open

Cannot keep format source code in output file #7

NeoHoang opened this issue Jan 13, 2022 · 2 comments

Comments

@NeoHoang
Copy link

Hello,

I've been making a cli script for convert Pug to Twig template.

This is my code

<?php

include 'vendor/autoload.php';

if( empty($argv[1]) ) exit('Missing file parameter.');

$file = $argv[1];

$filename = basename($file, ".pug");
$path = dirname($file);

$ouputfile = $path . DIRECTORY_SEPARATOR . $filename . ".htm";

#var_dump($filename, $path, $ouputfile);

$content = PugToTwig::convert(file_get_contents($file));

#var_dump($content);                         

if ( !empty($content) ) file_put_contents($ouputfile, $content);

exit();

But I cannot keep format for output file.

<ul id="users">{% for user in users %}{% endfor %}<li class="user">{#  comment #}{{ user.name | e }}Email: {{ user.email | e }}<a href="{{ user.url | e }}">Home page</a><span>aky</span></li></ul>

Can you help me to keep format syntax for twig output file ?

Thanks a lot
Neo

@kylekatarnls
Copy link
Member

Hello,

There is no "keeping format". There is no match, as Pug indent represent inheritence depth, while HTML/Twig indent can be either irrelevant or representing actual whitespace.

But you can use any option from Phug (https://phug-lang.com/#formatting) so you can format output:

$content = PugToTwig::convert(file_get_contents($file), [
    'indent_style' => ' ',
    'indent_width' => 4,
]);

With those settings each inner bloc is indented with 4 spaces.

The thing is that for HTML content, the {%...%} is just text like any other with no particular meaning, so it won't be indented. If you want it to be indented, you'll need to use a Twig formatter instead which could provide a more complete support.

@morgannunan
Copy link

I came here to post the same issue. I just added the indentation options from phug, and still my converted twig comes out in one long line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants