code styles supporting linenumbers alternating bg colors?

Everything related to our flagship word processor.
Post Reply
dr_jerry
Posts: 1
Joined: 2011-04-10 10:55:31

code styles supporting linenumbers alternating bg colors?

Post by dr_jerry »

Hi I'm looking a for a way to introduce a style which support programmatic code like perl or javascript. On the internet there are some javascript css librariers which applies a nice code formatter on your text with linenumbers, alternating background colors and even language aware keyword highlighters. (http://alexgorbatchev.com/SyntaxHighlighter/)

Does Nisus offers the same possibility? I was looking for list styles, which are a bit different from the normal character and paragraph styles. (I don't see them back at the style palette in tooldrawer) But I couldn't get that to work.
Is this possible with either styles or macros?

Cheers,
Jeroen.

Ruchama
Posts: 213
Joined: 2006-08-19 18:35:27

Re: code styles supporting linenumbers alternating bg colors

Post by Ruchama »

You should be able to apply any command (including attributes) and series of command with macros. I am not sure I understood the application yo require, but you should be able to alternate between those macros.

martin
Official Nisus Person
Posts: 4668
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: code styles supporting linenumbers alternating bg colors

Post by martin »

There's no support for syntax highlighting in Nisus Writer. If you had the skill and motivation, you could probably write a NWP macro that exported your document to HTML, fed the necessary parts to that syntax highlighting library, and then saved out the final results.

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: code styles supporting linenumbers alternating bg colors

Post by Kino »

Do you see Perl scripts colourized in Quick Look? If so, try the macro below with some Perl code selected.

Code: Select all

 $fontName = 'Courier'
$fontSize = 10

$sel = TextSelection.active
if ! $sel.length
	exit 'Nothing selected, exiting…'
end

$script = $sel.substring
$scriptPath = File.temporaryPathWithName 'script.pl'
File.writeDataToPath $script, $scriptPath
$folderPath = $scriptPath.filePathByRemovingLastComponent

Set Exported Perl Variables 'scriptPath', 'folderPath'
begin Perl
	`/usr/bin/qlmanage -p "$scriptPath" -o "$folderPath" 2>/dev/null `;
end

$rtfPath = $scriptPath & '.qlpreview/Preview.rtf'
$rtf = File.readDataFromPath $rtfPath
$rtf = Cast to String $rtf
$rtf = Decode RTF $rtf

Push Target Text $rtf
	Set Font Name $fontName
	Set Font Size $fontSize
Pop Target Text

$sel.text.replaceInRange $sel.range, $rtf
$sel.length = $rtf.length
TextSelection.setActiveRange $sel.range
• For me, the syntax colouring of Perl scripts (*.pl) seems to be done by a QL plugin within Xcode.
• The macro finishes keeping the selection. So it would be a piece of cake for you to apply your list style for line number on it, manually or by adding a code to the macro.
• The macro does not check if the selected text is really a Perl script or not.

User avatar
Hamid
Posts: 749
Joined: 2007-01-17 03:25:42

Re: code styles supporting linenumbers alternating bg colors

Post by Hamid »

Thank you, Kino, for the macro (and the idea behind it) which works like a charm.

As for javascript files, if the files are opened in Dashcode or Xcode and selections from js files are pasted to a NWP rtf file, all syntax colouring remains intact in NWP.
Then it is very easy (by using the Statusbar) to select all instances of a particular colour and make syntax colours into defined character styles. Defined styles can then be globally redefined if needed.

BTW js files are also quicklookable on my Mac.

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: code styles supporting linenumbers alternating bg colors

Post by Kino »

Hamid wrote:As for javascript files,
You can make the macro colourize code in other languages such as JavaScript just by changing the file extension of 'script.pl' (line 10) so that it becomes, for example, 'script.js'.

User avatar
Hamid
Posts: 749
Joined: 2007-01-17 03:25:42

Re: code styles supporting linenumbers alternating bg colors

Post by Hamid »

Thank you for that explanation.
I had been viewing js files in Dashcode which shows syntax colouring (which can be copied to NWP) but for perl scripts I did not see any syntax colouring; it is only when I viewed the same perl code in Xcode I realised that it shows syntax colouring for both as well as for other programming languages.

Post Reply