Login  Register

Re: Bold Source Code and Escaping

Posted by mojavelinux on Aug 20, 2013; 5:08am
URL: https://discuss.asciidoctor.org/Bold-Source-Code-and-Escaping-tp464p475.html

Rob,

Aha! I see that I missed the special case. The circumstances here require that we get into a little bit of yak shaving. You'll be quite surprised that it's very solvable.

I've come up with not one, but two solutions to the challenge. First, we'll finish the solution you started.

1. Double dollar escaping

You are correct to think that the double dollar will escape the second occurance of ** to allow the bold to stretch the whole line. In your case, though, the double dollar is not used because it's a macro and macros are not enabled on your pass:[] macro. Thus, simply enabling macros using pass:verbatim,quotes,macros[] will allow it to work (technically the verbatim isn't needed here since there's nothing to escape).

[source,java]
[subs="verbatim,macros"]
----
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            pass:verbatim,quotes,macros[**.antMatchers("/resources/$$**$$").permitAll()**]
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();
----

TIP: You can also use triple plus instead of double dollar, which is even more strict about escaping.

2. Use an empty attribute to interrupt the markup

Since the ** is closing your span of bold text, you can use an attribute to get in between the two asterisks and keep them apart during parsing:

[source,java]
[subs="verbatim,macros"]
----
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            pass:verbatim,quotes,attributes[**.antMatchers("/resources/\*{empty}*").permitAll()**]
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();
----

NOTE: You still need to escape the bold marks around {empty} or else it will bold the empty text.



Of course, now that I realize you are talking about escpaing formatting, and not turning on the formatting on a per-line basis, you can just skip the pass macro and enable quotes and macros on the whole block.

[source,java]
[subs="verbatim,quotes,macros"]
----
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            **.antMatchers("/resources/$$**$$").permitAll()**
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();
----

Btw, to save some typing and ensure consistency, I recommend making the substitution set an attribute on the document:

:markup-in-source: verbatim,quotes,macros

[source,java]
[subs="{markup-in-source}"]
----
...
----

Btw, a totally different solution to this problem (in the upcoming Asciidoctor 0.1.4) is to to introduce a Treeprocessor extension that goes through all source code blocks (or ones marked with a certain role) and look for special markup which indicates the line should be emphasized in some way. For instance, you could type it as:

[source,java]
----
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/resources/**").permitAll() // (*)
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll();
----

The Treeprocessor would examine the contents looking for the "// (*)" suffix and rewrite the line so that Asciidoctor will format it appropriately. This is probably more of a long-term approach to keep the docs simple. What's great about Asciidoctor is that hints like these are very possible...all with the aim of keeping docs as simple and clean as possible.

-Dan


On Mon, Aug 19, 2013 at 8:47 PM, rwinch [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Dan,

Thank you for your very informative response.

I may just be missing something, but I don't think this solves my initial use case (correct me if I am wrong). I would like to bold a line with two ** in it. For example the following bolds the first half of the line, but the bold ends as soon as it hits the ** within the code.

[source,java]
[subs="verbatim,macros"]
----
protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                pass:verbatim,quotes[**.antMatchers("/resources/**").permitAll()**]
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll();
----

I tried escaping the code's ** using $$, but I believe the verbatim gets in the way of $$ working. I also tried removing the verbatim from the macro and using $$ as shown below, but it is still not working as I would like:

[source,java]
[subs="verbatim,macros"]
----
protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                pass:quotes[**.antMatchers("/resources/$$**$$").permitAll()**]
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll();
----

 It may be something I am just missing, so if anyone can help me with this last piece I'd be extremely grateful.

Thanks again,
Rob Winch


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Bold-Source-Code-and-Escaping-tp464p472.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | http://google.com/profiles/dan.j.allen