Code Blocks and Syntax Highlighting

Continuing the discussion from Making smarter badges:

Will code blocks be syntax highlighted?

select u.id, u.username 
from users u 
where u.email like '%sketchup.com'

Guess not. :frowning:

Would be awesome to have that considering we will probably have some developer talk. In particular Ruby and C/C++ as well as HTML, CSS and JS (WebDialogs).

Any chance of support for that @sam?

This is totally supported

select u.id, u.username 
from users u 
where u.email like '%sketchup.com'

Let’s pretend its cpp

select u.id, u.username 
from users u 
where u.email like '%sketchup.com'

Source:

```
select u.id, u.username 
from users u 
where u.email like '%sketchup.com'
```

Let's pretend its cpp

```cpp
select u.id, u.username 
from users u 
where u.email like '%sketchup.com'
```
1 Like
if sketchup.forums.has?(:syntax_highlighting)
  thomthom = Users.get("tt_su")
  thomthom.happy *= 10
  thomthom.w00t!
  thomthom.thanks(Users.get("sam"))
end
3 Likes
10.times do |i|
  dan.mood += happy * i
end

puts String.new( Sketchup.version )

OK I notice the lexer does not yet know the SketchUp API class or module names.
Some color differences would also be nice.

@Sam, can we tweak this?

The ruby parser for highlight is defined here: highlight.js/ruby.js at main · highlightjs/highlight.js · GitHub

This is doable in a plugin, but we would need to figure out a way to work your rules into there in such a way that it would behave how you want it, or override with a different “ruby” implementation.

I looked at that. It seems that it does not even know Ruby standard class and module Names. It just “notices” Constants that begin with a capital, and colors them only if they follow the keywords class or module.

I do see that the string definition needs fixing for %x and %W strings. I’m starting a fork, for a pull request.

TEST

# Single-quoted
s1 = 'Hello, good day!'
s2 = %q(I am a single-quoted string)

# Double-quoted
d0 = "This may include \nescaped characters."
d1 = %Q(I am a double-quoted string)
d2 = %[Same as a %Q delimited string.]

# execute string
e1 = %x<dir>
e2 = `ver`

# Array string
a1 = %w|one two three|
a2 = %W{1 2 3 4}

# Regular Expression strings
r1 = /(darwin)/
r2 = %r[(mswin|mingw|cygwin)]
WIN = RUBY_PLATFORM !~ r1 || RUBY_PLATFORM =~ r2 ? true : false

TESTING

class Sketchup::Model

m = Sketchup::active_model()

Sketchup.send_action("fixNonPlanarFaces:") if m.is_a?(Sketchup::Model)

def m.function_name( arg = nil )
  puts "This method was passed " +
  arg ? "arg.inspect" : "no argument."
end

# What platform ?
WIN = RUBY_PLATFORM !~ /darwin/
MAC = !WIN

[quote=“DanRathbun, post:8, topic:116”]
I do see that the string definition needs fixing for %x and %W strings. I’m starting a fork, for a pull request.[/quote]

File mods done, and pull request made:

1 Like

The project owner was online (when I made the pull request,) and did the merge within minutes.

[quote=“sam, post:7, topic:116, full:true”]
The ruby parser for highlight is defined here: https://github.com/isagalaev/highlight.js/blob/master/src/languages/ruby.js[/quote]
@sam

I did the above mods, and Ivan made the merge almost immediately.

Nice, I will get this updated next week and ping this topic once its done.

OK when done the string literals in the first TEST post above should all be red.
Specifically: d1, e1, e2 and a2.

It’s updated now and all is looking good! Thanks!

Thank you Sam.

And now I also see that the local var identifier d0 does not colorize correctly.

Also inline code mentions, such as:
“This is class Sketchup::Dimension written inline.”
Is there a way to specify a programming language ??

Easy

def method
   bla
end
```ruby
def method
   bla
end
```

You misunderstood me. I said INLINE, as in the example in my post.

if i try "This is class ```Sketchup::Dimension``` written inline."
I get
“This is class Sketchup::Dimension written inline.”


if i try "This is class ```ruby Sketchup::Dimension``` written inline."
I get
“This is class ruby Sketchup::Dimension written inline.”

So sorry, I missed this, triple backtick does not work inline. Effectively there is no way at the moment to colorize inline :frowning:

Noting that:

[code=ruby]
class Author::Observers::DimensionWatcher < Sketchup::DimensionObserver
[/code]

… produces:

class Author::Observers::DimensionWatcher < Sketchup::DimensionObserver

Use case: Pasting code example block from a post on a phpBB engined forum.

Perhaps Discourse could translate:

"[code=langname]"
 to 
"```langname \n"

... and:
"[/code]"
 to 
"\n```"

:question: