Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Partial Gist File

I have a gist setup here. I use the following code to embed a single file from my gist to a webpage.

<script src="https://gist.github.com/4672365.js?file=1.java" type="text/javascript"></script>

But what I want is to embed a single line from file 1.java. You can say that I want to embed only line #2 from file 1.java into my webpage. Thanks in advance for help :)

like image 435
KULKING Avatar asked Jan 30 '13 14:01

KULKING


People also ask

How do you add code snippets to gist?

It's quite simple.Input the code and click on 'create secret gist' – this way search engines wouldn't index your code and only people with the URL will have access to it. Share using the 'Embed' code option – copy the embed code and paste it within your WordPress code editor (the Text tab, not the Visual tab).


2 Answers

After I got no helpful answers to this question, I tried myself to do some javascript coding. I have uploaded that code on github. The file on this link does the magic. To embed a single line into your webpage you need to add reference to my javascript file in HEAD tag. Here is a simple code.

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
    <script type="text/javascript" src="https://github.com/kashif-umair/gist-embed/raw/feature/show_single_line/gist-embed.js" ></script>
  </head>
  <body>
    <code id="gist-<YOUR_GIST_ID>" data-file="<YOUR_FILE_NAME_IN_GIST>" data-line="<LINE_NUMBER>"></code>
  </body>
</html>

You need to replace YOUR_GIST_ID with the ID of your gist, YOUR_FILE_NAME_IN_GIST with the file name from which you want to embed the line and LINE_NUMBER with the line number in file which you want to embed.

NOTE:

  • If you want to embed a single line then both the file name and line number should be present in code tag.
  • If you want to embed multiple selective lines from a file then you should put values something like MS Word page ranges for printing. e.g. data-line="1-4,10,12-15". Please make sure that you used the correct syntax to put these values to avoid getting odd results.
  • If you want to embed whole file then remove the data-line attribute from code tag.

Hopefully I've helped for the people who have or will face this problem.

As I am continuously making changes to my javascript file so I think updating this answer every time is not a good idea at all. So from now I'll update the README.md file in the GitHub repository. Anyone having problems can visit the github to read README.md file. For reference I'm adding the link to my repository here.

https://github.com/kashif-umair/gist-embed

P.S: Please take a look at my javascript file in the github repository and suggest any good practices for coding.

like image 110
6 revs, 2 users 96% Avatar answered Sep 30 '22 23:09

6 revs, 2 users 96%


The Right and fastest way to do is just append the .js address with ?file=file.name : )

<script src="https://gist.github.com/kashif-umair/4672365.js?file=MyApplication.java"></script>
like image 44
Stan Avatar answered Sep 30 '22 23:09

Stan