Do you like where Oracle is taking you? – jdk12 coming in 2021 (maybe)

Simon Ritter from Oracle outlined just recently the upcoming jdk 8 and the future directions that oracle wants to take the jdk. According to their plans, 2021 is going to be a great year, as the jdk12 will see the probably dim light of a smog-filled day: They certainly have plans for java. Focusing on the here and now, at least it will be likely that java 8 will finally include lambdas, which is something. But skip the corporate jadajada and just browse through the slides.

Filed under: Java — Tags: , ,

Roll out the RedCarpet: Macros for Markdown

Letting Users use WYSIWYG-Editors for writing content is usually a bad idea: We all start with good intentions, but sooner or later, this pseudo-html produces a great, ugly markup-mess (e.g. the living hell that Microsofts Word places on the Clipboard if you paste it into TinyMCE. That’s why we replaced all this nastiness with simpler editors for Markdown. Much cleaner, much better. But you can get fancier than that and try to extend the markdown-language with your own tags. Here is a simple example how this can be done.

Sometimes I envy the php/wordpress-guys: They are really clever when it comes to efficient content-production. For example, they have some simple macros to insert common pieces of content:

 [gallery]

which inserts a gallery of pictures attached to your post. I’m using Redcarpet, and I want that too: Let me show you how to implement a very simple macro that echos the content of a variable into the document. The macro will look like this:

 [echo my_var]

and shall print the contents of my_variable into the document.

Here is what we have to do to achieve this:

  • implement a custom Renderer that understands our macro
  • find the tag in the document and replace the macro with the content of the variable
  • replace all occurences of [echo my_var] with the content of my_var fetched from the hash

1) Write your own renderer:

There are several ways to extend Redcarpet. The usual html-Renderer(Redcarpet::Render::HTML) is stateless – it does not know anything about it’s environment. So we have to make the variable somehow available to our renderer. We therefore need to implement a custom renderer that preprocesses the Markdown before it gets passed on to the html-rendering:

module Redcarpet
  module Render
    class MacroRenderer < Redcarpet::Render::HTML
 
      def initialize(env_vars, options={})
        super options
        @env_vars = env_vars
      end
 
      def preprocess(full_document)
        full_document.gsub!(/\[echo (\w+)\]/) {@env_vars[($1).to_sym]}
        full_document
      end
 
    end
  end
end

2) Pass variables to the renderer and make the markdown-rendering available to the views:

I have a simple helper-method in ApplicationHelper that you can pass in a markdown-string and which returns the rendered html. Here is where you insert in your custom MacroRenderer:

 def md(mdown, env_vars={})
    unless mdown.nil?
      renderer = Redcarpet::Render::MacroRenderer.new(env_vars)
      markdown = Redcarpet::Markdown.new(renderer, Rails.application.config.markdown_options)
      markdown.render(mdown).html_safe
    else
      ""
    end
  end

If you make the env_vars optional, you can simply render markdown with md(“# Render me!”) – adding the env_vars to the render-call makes the renderer aware of its context.

3) Prepare your custom content and pass it to the renderer:

You can now add some custom content and pass it down to the renderer. Rendering a macro now works like this:

<pre lang=”ruby> md(“#Hello [echo my_var]“, :my_var => “World!”)

renders:

<h1>Hello <strong>World!</strong></h1>

As the content of the variable gets added in the pre-processing, you can get fancy with markdown, eg. to produce simple lists like this:

  list = items.map {|item| "* #{item.name}"}.join("\n")
  md("# Here comes a list of items\n [echo list]", :list => list)

An Outlook and a Word of caution

The echo-tag is a very simple example for a macro: You can get fancy with this, pull content from the DB, produce custom html eg to embed videos etc. Get fancy, but not too fancy: It is probably not a good idea to implement things like loops here - in the end it is only markdown, if you want a proper templating-language you probably should pipe this through liquid - but for such simple things here, these macros will do fine. Be a little cautious though when it comes to performance: using gsub&and a regex to replace your macros is not the fastest thing on earth. You either want to cache the results or not have to many big markdown-documents on your page... Other than that: Have fun with these tiny macros!

Filed under: Rails,Ruby,Tools — Tags: , , ,

As a Webadministrator, in order to make sure we serve our customers, my website should be monitored.

He that is amonge you without synne, let him cast the first stone at her. – can’t say that something like this did not really ever happen to me, but the outages on the various jboss-websites appear quite frequently. This is especially annoying, when it happens for Arquillian, a project whose sole purpose it is to prevent these things from happening:

It is not about blame, it is about learning: Arqullian is highly anticipated and very much needed. The state of testing in the various jboss-frameworks is miserable (ever tried to test the esb?) – and we all should be working on this: I am not too confident that this error can’t happen in one of our sites too. Are you?

Filed under: Java — Tags: , , ,