Original Question:
I can't find how to activate syntax highlighting on GitBook!
It works perfectly when I look at the .md files on GitHub where my repository is hosted, by not when I look at my rendered gitbook on the web.
Any ideas? Thanks
Edit:
it works for bash, Python, C, C++, javascript, etc, but not for Fortran! It seems that gitbook and github don't use the same syntax highlighting package.
Is there a way to activate syntax highlighting with Fortran on GitBook?
Sample code:
``` fortran
program compute_t
implicit none
integer :: d1, d2, d3, d4 d5 ! Input data
integer :: u1, u2, v, w, t ! Computed entities
call read_data(d1,d2,d3,d4,d5)
call compute_u(d1,d2,u1)
call compute_u(d3,d4,u2)
call compute_w(d5,w)
call compute_v(u2,w,v)
call compute_t(u1,v,t)
write(*,*), "t=", t
end program
```
GitBook's own documentation, which is available as a GitBook, uses named fenced code blocks as popularized by GitHub:
```markdown
# H1
## H2
### H3
#### H4
##### H5
###### H6
```
Although I can't find any documentation that explicitly says this is supported, I suspect that it works just fine.
Edit:
it works for bash, Python, C, C++, javascript, etc, but not for Fortran! It seems that gitbook and github don't use the same syntax highlighting package.
GitHub uses a Ruby library called Linguist for syntax highlighting. It looks like GitBook uses the JavaScript library highlight.js, which doesn't support Fortran.
Is there a way to activate syntax highlighting with Fortran on GitBook?
Short of contributing Fortran support to highlight.js, I don't think there's a good solution for this at the moment.
Related
cgo is written in "comments" in go which means by default it's given comment syntax highlighting. It would be nice have proper golang and C syntax highlighting but within cgo files.
package main
// ... C code or #include here ...
import "C"
... Go code here ...
Example
I'd like this for either Visual Studio Code, or ViM.
How can this be achieved?
One way is to place the C code in a header file, for instance example.h, then in your Go program use:
// #include "example.h"
import "C"
When opening example.h, you get syntax highlighting.
Alternatively, for ViM, the SyntaxRange plugin can supposedly highlight C code that is part of Go code, but it may not be straightforward to configure.
I woul say that, depending on your editor this is somewhere between impossible, and pointlessly difficult.
The only way you can tell the difference between a normal comment block and one used by cgo, is that the cgo block is immediately followed by import "C". Depending on how the syntax highlighting lexer for your editor is constructed it may or may not be able to detect this.
A possible partial solution would be to write a simplified subset of the C highlighter that only effects obvious code, then apply that to all comment blocks. Not a very good solution, but better than nothing.
Good luck!
I'm learning Ruby and I'm using Sublime Text 3 but I find the syntax highlighting really strange.
For example :
Ugly syntax
Even after setting the syntax to ruby.
Ruby syntax set
I'd like to know if this is normal, or if I need to change something on the users
settings or something like that.
The syntax file for Ruby (Ruby.sublime-syntax) contains a list of unresolved issues. Among them is:
text:
"p << end
print me!
end"
symptoms:
not recognized as a heredoc
solution:
there is no way to distinguish perfectly between the << operator and the start
of a heredoc. Currently, we require assignment to recognize a heredoc. More
refinement is possible.
• Heredocs with indented terminators (<<-) are always distinguishable, however.
• Nested heredocs are not really supportable at present
So yeah, it's normal.
You could visit https://packagecontrol.io/ and use something like Railscast Colour Scheme
The basic syntax highlighting that comes w/ sublime is pretty sparse - these packages usually do a better job. Also this is just one example. There's plenty of themes and color schemes.
To install package control ctrl+ and past in the snippet according to your version of sublime from this page https://packagecontrol.io/installation#st3
I cannot find this anywhere and I swear I used to be able to very simply without much prying. Can anyone help me? Thanks. I would appreciate it. Also, does prettify support Batch?
I thought it would be helpful to have an actual list rather than just a link. I found it in the loader directory that #MikeSamuel linked to from Javascript code prettifier. As the readme states, the prettify.js comments are the authoritative source. However, What is syntax highlighting and how does it work? provided a better formatted list, so I will copy that below. Refer to the the links for the most up-to-date information.
If you are using the Prettify codes to markup Stack Overflow code, you use
<!-- language: lang-or-tag-here -->
your code
Language Codes:
Let Prettify interpret the code and guess.
default
Explicitly do not use any syntax highlighting.
lang-none
Bash and other Shell scripting
lang-bash, lang-bsh, lang-csh, lang-sh
C, C++, et al
lang-c, lang-cc, lang-cpp, lang-cxx, lang-cyc, lang-m
C#
lang-cs
Clojure
lang-clj
CoffeeScript
lang-coffee
CSS
lang-css
Dart
lang-dart
Delphi
lang-pascal
Erlang
lang-erl, lang-erlang
Go
lang-go
Haskell
lang-hs
HTML
lang-html
Java
lang-java
JavaScript
lang-js, lang-javascript
JSON
lang-json
LaTeX and TeX
lang-latex, lang-tex
Lisp and Scheme
lang-cl, lang-el, lang-lisp, lang-lsp, lang-scm, lang-ss, lang-rkt
Lua
lang-lua
OCaml, SML, F#, et al
lang-fs, lang-ml
Pascal
lang-pascal
Perl
lang-pl, lang-perl
PHP
lang-php
Protocol buffers
lang-proto
Python
lang-py, lang-python, lang-cv
R and S
lang-r, lang-s
Regex
lang-regex
Ruby
lang-rb, lang-ruby
Rust
lang-rc, lang-rs, lang-rust
Scala
lang-scala
SQL
lang-sql
VHDL
lang-vhdl, lang-vhd
Visual Basic
lang-vb, lang-vbs
XML
lang-xml
You can find a table in the FAQ, under the header For which languages does it work?:
The comments in prettify.js are authoritative but the lexer should work on a number of languages including C and friends, Java, Python, Bash, SQL, HTML, XML, CSS, Javascript, Makefiles, and Rust. It works passably on Ruby, PHP, VB, and Awk and a decent subset of Perl and Ruby, but, because of commenting conventions, but doesn't work on Smalltalk.
Other languages are supported via extensions: ...
You can find the handlers, with their extensions in the loader directory
For the mapping from extensions to builtin languages, see the registerLangHandler calls in prettify.js
m4 can be used with different programming languages because its default policy on unrecognised text is to output and it has a quoting mechanism required for convenient preprocessor usage. I mean, I can write preprocessed code in such a way that my IDE thinks it's Ada. m4 macro invocations (include, define) are hidden in Ada comments. Ada comments start with --, Prolog comments start with % or inside /* */. I can't mix Ada and Prolog easily.
m4 does preprocessing well enough, but its programming side sucks. I thought: why don't me try a general purpose language instead? Prolog looks like being appropriate for my tasks. Prolog has ISO standard, and logic programming looks being useful for reasoning about source generation.
So here we are: we have an arbitrary output language (Ada, then JavaScript, then maybe something else), and it would be nice to write source code in native IDEs for corresponding languages most of the time. I need preprocessor to have a proper parser (like m4 has). This parser shouldn't have Ada syntax knowledge, instead it should only know Ada lexical, and it should be possible to make its macro invocations look like Ada functions invocations.
Its command line interface should look similar to m4 or gcc -E. It should be possible to do both kinds of includes: pure Prolog source code and preprocessed code.
If you know how can I shortly achieve these goals, please write here. I can probably accept non-Prolog solutions, but among the others I prefer standard languages. m4 is part of POSIX, for example.
Prolog is an unusual choice for this task. Indeed, SWI-Prolog used to have the possibility to invoke C preprocessor - should has been removed now, because of its little usefulness.
Anyway, a DCG could be used. I would advice against it if you are not really proficient in Prolog, because debugging can be difficult:
prep(I, O) :-
phrase(exp, I, O).
exp --> mac, !, exp.
exp, [C] --> [C], exp.
exp --> [].
% macro definitions, use 'pushback' argument to change text
mac, "AAA" --> "a".
mac, "G" --> "goal".
example
?- prep("my goal is mars", X).
X = "my G is mAAArs" .
edit: from SWI-Prolog mailing list, WRT the latest release 6.3.18
MODIFIED: Deleted preprocessor/2 after discussion on the
mailinglist. Code relying on this (we hope none) can use the hook
user:prolog_load_file/2 to achieve the same result.
I think the functionality available is more or less that of #include <...>.
For macro expansion (or better, term rewrite) in Prolog there is goal_expansion or term_expansion
edit: latest release brings quasiquotations. Together with a parser in DCG could help to achieve the transformation.
Have you looked at StringTemplate? It's a part of ANTLR (compiler construction toolket). It's written in Java (though there's a C# port, among others). An ANTLR grammar can be written to target many different languages. StringTemplate is what it uses to generate code for the different target languages.
Check it out.
http://antlr.org
http://stringtemplate.org
http://www.cs.usfca.edu/~parrt/
I use DCGs to generate python scripts for FontForge (to create and maintain candlestick fonts for forex trading); I tried M4 scripts but they are not easily amenable to programming (as you mention) and difficult to merge with my glyph definitions that reside in a Prolog database.
DCGs are perceived to be mainly for parsing; but I find they are like a templating engine on their own.
I'm using Sphinx for code documentation and use several languages within the code, I would like to setup highlighting for all of that code. Sphinx briefly mentions a few of the languages it supports (on this page), and then mentions that it uses Pygments for lexical analysis and highlighting. Sifting through the documentation for both Sphinx and Pygments yielded me no clue on how to do something like highlight objective-c code.
Pygments does mention the list of languages it supports, here, however that doesn't tell me the exact syntax that I must use within Sphinx (.rst files) to tell the code block to highlight for a specific language. For example, to highlight c++ code you simply use this before your code block:
.. highlight:: c++
However after trying these I cannot seem to highlight Objective-C code:
.. highlight:: Objective-C
.. highlight:: objective-c
.. highlight:: Obj-C
.. highlight:: obj-c
Can anyone supply me with the list of languages (as you would refer to them within documentation)?
pygmentize -L lexers lists all supported lexers.
http://pygments.org/languages/
http://pygments.org/docs/lexers/
http://pygments.org/docs/cmdline/#getting-help
As far as I can tell, the list is in the file pygments/lexers/_mapping.py, in the (autogenerated) dictionary LEXERS. In my copy, I see a line
'ObjectiveCLexer': ('pygments.lexers.compiled', 'Objective-C', ('objective-c', 'objectivec', 'obj-c', 'objc'), ('*.m',), ('text/x-objective-c',)),
I think this should mean that any of the tags objective-c, objectivec, obj-c, or objc should work, as long as your version of Pygments is up-to-date. They work for me.
If you install pygments module. You can use this script to get a list of supported highlighters:
from pygments.lexers import get_all_lexers
lexers = get_all_lexers()
for lexer in lexers:
print "-\t" + lexer[0] + "\n"
print "\t-\t" + "\n\t-\t".join(lexer[1]) + "\n"
First indent level of output will be general name and second level will be short names of highlighters that you can use.
Example Output
Debian Sourcelist
sourceslist
sources.list
Delphi
delphi
pas
pascal
objectpascal
Source