How do I syntactically highlight text?

176 views
Skip to first unread message

NullVoxPopuli

unread,
Apr 19, 2013, 3:59:22 PM4/19/13
to yar...@googlegroups.com
Currently, in my template, I'm doing this:

<% @examples.each_with_index do |example, index| %>
 <pre class="example code">
<%= html_syntax_highlight(example.text, :plain) %>
 </pre>
<% end %>

this line: <%= html_syntax_highlight(example.text, :plain) %>
doesn't appear to be working they way I want it to.

In my documentation, I have this:
# @example_response 
# ```
# {
# "proposal" => {
# "name" => "proposal name",
# "proposal_sections" => [
# "proposal_contents" => [ ... ]
# ]
# "proposal_attachments" => [ ... ]
# }
# }
# ```


but it renders like this:

<pre class="example code">     ```
{
"proposal" =&gt; {
"name" =&gt; "proposal name",
"proposal_sections" =&gt; [
"proposal_contents" =&gt; [ ... ]
]
"proposal_attachments" =&gt; [ ... ]
}
}
```
   </pre>

shouldn't the strings be a different color or something? or the symbols? (like =>)?
and why do the backticks stay?

in my script that generates the documentation:
--tag example_response:"Example Response" 




Loren Segal

unread,
Apr 19, 2013, 4:07:14 PM4/19/13
to yar...@googlegroups.com, NullVoxPopuli
Hi,

You should not use backticks in @example tags. @example tags are already handled as pre-formatted text. Specifically, the "example.txt" that you are passing to html_syntax_highlight will include your backticks

Secondly, html_syntax_highlight with type=:plain does no syntax highlighting. It's a pass-through command for when you want to disable highlighting.

That said, YARD doesn't have any native support for highlighting non-Ruby source. If code cannot be properly parsed as Ruby, it will fail to be highlighted and revert to no highlighting. You would have to implement an `html_syntax_highlight_js` method that does the parsing and highlighting and call `html_syntax_highlight(example.text, :js)` (or you could call your method directly) in your template instead.

Hope that helps,

Loren
--
 
---
You received this message because you are subscribed to the Google Groups "YARD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to yardoc+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Loren Segal

unread,
Apr 19, 2013, 4:09:38 PM4/19/13
to yar...@googlegroups.com, NullVoxPopuli
Oops, that code looked like JSON to me at a cursory glance. If it's valid Ruby just drop your :plain argument and things should work just fine.

Loren

Loren Segal

unread,
Apr 19, 2013, 4:31:42 PM4/19/13
to L. Preston Sego III, yar...@googlegroups.com
This could very well be due to the way you are formatting your erb file. <pre> tags are whitespace sensitive, so all data inside of them should be printed from column 0. If you are indenting your <pre> tag and then indenting the <%=%> line, that would cause the indentation in the HTML.

Loren


On 4/19/2013 1:28 PM, L. Preston Sego III wrote:
yeah, it's pretty much json. oh well.
the issue Im' running into now (I'm just doing :plain), is that the first line is super indented.
any ideas on that?

# @example_response 
#  [
# {

becomes: 

		    	[
	{

L. Preston Sego III

unread,
Apr 19, 2013, 4:28:49 PM4/19/13
to Loren Segal, yar...@googlegroups.com
yeah, it's pretty much json. oh well.
the issue Im' running into now (I'm just doing :plain), is that the first line is super indented.
any ideas on that?

# @example_response 
#  [
# {

becomes: 


		    	[
	{
On Fri, Apr 19, 2013 at 4:09 PM, Loren Segal <lse...@soen.ca> wrote:

L. Preston Sego III

unread,
Apr 19, 2013, 4:32:54 PM4/19/13
to Loren Segal, yardoc
doh! that was it. thank you. :-D

L. Preston Sego III

unread,
Apr 19, 2013, 5:21:03 PM4/19/13
to Loren Segal, yardoc
this may be related, I'm not sure.
I'm getting this output:
Inline image 1

in the bottom thing, the code is being split up.... and some how being shoved above the arguments...?

for list_section_ids, the @example_response looks like this:
# @example_response
#   {
#   "ids": [ "1", "2", "3" ]
#   }
#


but for list_sections, the @example_response looks like this:
#
# @example_response
#    [{
# "proposal_section": {
# "name": "My Section",
# "proposal_id": "0",
# "sequence": "0",
# "id": "1"
# }
# },
# {
# "proposal_section": { .. }
# }]
#


so... why is this strange behavior occurring?

This is all I'm doing to render the text: 

   <pre class="example code"><%= html_syntax_highlight(example.text, :plain) %></pre>


image.png

L. Preston Sego III

unread,
Apr 19, 2013, 5:28:23 PM4/19/13
to Loren Segal, yardoc
apparentyl it was a mixed kinds of white space issue. o.o
image.png
Reply all
Reply to author
Forward
0 new messages