Skip to content

Commit 76de68c

Browse files
committed
First working version (C++ only) with packaged extension.
1 parent 496ea1e commit 76de68c

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#github-linkify
22

3-
This is a Chrome extension to turn `#include X` and `import X`/`from X import Y` statments into hyperlinks, so you can find the files easily. (I don't know why this hasn't been done already.)
4-
5-
Designed initially to work with CMSSW. YMMV. Not working? Log and Issue.
3+
This Chrome extension turns `#include X` in C/C++ files (and in the future `import X`/`from X import Y` in python configs) into hyperlinks when viewing CMSSW files on Github.com, so you can get to the includes easily. (I don't know why this hasn't been done already.)
64

75
## Instructions
86

97
1) First clone me:
108
```
11-
git clone
9+
git clone [email protected]:raggleton/github-linkify.git
1210
```
1311
2) Then install me:
12+
- go to `chrome://extensions`
13+
- drag and drop the `github-linkify.crx` package onto the window. This should install it. (not tested, please feedback)
1414

15+
## TODO
16+
- [ ] Do for Python `import`/`from X import Y` statements
17+
- [ ] ignore STL/boost libraries (like `<iostream>`)
18+
- [ ] keep the colour of the links to their original colour
1519

1620
Maybe I'll do a Firefox equivalent some day. If I ever get this one done.
1721

contentscript.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// get current page URL
2+
url = table.baseURI;
3+
// make root URL, i.e. only up to CMSSW_*
4+
var pattern = /CMSSW_[0-9]_[0-9]_[0-9]_[A-Za-z0-9]*/;
5+
cmssw = pattern.exec(url)[0]; // gives us the CMSSW name
6+
rootURL = url.substr(0, url.search(pattern)+cmssw.length+1);
7+
// console.log("URL: " + rootURL);
8+
9+
// pattern to do #include matching
10+
cpp_pattern = /#include/;
11+
cpp_header_pattern = /[<\"].*[>\"]/;
12+
13+
// the lines of code are stored in a table
14+
table = document.getElementsByClassName("js-file-line-container")[0];
15+
16+
// loop through all rows <tr>
17+
var rows = document.getElementsByTagName("tr");
18+
for (var i = 0; i < rows.length; i++) {
19+
for (var j = 0; j < rows[i].childNodes.length; j++) {
20+
21+
// check it's a line of the file, not anything else
22+
var file_line_pattern = /js-file-line/;
23+
var cell = rows[i].childNodes[j];
24+
if (file_line_pattern.test(cell.className)) {
25+
var line = cell.textContent;
26+
27+
// check line has a C++ #include, if so pull the header path
28+
// TODO: check it's not a STL include, like iostream
29+
if (cpp_pattern.test(line)) {
30+
var path = cpp_header_pattern.exec(line)[0];
31+
path = path.replace(/[<>\"]/g,""); //g inmportant - global so replaces all instances
32+
var link = rootURL.concat(path);
33+
34+
// let's replace the text with a link
35+
// TODO keep same styling as before
36+
for (var k = 0; k < cell.childNodes.length; k++) {
37+
if (cell.childNodes[k].textContent.indexOf(path) != -1) {
38+
var cell_html = cell.childNodes[k].innerHTML;
39+
cell.childNodes[k].innerHTML = cell_html.replace(path, "<a href=\""+link+"\">"+path+"</a>");
40+
}
41+
}
42+
}
43+
// TODO test for python
44+
}
45+
}
46+
}

github-linkify.crx

53.9 KB
Binary file not shown.

manifest.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Github linkify for CMSSW",
3+
"description": "Turns #include statments into links when browing CMSSW on Github.",
4+
"version": "0.1",
5+
"content_scripts": [
6+
{
7+
"matches": [
8+
"https://www.github.com/cms-sw/cmssw/*",
9+
"https://github.com/cms-sw/cmssw/*"
10+
],
11+
"js": ["contentscript.js"],
12+
"run_at": "document_end" // pay attention to this line
13+
}
14+
],
15+
"manifest_version":2
16+
}

0 commit comments

Comments
 (0)