Code Blocks

Joseph Crawford's Avatar

Joseph Crawford

26 Sep, 2013 02:06 PM

Hello,

Could you also add support for twig code blocks? I ask because I use a static site generator called PieCrust which is like Jekyll except for it's highlighting it uses twig variables as such

{% geshi 'css' %}

{% endgeshi %}

It does not currently support MMD or Discount formats.

  1. Support Staff 1 Posted by Brett on 26 Sep, 2013 02:09 PM

    Brett's Avatar

    You can use a custom processor to handle this. I do similar with my blog previews to handle special formatting:

    http://brettterpstra.com/2013/01/04/previewing-jekyll-posts-with-marked/

    Twig isn't a widely requested feature (you're the first), so don't hold out too much hope that it will find its way in as a feature in the near future. A little scripting will do the trick, though.

    -Brett

  2. 2 Posted by Joseph Crawford on 26 Sep, 2013 02:15 PM

    Joseph Crawford's Avatar

    Thanks I will look into the scripting to see how it can be accomplished :) Thanks for releasing version 2 much better than 1 :)

  3. 3 Posted by Joseph Crawford on 26 Sep, 2013 02:50 PM

    Joseph Crawford's Avatar

    Any possibility you could help with this, I am not too familiar with Ruby but this regex works in online testers when tested against the following code, however run as a preprocessor it is not doing anything.

    #!/usr/bin/env ruby
    require "rubygems"  
    
    content = STDIN.read
    
    parts = content.split(/^---\s*$/)  
    content = parts[2]
    
    # Process geshi tags
    content.gsub!(/\{% geshi '(.*?)' (.*?) class='(.*?)' %\}(.*)\{% verbatim %\}(.*?)\{% endverbatim %\}(.*)\{% endgeshi %\}/m) {|q|  
      codeblock = $5
      if codeblock =~ /\{"\s*(.+?)\s*"\}/m
        "<pre><code class=\"\\1 \\3\">#{codeblock}</code>
    </pre>"
      else
        codeblock
      end
    }
    
    puts #{content}
    
    This will be the default page layout for your site so we will name it ```default.html```.  Go ahead and create this file under  
    ```_content/templates/```.  For now just make it look like the following:
    
    {% geshi 'twig' line_numbers class='codeblock' %}
      {% verbatim %}
        <html>
          <head>
            <title>Your Site Title</title>
          </head>
          <body>
            {{ content|raw }}
          </body>
        </html>
      {% endverbatim %}
    {% endgeshi %}
    Now if you run ```chef serve``` you will still see the page load with the same content but the styles are no longer making it look 'pretty'.
    
  4. Support Staff 4 Posted by Brett on 26 Sep, 2013 03:20 PM

    Brett's Avatar

    Do you have YAML headers in your documents? Lines 6 and 7 are looking for two --- lines and removing them. If you don't have any, it's getting an error when it tries to pick from the array. If you remove that section, you might have better luck. Let me know if that helps.

  5. 5 Posted by Joseph Crawford on 26 Sep, 2013 03:21 PM

    Joseph Crawford's Avatar

    Hello Brett,

    Yes the file does in-fact have a YAML header.

  6. 6 Posted by Joseph Crawford on 26 Sep, 2013 03:22 PM

    Joseph Crawford's Avatar

    Oh and verbatim is there in this case but it's not always present.

  7. Support Staff 7 Posted by Brett on 26 Sep, 2013 03:26 PM

    Brett's Avatar

    Can you upload a complete document (including headers) for me to test?

  8. Support Staff 8 Posted by Brett on 26 Sep, 2013 03:30 PM

    Brett's Avatar

    Oh, I see. You're using interpolation outside of a string in the last line. Change it to just:

     puts content
    

    The regex needs some tweaking with more optionals if things like verbatim won't always be there, but making that change gets it to work with your sample code.

  9. 9 Posted by Joseph Crawford on 26 Sep, 2013 03:31 PM

    Joseph Crawford's Avatar

    Sure I have attached a page from my site

  10. Support Staff 10 Posted by Brett on 26 Sep, 2013 03:56 PM

    Brett's Avatar

    See if you can work with this: http://ckyp.us/UAY2

    You have an error in your syntax in the example post, line 82 has a double endgeshi and no endverbatim. With that fixed the above script is working for me, at least for the code block portion.

    Are you using Marked 1 or 2?

  11. 11 Posted by Joseph Crawford on 26 Sep, 2013 04:13 PM

    Joseph Crawford's Avatar

    Brett,

    That does not appear to work for me: https://www.dropbox.com/s/hr8wvnjzqetqyr9/Screenshot%202013-09-26%2012.12.07.png

    I modified my preprocessor file, then saved it and modified the md file and saved it, the preview did not update.

  12. 12 Posted by Joseph Crawford on 26 Sep, 2013 04:37 PM

    Joseph Crawford's Avatar

    I am using Marked 2 just bought it this morning when I saw the PRMac hit my inbox :)

  13. Support Staff 13 Posted by Brett on 26 Sep, 2013 05:33 PM

    Brett's Avatar

    what do you get if you run:

     cat yourdocument.md | yourprocessor.rb
    

    on the command line?

  14. 14 Posted by Joseph Crawford on 26 Sep, 2013 05:41 PM

    Joseph Crawford's Avatar

    Hello Brett,

    I had a permission issue as the file was not chmod 755. I fixed that and on the console I get the document html output with the geshi stuff replaced.
    However in Marked2 I still get the geshi blocks in the preview which is odd…

    Thanks,
    Joseph Crawford

  15. Support Staff 15 Posted by Brett on 26 Sep, 2013 05:45 PM

    Brett's Avatar

    Just for giggles, chmod it to 777 and see what it does.

    -Brett

  16. 16 Posted by Joseph Crawford on 26 Sep, 2013 05:50 PM

    Joseph Crawford's Avatar

    Brett,

    chmod to 777 did not help the issue. What I did find is that nothing was changing so I quit Marked2 and then re-opened it.
    Now it shows a blank white page for the preview of the document. If I go to settings and remove the pre-processor the content comes back.
    I don't think it actually loaded the processor until i restarted the app. However now I am left with a blank white preview.

    Thanks,
    Joseph Crawford

  17. Support Staff 17 Posted by Brett on 26 Sep, 2013 06:56 PM

    Brett's Avatar

    I'll need to test this all locally to figure out what's going on. Custom processors can be finicky. Can you post:

    - the script you're currently using as the processor
    - a screenshot of your Behavior settings
    - the markdown file you're testing with

    I'll take a look.

    -Brett

  18. 18 Posted by Joseph Crawford on 26 Sep, 2013 07:19 PM

    Joseph Crawford's Avatar

    I posted the files, but they are not showing up. Please let me know if they end up posting or not.

  19. 19 Posted by Joseph Crawford on 26 Sep, 2013 07:20 PM

    Joseph Crawford's Avatar

    Files

  20. Support Staff 20 Posted by Brett on 26 Sep, 2013 07:48 PM

    Brett's Avatar

    If you set your settings like this: http://ckyp.us/7u77

    And use the attached script, you should be able to get going with it. Let me know.

  21. 21 Posted by Joseph Crawford on 26 Sep, 2013 08:07 PM

    Joseph Crawford's Avatar

    Brett,

    That's working except for instances which contain YAML such as this

    {% geshi 'markdown' line_numbers class='codeblock' %}
    ---
    title: Index
    date: 2013-08-12 18:36
    ---
    A new page.
    {% endgeshi %}

    It actually kinda messes with the mark-up. If you look at the sample document I provided this appears on line 48
    however if you check the preview it appears the Marked is chopping out lines 48-72 from the resulting document.

    For these blocks I wish to keep the entire body in place without stripping the yaml from there, etc.

    Thanks,
    Joseph Crawford

  22. Support Staff 22 Posted by Brett on 26 Sep, 2013 08:26 PM

    Brett's Avatar

    It's not Marked doing the chopping, it's the script. Still needs some refining.

    -Brett

  23. 23 Posted by Joseph Crawford on 26 Sep, 2013 10:15 PM

    Joseph Crawford's Avatar

    I've got the code fixed so that it will do the replacements and it works on the command line but in Marked they all still show as twig blocks.

  24. 24 Posted by Joseph Crawford on 29 Sep, 2013 04:42 AM

    Joseph Crawford's Avatar

    Any ideas why it would just load a blank white screen? It's working just fine n the console it replaces everything properly, but in Marked it is not showing a preview at all. Is there a log I can check to see what is going on?

    Thanks,
    Joseph Crawford

  25. 25 Posted by Joseph Crawford on 29 Sep, 2013 05:19 AM

    Joseph Crawford's Avatar

    Brett,

    As you can see it's working just fine here: http://ideone.com/AVUOve

    however it does not appear that Marked likes it, for some reason it's still showing the template tags

    Thanks,
    Joseph Crawford

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac