admin 管理员组文章数量: 1086019
I've written several C++ programs that live in the same repository as a reveal.js presentation. What I would like to do is to simply include the C++ source file into my document.
First, I found a great way to include a file into an html document using the following function (thanks to stackoverflow):
<script src=".1.1/jquery.min.js"></script>
<script>
$(function(){
$("#awesomecpp").load("../src/awesome.cpp");
});
</script>
Then I include it in my reveal.js presentation like this:
<pre><code data-trim>
<div id="awesomecpp"></div>
</code></pre>
However, this is no bueno because I end up with <div id="awesomecpp>
at the top of the source code, plus a bunch of other junk tags at the end of the source code snippet. I'd very much not like to have to copy and paste code because I find I make changes frequently to the code, and will probably not make changes to the presentation code. Furthermore, I found that if there are any special characters such as '&', bad things will also happen. Does anyone have any suggestions for use of a javascript library that can handle what I want to do properly? I simply want to include a file verbatim with code formatting into my reveal.js presentation. Is that too much to ask?
I've written several C++ programs that live in the same repository as a reveal.js presentation. What I would like to do is to simply include the C++ source file into my document.
First, I found a great way to include a file into an html document using the following function (thanks to stackoverflow):
<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(function(){
$("#awesomecpp").load("../src/awesome.cpp");
});
</script>
Then I include it in my reveal.js presentation like this:
<pre><code data-trim>
<div id="awesomecpp"></div>
</code></pre>
However, this is no bueno because I end up with <div id="awesomecpp>
at the top of the source code, plus a bunch of other junk tags at the end of the source code snippet. I'd very much not like to have to copy and paste code because I find I make changes frequently to the code, and will probably not make changes to the presentation code. Furthermore, I found that if there are any special characters such as '&', bad things will also happen. Does anyone have any suggestions for use of a javascript library that can handle what I want to do properly? I simply want to include a file verbatim with code formatting into my reveal.js presentation. Is that too much to ask?
- It's not possible to embed c++ via javascript – Philip Kirkbride Commented Nov 6, 2016 at 2:08
- Not trying to "embed" it, just want to display the actual c++ verbatim (or any language for that matter). – user985030 Commented Nov 6, 2016 at 2:09
2 Answers
Reset to default 4Try something like this:
$.ajax({
url : "../src/awesome.cpp",
dataType: "text",
success : function (data) {
$("#awesomecpp").text(data);
}
});
The issue is that reveal.js uses the highlight.js for syntax highlighting and that will use the contents of the code
tag (your div
in this case).
You most likely need to put the id on the code
tag.
<pre><code data-trim id="awesomecpp">
</code></pre>
You might also have to prevent the reveal.js from initializing the syntax highlighter when it does and manually run it after the ajax is loaded.
本文标签: javascriptHow to include verbatim source code into an html documentStack Overflow
版权声明:本文标题:javascript - How to include verbatim source code into an html document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744098785a2533394.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论