Skip to content

Commit

Permalink
Improves the TOC
Browse files Browse the repository at this point in the history
* Makes a human phrase out of the title on disk, by using the
`unslugify` function.
* Introduces the equivalent (currently unused) `slugify`.
* Sorts the TOC alphabeitcally.
  • Loading branch information
joehakimrahme committed May 30, 2020
1 parent 08d66f1 commit 73efaa1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 18 additions & 2 deletions blogstrap/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,34 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
import os


def slugify(message):
message = re.sub(r"[^\w\s]", '', message)
message = re.sub(r"\s+", '-', message)

return message


def unslugify(message):
message = message.replace(".", ". ")
message = message.replace("-", " ")
message = " ".join(word.capitalize() for word in message.split())
return message


def context(app, message=None):
def toc():
result = []
for entry in os.listdir(app.config['BLOGROOT']):
if os.path.isfile(entry) and not entry.startswith(".") and \
entry not in app.config['TOC_BLACKLIST']:
result.append("* [{article}](../{article})".format(
result.append("* [{unslug}](../{article})".format(
unslug=unslugify(entry),
article=entry))
return "\n".join(result)
return "\n".join(sorted(result))

context_dict = {
"author": app.config['AUTHOR'],
Expand Down
6 changes: 4 additions & 2 deletions blogstrap/static/blogstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var converter = new showdown.Converter({
window.onload = function () {

var blogpost = document.getElementsByClassName("blogstrap");
//TODO: ES6 only. Provide support for older browsers
for (div of blogpost) {
text = div.textContent.trim().split('/\n */').join('\n');
var html = converter.makeHtml(text);
Expand All @@ -16,7 +15,6 @@ window.onload = function () {


/* Bootstrap Classes */
// Add .table to our generated tables
tables = blogpost[0].getElementsByTagName("table");
for (table of tables) {
table.classList.add("table");
Expand All @@ -28,5 +26,9 @@ window.onload = function () {
}


// the page starts hidden and is only shown visible after the whole
// conversion is done, to avoid the flickering animation at the
// start.
document.body.style.visibility = "visible";

};

0 comments on commit 73efaa1

Please sign in to comment.