readFile combined with replaceRE can't seem to capture/remove wanted text

I have been referencing the re2 wiki but with very little luck. I have the following in a file:

/*@@

This is my *comment* right here. 

@@*/

console.log("A whole bunch of javascript code down here");

The goal is to use a combination of replaceRE and readFile to break the content between /*@@ and @@*/ then use markdownify and then to separate the code below the comment so that I can write it to the page in a code block.

This is as far as I’ve gotten in terms of getting just the code, but it’s still writing everything to the page.

{{ $jsfile := "assets/scripts/js/my-js-module.js" }}
{{ $jsmod := readFile $jsfile | replaceRE "(?m)(\\Q/*@@\\E.*\\Q/@@*/\\E)" "" }}
<div class="js-code">
<pre>
<code>
{{ $jsmod }}
</code>    
</pre>    
</div>

I would also love any insight on how to get just the markdown/text between the comment delimiters (/*@@ and @@*/), but I think I can get this using "$1" as soon as I figure out this first problem. I feel like @moorereason has always been my guru for this type of thing. Thanks much to the community ahead of time.

How about:

{{ $jsfile := "foo.js" }}
{{ $comment_begin_regexp := `/\*@@` }}
{{ $comment_end_regexp := `@@\*/` }}
{{ $char_or_newline_maybe_regexp := `(?:.|\n)*` }}
{{ $comment_regexp := printf `(%s(%s)%s)(%s)` $comment_begin_regexp $char_or_newline_maybe_regexp $comment_end_regexp $char_or_newline_maybe_regexp }}
{{ $comment := readFile $jsfile | replaceRE $comment_regexp "$2" }}
{{ $jsmod := readFile $jsfile | replaceRE $comment_regexp "$3" | replaceRE `^\s*` "" }}
<div class="js-code">
    <div class="caption">
        {{ $comment | markdownify }}
    </div>
    <pre><code>{{ $jsmod }}</code></pre>
</div>

HTML output

1 Like

Another iteration to get all the colors using Chroma :slight_smile:

Template

{{ $jsfile := "foo.js" }}
{{ $comment_begin_regexp := `/\*@@` }}
{{ $comment_end_regexp := `@@\*/` }}
{{ $char_or_newline_maybe_regexp := `(?:.|\n)*` }}
{{ $comment_regexp := printf `(%s(%s)%s)*(%s)` $comment_begin_regexp $char_or_newline_maybe_regexp $comment_end_regexp $char_or_newline_maybe_regexp }}
{{ $comment := readFile $jsfile | replaceRE $comment_regexp "$2" }}
{{ $jscode := readFile $jsfile | replaceRE $comment_regexp "$3" | replaceRE `^\s*` "" }}
<div class="js-code">
    <div class="caption">
        {{ $comment | markdownify }}
    </div>
    {{ printf "```js\n%s\n```" $jscode | markdownify }}
</div>

Test <hugo base dir>/foo.js

/*@@


This is my *comment* right here.

@@*/

console.log("A whole bunch of javascript code down here");

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
    fruits.sort();
    fruits.reverse();
    document.getElementById("demo").innerHTML = fruits;
}

Output


Update (2018/08/01): Make the comment parsing regexp optional.

1 Like

Heck yeah! Thanks @kaushalmodi! I feel like this could be a little more terse, but it’s working…and well! I’ll take it. Thanks so much!

I code for my future self.

I don’t want to be lost decoding the cryptic regular expressions when I need to debug or refactor those. So I simply broke up the modular and could-be-configurable regexp into self-explanatory vars. Tomorrow if you want to change the ending comment delimiter to @@_iamcool\*/, you know exactly what part to change :slight_smile:

1 Like

Agreed. That wasn’t a negative criticism. This is awesome! Thank you again!

1 Like

D’oh! Again, thanks @kaushalmodi for the great help on this, but as it turns out, if there isn’t that comment block, it takes the next available string. So if it’s just…

console.log("A whole bunch of javascript code down here");

It will actually writes that text to the page (i.e. assigns console.log... to {{$comment}}). Any thoughts?

Just make the comment regexp portion optional (*)…

Before

{{ $comment_regexp := printf `(%s(%s)%s)(%s)` $comment_begin_regexp $char_or_newline_maybe_regexp $comment_end_regexp $char_or_newline_maybe_regexp }}

After

{{ $comment_regexp := printf `(%s(%s)%s)*(%s)` $comment_begin_regexp $char_or_newline_maybe_regexp $comment_end_regexp $char_or_newline_maybe_regexp }}
1 Like

This is a testament to the sheer awesome sauce of Hugo. I got a full-fledged solution/response for a complicated question within 10 minutes :smile:

Thanks again, @kaushalmodi!

1 Like