Insert Partial Gist File

I have a gist setup here . I use the following code to insert a single file from my entity into a web page.

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

But I want to insert one line from the 1.java file. You can say that I want to insert only line # 2 from the 1.java file into my web page. Thanks in advance for your help:)

+4
source share
3 answers

After I did not get useful answers to this question, I tried to make some javascript codes. I downloaded this code on github . The file at this link does the magic. To insert one line in your web page, you need to add a link to my javascript file in the 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 identifier of your gist, YOUR_FILE_NAME_IN_GIST name of the file from which you want to insert the line and LINE_NUMBER , with the line number in the file that you want to embed.

NOTE :

  • If you want to insert one line, then the code tag must contain both the file name and the line number.
  • If you want to insert multiple sample lines from a file, then you should put values โ€‹โ€‹similar to the ranges of MS Word pages for printing. e.g. data-line="1-4,10,12-15" . Please make sure you use the correct syntax so that these values โ€‹โ€‹avoid getting odd results.
  • If you want to insert the entire file, then remove the data-line attribute from the code tag.

I hope I helped people who have encountered this problem or have encountered it.

Since I am constantly making changes to my javascript file, so I think updating this answer every time is not a good idea. So now I am updating the README.md file in the GitHub repository. Anyone with a problem can visit github to read the README.md file. For reference, I am adding a link to my repository here.

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

PS: Please see my javascript file in the github repository and suggest any useful methods for coding.

+13
source

The correct and fastest way is to simply add the .js address with ?file=file.name :)

 <script src="https://gist.github.com/kashif-umair/4672365.js?file=MyApplication.java"></script> 
+4
source

Create a line containing only the desired line.

+1
source

All Articles