javascript - Import and syntax highlight code from external source -
i'm not front-end developer , have been struggling hours highlight.js want. need display code nicely in blog. okay, works in renders code post into
<pre><code>...</code></pre>
...very nicely , colourfully chosen style, idea.css example. i've got styles , highlight.pack.js ready go in directory.
but messy paste whole program between tags! more cleanly reusable other things if code can stay in file.
what shortest , elegant way (without loading in external libraries if possible) pull code out of python file in same directory mycode.py
in between tags?
the main reference library here.
i assuming code file highlight lives on webserver.
- import file using jquery
get
. - put content code tags.
- call appropriate function hightlight.js make highlight code.
here html/js code:
<pre><code></code></pre> <script type="text/javascript"> $.get("/mycode.py", function(response) { //(1) $("code").html(response); //(2) $("code").each(function(i, block) { hljs.highlightblock(block); //(3) }); }); </script>
note assumes there 1 <code>
tag in page.otherwise step 2 needs adjusted.
Comments
Post a Comment