7

In HAML I often want to use a tag with punctuation following immediately after the tag. For example, I might want to put something in bold but have a closing bracket. The formatting would look like this: (Example sentence with bold text.)

Note that the 'bold text' is in bold but the period and closing bracket '.)' are not.

The obvious HAML is like this:

(Example sentence with
%span.important bold text
\.)

but this causes an extra space between 'bold text' and '.)'. Here's one way of doing it instead:

(Example sentence with
%span.important bold text
%span>\.)

Where enclosing the '.)' in a span with > makes the space disappear. However, this requires an unnecessary span.

Is there any way of getting the desired output, without the extra span?

Peter
  • 127,331
  • 53
  • 180
  • 211
  • The haml faq covers pretty much this exact question: http://haml-lang.com/docs/yardoc/file.FAQ.html#q-punctuation - the answer is basically the same as Daves below: inline html or a filter. – matt May 25 '11 at 02:05
  • I've adjusted the example to make it clearer, with thanks to @Dave. – Peter May 25 '11 at 03:46
  • 1
    http://chriseppstein.github.com/blog/2010/02/08/haml-sucks-for-content/ this might help you somehow :) it just supports matt and Dave's assertion that you could just use inline/markdown for inline tags..because haml is for layout – corroded May 25 '11 at 03:52
  • There are three HAML helpers (surround, precede, and succeed) that address this specifically. Read [Haml: Control whitespace around text](http://stackoverflow.com/questions/1311428/haml-control-whitespace-around-text) or read "Helper Methods" in the [HAML documentation](http://haml.info/docs/yardoc/file.REFERENCE.html) – mayatron Jul 16 '13 at 18:00

1 Answers1

7

That initial code snippet shouldn't work in HAML at all:

(Example sentence with
%b bold text
.)

The third line would result in an Illegal element: classes and ids must have values error. It should be:

(Example sentence with
%b bold text
\.)

However, that just fixes the code error. It still displays the way you're complaining about. I only know of two ways to address it:

  1. Just use inline HTML tags in your HAML file: (Example sentence with <b>bold text</b>.)
  2. Install maruku (or another markdown gem) and do the following:
:markdown  
  (Example sentence with **bold text**.)
D. Simpson
  • 1,882
  • 17
  • 32
  • Definitely right about the `\.` (thanks). However, not quite a complete solution for me as `%b` is only an example and I want to handle tags in general, including custom tags. – Peter May 25 '11 at 03:38