How to highlight source code for displaying in HTML
A useful online app that allows you to easily create fully formatted HTML to display highlighed code.
I just want to share with you a useful web tool created by Alexander Kojevnikov (thank you) that allows you to quickly and conveniently convert your source code to highlighted HTML so that you can place it on your website or blog.
Lets say you have some Python code snippet:
def convert(x):
return(x*2.54)
## list of values to convert
vals = [31,24,15,6.3]
for each in vals:
print convert(each)
However, what you really want is HTML that displays your source code as it appears in your development environment or code editor (e.g. spyder, jupyter, pycharm, vim, etc.). For example with highlighted keywords like:
def convert(x): return(x*2.54) ## list of values to convert vals = [31,24,15,6.3] for each in vals: print convert(each)
Try it
Just go the the web app at http://hilite.me/ and type your source code in the box on the left, select the language of the source code and the output style, then press "Highlight!". You will get output HTML for displaying your source code for many different languages. The screen shot below shows an example using the Python snippet above as it would be displayed in the vim text editor (my favorite) using vim's default color scheme. You also have the option to display line numbers in the HTML.
Hilite.me is a useful web tool that you can easily use in a quick fashion to highlight and style source code of varying type to output in HTML, markdown, LaTeX, and others. With a bit of research you will find that everything done on the tool can easily be done on your system using the Pygments Python package. This may be worth learning if you commonly have workflows converting source code to readable HTML or produce documentation. Or if you do not have internet access. If there is interest in how to use Pygments in a workflow it will be a future post.

Hi
ReplyDeleteThe article presents a simple, practical way to make source code easier to read when publishing tutorials or technical documentation. Hilite.me lets users paste a code snippet, select its programming language and highlighting style, and generate HTML that can be embedded in a website or blog. The option to include line numbers is especially useful when explaining code step by step, because readers can follow references more easily. The article also points out that Pygments can support a local workflow for developers who regularly need to format code examples.
ReplyDeleteThe discussion of Pygments is a useful reminder that Python can be applied to more than application development and data processing. Developers can use Python packages to automate repetitive tasks, including preparing code samples and supporting documentation workflows. For teams that want to strengthen their practical Python capabilities and explore useful libraries, Corporate Python Training is a relevant learning option.
One of the most valuable aspects of the tool is that it produces HTML that can be added directly to a blog or website, helping code examples remain readable and consistent with the surrounding content. This is particularly helpful for technical writers and developers who publish tutorials in multiple programming languages. Learning Python fundamentals can also make it easier to understand how tools such as Pygments work and how similar formatting tasks could be automated, making Python Online Training in India relevant for readers who want to build those skills.
ReplyDeleteThe article also highlights the connection between code presentation and web publishing. Syntax highlighting improves readability, but the surrounding HTML and CSS determine how the snippet fits into the page, while JavaScript can support interactive documentation experiences. Understanding these web technologies helps developers present technical material in a polished and accessible way, whether they are building a tutorial site, documentation portal, or learning resource. Readers interested in the web side of publishing code examples can explore HTML CSS JavaScript Training.
ReplyDelete