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?

Share Improve this question asked Nov 6, 2016 at 2:03 user985030user985030 1,6171 gold badge20 silver badges34 bronze badges 2
  • 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
Add a ment  | 

2 Answers 2

Reset to default 4

Try 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