📜 ⬆️ ⬇️

Online compilation on a static site, a recipe for beginners


You have been writing your project for several years and now you want to attract users? I have a simple, as bachelor scrambled eggs with bacon, recipe especially for you.

To make a simple website you will need:

Change any components, experiment and prepare your sites for your favorite language!

We are preparing a hosting


In this example, I will use GitHub for hosting. Yes, it allows not only working with repositories, but also provides the ability to publish sites for the project. All you need to do is to create a gh-pages branch and commit the index.html file with the site there:
# :       user     GitHub, #  repository    . #      : git clone github.com/user/repository.git #   gh-pages cd repository git checkout --orphan gh-pages #    git rm -rf . #    index.html echo "Test" > index.html git add index.html git commit -a -m "Testing gh-pages" #     GitHub git push origin gh-pages 

Now your page is available at http (s): //user.imtqy.com/repository/. Of course, if you wish, you can choose another hosting.

We decorate the dish with html statics



It's time to fill the site with something useful. We find a nice html template on the Internet and upload it to our hosting.
 #   html   : cp -rf ../folder_with_template/* ./ 

Chef Tricks
Choosing a template should immediately pay attention to whether it is displayed correctly on mobile devices. This is done very simply: change the scale of the page (Ctrl + +). The template on a large scale should look good and the horizontal scroll bar should not appear.

Now the most difficult and creative part of the dish. It is necessary to fill a ready-made template with content, add a description of the project, documentation and other useful things.
')
At the end, we do not forget everything to commit and send to GitHub:
 git add * git commit -m "Added some content" git push 


Preparing meat code


For the marinade, we need jQuery and Ace editor written in javascript. You can do without these components, or replace them with something else. Personally, I tried several editors and Ace.js seemed to me the most convenient and functional. Of the shortcomings he has a lot of weight ~ 500Kb.

Choose a place on the page where we put the online compilation. Add to the right place:
 <div id="code"> ... your code goes here ... </div> 

Do not forget to add CSS and javascript download:
 <style type="text/css" media="screen"> #code { width: 100%; float:left; min-height:100px; overflow: hidden; } </style> <script src="assets/js/jquery.min.js"></script> <script src="assets/js/ace/ace.js"></script> 

Now we need to configure our editor:
 <script> var code; $(function() { code = ace.edit("code"); //      id="code" code.setTheme("ace/theme/textmate"); //       code.getSession().setMode("ace/mode/c_cpp"); //       C++  code.setShowPrintMargin(false); // :     80  code.setOptions({ maxLines: Infinity, // :       fontSize: "12pt", // :     }); code.$blockScrolling = Infinity; //  ,     }); </script> 

Chef Tricks
You always want to avoid situations of copying code from the repository to the documentation page. If this is not done, then there is a great chance that the code in the repository and on the page will be out of sync. Then on your wonderful page there will be an expired product, and users will begin to complain.

With GitHub, it's easy enough to avoid this, since it allows cross-origin resource sharing. That is, you can request from your static page on http (s): //user.imtqy.com/repository/ resources, for example, from raw.githubusercontent.com . Just add the following after the code. $ BlockScrolling = Infinity;
 $.get("https://raw.githubusercontent.com/user/repository/master/" + "path_to_source_file_in_repo.cpp", function(data) { code.setValue(data); //      code.clearSelection(); // :      }); 

Advanced cooks can independently add error checking and display the appropriate message to the user.

As a result, you should see your code in an editor with beautiful syntax highlighting:


Fry bacon code


So, now we need to add a button to compile the code and a place to display the result on our page:
 <button id="run" onclick='run();'>Run</button> <p>Output:</p><pre id="output">Waiting...</pre> 

With online compilation we will be helped by the Coliru service:
 function run() { //       var cmd = "g++ -Wall main.cpp -o main_prog && echo 'Compilation: SUCCESS." + " Program output is:\n' && ./main_prog && echo \"\nExit code: $?\""; var output = $("#output"); output.text(''); var to_compile = { "src": code.getValue(), "cmd": cmd, }; output.text("Executing... Please wait."); $.ajax({ url: "http://coliru.stacked-crooked.com/compile", type: "POST", data: JSON.stringify(to_compile), contentType:"text/plain; charset=utf-8", dataType: "text" }).done(function(data) { output.text(data); }).fail(function(data) { output.text("Server error: " + data); }); }; 

Why was Coliru chosen?
There are a lot of services for online compilation, but most of them have no external API, or this API is undocumented. Coliru was the first online compilation service for C ++ code that I had earned.


Give the dish to friends


There were final touches, the cherry on our scrambled eggs is our cake! If we started all this to attract users, then it would be a sensible step to add social buttons.

You can use the plugin SocialLikes
 <link rel="stylesheet" href="social-likes_flat.css"> <meta property="og:title" content="Your Title"/> <meta property="og:image" content="Your logo"/> <link rel="canonical" href="https://user.imtqy.com/repository/"> <meta property="og:url" content="https://user.imtqy.com/repository/"/> <meta property="og:site_name" content="Your repository name"/> <meta property="og:type" content="website"/> <meta property="og:description" content="Your short description"/> ... <script src="social-likes.min.js"></script> ... <div class="social-likes right" data-counters="no"> <div class="facebook" title="Share on Facebook">Facebook</div> <div class="twitter" title="Share on Twitter">Twitter</div> <div class="plusone" title="Share on Google+">Google+</div> <div class="vkontakte" title="Share on VK">VK</div> <div class="odnoklassniki" title="Share on Odnoklassniki">Odnoklassniki</div> <div class="mailru" title="Share on MyWorld">MyWorld</div> </div> 


Done! It remains cunning and deceit to make people go wild. For example, write to them: “Hello, Vasya. Please check if my social buttons work! ”

Instead of the total


The resulting dish can be modified to your taste. Instead of GitHub, you can use any services you like, Coliru can compile and C code (it is possible that other languages ​​are also supported). There are quite a few services for online compilation; if you search, you can find similar services for your favorite language.

I hope this mini-recipe will be useful to someone and will add interactivity to your projects.

The result in my case looks like this: apolukhin.imtqy.com/Boost-Cookbook-4880OS

PS: If it's not difficult, please check if the social buttons work.

Source: https://habr.com/ru/post/270141/


All Articles