aspose file tools*
The moose likes Ranch Office and the fly likes Add starting line to code tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Add starting line to code tag?" Watch "Add starting line to code tag?" New topic
Author

Add starting line to code tag?

Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 912
    
  14

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
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
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.


Ping & DNS - my free Android networking tools app
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

It would be kind of cool to be able to do something like:

[code start='5']System.out.println("Hello world!");[/code]


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

I could definitely see the utility of that when including stack traces that refer to line numbers.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 954
    
  47

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....


Tim Driven Development
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
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 ?


Joanne
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 912
    
  14

I tried &#93;
It didn't work. It was translated to &amp;#93;
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
&lbrack; and &rbrack; = [ and ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

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.



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 954
    
  47

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
Bartender

Joined: Mar 28, 2008
Posts: 954
    
  47

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

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
Bartender

Joined: Mar 28, 2008
Posts: 954
    
  47

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

Joined: Apr 18, 2013
Posts: 912
    
  14

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

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
Bartender

Joined: Mar 28, 2008
Posts: 954
    
  47

The Wiki has been updated RanchGuide#using-code-tags
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Great. And now this feature is in production.

Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 912
    
  14

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
Bartender

Joined: Mar 28, 2008
Posts: 954
    
  47

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

Joined: Apr 18, 2013
Posts: 912
    
  14

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add starting line to code tag?