Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add starting line to code tag?

 
Paweł Baczyński
Bartender
Posts: 1761
33
Firefox Browser IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there is a code:And I want to quote a fragment from it using code tag. Let's say line 5.
I might write: But this displays 01. System.out.println("Hello world!"); .
Is there a way to make it display 05. System.out.println("Hello world!"); ?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You could just write "line 5 of the code" - it's what I do, and I think that makes it pretty clear what is being referred to.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be kind of cool to be able to do something like:

[code start='5']System.out.println("Hello world!");[/code]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could definitely see the utility of that when including stack traces that refer to line numbers.
 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a bit of a play around with this but unfortunately without success. It appears that the Ranch forum uses an old version of Alex Gorbatchev's SyntaxHighlighter, version 1.5.1 for which the documentation lives on this old Google Code page.

The [ code ] tags are BB Code which are translated into HTML when you submit your post. So the following BB Code snip:

[ code=java]
System.out.println("Hello world!");
[ /code]
Translates to HTML:

<textarea name="code" class="java" cols="60" rows="10" style="display: none;">System.out.println("Hello world!");
</textarea>

And gives you the output:

Now, according to the SyntaxHighlighter Wiki on configuration options we can add extra configs to the element class as colon separated items. For example if you wanted to remove the "gutter" (line numbers) and the "controls" you'd need the HTML:

<textarea name="code" class="java:nogutter:nocontrols" cols="60" rows="10" style="display: none;">System.out.println("Hello world!");
</textarea>

Which if I translate that back to BBCode would be:

[ code=java:nogutter:nocontrols]
System.out.println("Hello world!");
[ /code]

Output:

At this point I'm feeling confident until I notice that the config item for defining the starting line number is "firstline[x]". So to start a Java snip on line 5 you'd need [ code=java:firstline[5]] which doesn't work because the BB Code closes the code tag on the first closing square bracket. So trying:

[ code=java:firstline[5]]
System.out.println("Hello world!");
[ /code]

gives:

Bummer.

I though that you would be able to just use the plain HTML and bypass the BB Code altogether by unchecking the "Disable HTML in this message" option in the post editor. But that didn't work either.

For now I'm out of ideas....
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:For now I'm out of ideas....

Is there any way of escaping the first ']' so that it is not considered to be the closing bracket of the code tag ?
 
Paweł Baczyński
Bartender
Posts: 1761
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried &#93;
It didn't work. It was translated to &amp;#93;
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&lbrack; and &rbrack; = [ and ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,
I gave you a cow for doing that research. If you or someone else want to write some code, here are the two regular expressions used to do the BB code to HTML translation within JForum. If you feel like changing the regexp to accommodate the extra brackets or write a third section of XML that does it without overlapping the other two, I'm happy to put it in the codebase. (Sorry, I have other things I need to be doing with my actual development time.) If you do decide to implement this, please submit the test cases you used (the BB and output HTML) so I can stick it in our JUnit test suite.

I like this feature because it can be community written and then easily merged into the codebase.

 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell has it:

[ code=java:firstline&lbrack;5&rbrack;]
System.out.println("Hello world!");
[ /code]

gives

However it is a bit hacky and not intuitive to anyone who hasn't followed this thread or knows about such things. I'll brush off my Regex skills and see if I can write a better parser.

(and thanks for the cow Jeanne)
 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,

I have written an updated regular expression that handles the "firstline[5]" configuration in the [code] bbCode tag. I also have a suite of JUnit tests to support it.

How best to proceed?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:
How best to proceed?

Awesome. Can you email it to me and I'll look at it later (tonight or over the weekend). I sent you a PM with my email.
 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne: Done

Pawel: I'm giving you a cow for raising the initial question that led us on this journey to (hopefully) make The Ranch just a little bit better.
 
Paweł Baczyński
Bartender
Posts: 1761
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Pawel: I'm giving you a cow for raising the initial question that led us on this journey to (hopefully) make The Ranch just a little bit better.

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've confirmed Tim's change works with our software, fixes the problem and doesn't introduce regression errors. (I haven't committed yet because I need to convert the unit test.) But it will be committed today or this weekend and in the next release of our forum software.

Pawel or Tim: Would one of you mind editing the RanchGuide wiki page to document how to use code tags in this "advanced" way?

More cows for Tim!
 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Wiki has been updated RanchGuide#using-code-tags
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. And now this feature is in production.

 
Paweł Baczyński
Bartender
Posts: 1761
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:The Wiki has been updated RanchGuide#using-code-tags

Nah... I wanted to update it. It was just waiting till it's in production :P

Jeanne Boyarsky wrote:Great. And now this feature is in production.


Nice, it finally works .
 
Tim Cooke
Sheriff
Pie
Posts: 2920
121
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Pawel, didn't intend to step on your toes with the Wiki update. Apologies.

Jeanne: Thanks so much for taking up my change suggestion and making it a reality. How amazing is that!! Seriously great.

I'm going to need a bigger barn for all these cows! But I really do appreciate the appreciation.
 
Paweł Baczyński
Bartender
Posts: 1761
33
Firefox Browser IntelliJ IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Sorry Pawel, didn't intend to step on your toes with the Wiki update. Apologies.
The first is the best ;). The early bird catches the worm. Etc.
Tim Cooke wrote:I'm going to need a bigger barn for all these cows! But I really do appreciate the appreciation.
Wait until it's too late! If Jeanne appreciated appreciating the appreciation we would go into stack overflow!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic