websvn icon indicating copy to clipboard operation
websvn copied to clipboard

Syntax highlighting using shebang only?

Open danielmarschall opened this issue 2 years ago • 9 comments

I wonder, is it possible to have syntax highlighting based on the shebang of a file, or is the content not known at the time Geshi/Enscript is initialized?

Examples where Syntax highlighting would be very nice:

https://svn.viathinksoft.com/websvn/filedetails.php?repname=oidplus&path=%2Ftrunk%2Fdev%2Fgenerate_example_data_access

https://svn.viathinksoft.com/websvn/filedetails.php?repname=oidplus&path=%2Ftrunk%2Fdev%2Fgenerate_wellknown_other_mysql

danielmarschall avatar Nov 07 '23 23:11 danielmarschall

One could likely implement content sniffing on the first line, but I think you can solve it this way: https://stackoverflow.com/a/15302869/696632 better.

michael-o avatar Nov 08 '23 09:11 michael-o

I modified my copy of the code to do this. It currently works with enscript only, but I can look at the other markup language too. My solution is pretty inefficient since I don't really know the code base; just one function that does an svn cat, grabs first line, and checks if it is a shebang.

All my code recently has been on my own projects (http://svn.dailydata.net), and not real clear on what I should do with the code. If someone will point me to instructions, I'll be happy to upload and/or if you want it extended to the other one (don't remember name), I'll see what I can do.

Following is a diff using diff svnlook.php svnlook.php.original

` 631,640d630 < < // private function to open file, read first line and see if it is a Unix script < // with #! showing the type of file it is < function getFileType ( $path ) { < $output = runCommand( $this->svnCommandString('cat', $path,'','' ) ); < preg_match( '/^#!.^a-z\s$/', $output[0], $matches ); < return $matches[1]; < } < < 659,662c649 < } else if ( $type = $this->getFileType( $path) ) { // scripting language with #! in first line < $cmd .= ' --color --'.(!$config->getUseEnscriptBefore_1_6_3() ? 'highlight' : 'pretty-print').'='.$type; < } <

  }

666c653 <

`

rrodolico avatar Aug 11 '24 19:08 rrodolico

I modified my copy of the code to do this. It currently works with enscript only, but I can look at the other markup language too. My solution is pretty inefficient since I don't really know the code base; just one function that does an svn cat, grabs first line, and checks if it is a shebang.

All my code recently has been on my own projects (http://svn.dailydata.net), and not real clear on what I should do with the code. If someone will point me to instructions, I'll be happy to upload and/or if you want it extended to the other one (don't remember name), I'll see what I can do.

Following is a diff using diff svnlook.php svnlook.php.original

`

631,640d630 < < // private function to open file, read first line and see if it is a Unix script < // with #! showing the type of file it is < function getFileType ( $path ) { < $output = runCommand( $this->svnCommandString('cat', $path,'','' ) ); < preg_match( '/^#!.^a-z\s$/', $output[0], $matches ); < return $matches[1]; < } < < 659,662c649 < } else if ( $type = $this->getFileType( $path) ) { // scripting language with #! in first line < $cmd .= ' --color --'.(!$config->getUseEnscriptBefore_1_6_3() ? 'highlight' : 'pretty-print').'='.$type; < } <

  }

666c653

<

`

Add a readable patch please

michael-o avatar Aug 11 '24 19:08 michael-o

I'd love to. Just don't know how. Can you tell me the command? Sorry, I've never submitted to another project before, and mine are so focused on specific areas I've never gotten a patch.

rrodolico avatar Aug 11 '24 19:08 rrodolico

I'd love to. Just don't know how. Can you tell me the command? Sorry, I've never submitted to another project before, and mine are so focused on specific areas I've never gotten a patch.

You can attach a patch file.

michael-o avatar Aug 11 '24 20:08 michael-o

figured it out. git clone make changes git diff filename > filename.diff send you filename.diff

found bug. Perl allows #! /usr/bin/perl -w (very old school). Will fix bug, then send patch.

rrodolico avatar Aug 11 '24 20:08 rrodolico

It would not accept a filename with .diff, so I renamed it to .txt. I've run some tests but, if you see any problems, let me know and I'll fix them.

Also, have an idea for syntax highlighting php using the php built in's.

Rod

svnlook.php.txt

rrodolico avatar Aug 11 '24 21:08 rrodolico

It would not accept a filename with .diff, so I renamed it to .txt. I've run some tests but, if you see any problems, let me know and I'll fix them.

Also, have an idea for syntax highlighting php using the php built in's.

Rod

svnlook.php.txt

Your patch isn't bad after all, I will get back to this next week.

michael-o avatar Aug 14 '24 14:08 michael-o

Went through the patch and the current code. There are a few issues here:

  • There is not reason why it should not work for both enscript and geshi
  • It performs yet another svn cat while it could reuse the command in the file
  • It completely ignored rev and peg

michael-o avatar Aug 15 '24 14:08 michael-o