Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit f353c6c

Browse files
authored
add link to source code in docs (#4807)
* add link to source code in docs * fix test
1 parent 0a83271 commit f353c6c

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

.github/workflows/ci.yml

+10-9
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ jobs:
373373
run: |
374374
pip freeze
375375
376+
- name: Prepare environment
377+
run: |
378+
if [[ $GITHUB_EVENT_NAME == 'release' ]]; then
379+
echo "DOCS_FOLDER=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV;
380+
echo "BASE_SOURCE_LINK=https://github.com/allenai/allennlp/blob/${GITHUB_REF#refs/tags/}/allennlp/" >> $GITHUB_ENV;
381+
else
382+
echo "DOCS_FOLDER=master" >> $GITHUB_ENV;
383+
echo "BASE_SOURCE_LINK=https://github.com/allenai/allennlp/blob/master/allennlp/" >> $GITHUB_ENV;
384+
fi
385+
376386
- name: Build docs
377387
run: |
378388
./scripts/build_docs.sh
@@ -384,15 +394,6 @@ jobs:
384394
git config --global user.name "ai2service"
385395
git config --global push.default simple
386396
387-
- name: Set target folders
388-
if: github.event_name == 'release' || github.event_name == 'push'
389-
run: |
390-
if [[ $GITHUB_EVENT_NAME == 'release' ]]; then
391-
echo "DOCS_FOLDER=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV;
392-
else
393-
echo "DOCS_FOLDER=master" >> $GITHUB_ENV;
394-
fi
395-
396397
- name: Stage docs
397398
if: github.event_name == 'release' || github.event_name == 'push'
398399
run: |

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
## Unreleased
1010

11+
### Added
12+
13+
- Added links to source code in docs.
14+
1115

1216
## [v1.2.2](https://github.com/allenai/allennlp/releases/tag/v1.2.2) - 2020-11-17
1317

docs/css/extra.css

+12
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ h4 {
77
h2, h3, h4 {
88
color: #213744;
99
}
10+
11+
.alignleft {
12+
float: left;
13+
}
14+
15+
.alignright {
16+
float: right;
17+
}
18+
19+
a.sourcelink {
20+
color: #888;
21+
}

scripts/py2md.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030
logging.basicConfig(level=logging.INFO)
3131
logger = logging.getLogger("py2md")
32+
BASE_MODULE = os.environ.get("BASE_MODULE", "allennlp")
33+
BASE_SOURCE_LINK = os.environ.get(
34+
"BASE_SOURCE_LINK", "https://github.com/allenai/allennlp/blob/master/allennlp/"
35+
)
3236

3337

3438
class DocstringError(Exception):
@@ -247,7 +251,7 @@ def _transform_cross_references(self, line: str) -> str:
247251
Replace sphinx style crossreferences with markdown links.
248252
"""
249253
for match, ty, name in self.CROSS_REF_RE.findall(line):
250-
if name.startswith("allennlp."):
254+
if name.startswith(f"{BASE_MODULE}."):
251255
path = name.split(".")
252256
if ty == "mod":
253257
href = "/api/" + "/".join(path[1:])
@@ -396,20 +400,24 @@ def _format_classdef_signature(self, cls: Class) -> str:
396400

397401
def _render_module_breadcrumbs(self, fp, mod: Module):
398402
submods = mod.name.split(".")
399-
if submods[0] != "allennlp":
400-
return
401403
breadcrumbs = []
402404
for i, submod_name in enumerate(submods):
403405
if i == 0:
404-
title = f"*{submod_name}*"
406+
title = f"<i>{submod_name}</i>"
405407
elif i == len(submods) - 1:
406-
title = f"**.{submod_name}**"
408+
title = f"<strong>.{submod_name}</strong>"
407409
else:
408-
title = f"*.{submod_name}*"
409-
# href = "/api/" + "/".join(submods[1 : i + 1])
410-
# breadcrumbs.append(f"[{title}]({href})")
410+
title = f"<i>.{submod_name}</i>"
411411
breadcrumbs.append(title)
412-
fp.write("[ " + "".join(breadcrumbs) + " ]\n\n---\n\n")
412+
"/".join(submods[1:])
413+
source_link = BASE_SOURCE_LINK + "/".join(submods[1:]) + ".py"
414+
fp.write(
415+
"<div>\n"
416+
' <p class="alignleft">' + "".join(breadcrumbs) + "</p>\n"
417+
f' <p class="alignright"><a class="sourcelink" href="{source_link}">[SOURCE]</a></p>\n'
418+
"</div>\n"
419+
'<div style="clear: both;"></div>\n\n---\n\n'
420+
)
413421

414422
def _render_object(self, fp, level, obj):
415423
if not isinstance(obj, Module) or self.render_module_header:

scripts/tests/py2md/basic_example_expected_output.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
<div>
2+
<p class="alignleft"><i>scripts</i><i>.tests</i><i>.py2md</i><strong>.basic_example</strong></p>
3+
<p class="alignright"><a class="sourcelink" href="https://github.com/allenai/allennlp/blob/master/allennlp/tests/py2md/basic_example.py">[SOURCE]</a></p>
4+
</div>
5+
<div style="clear: both;"></div>
6+
7+
---
8+
19
This is a docstring.
210

311
And this is a multi-line line: [http://example.com](https://example.com/blah/blah/blah.html).

0 commit comments

Comments
 (0)