Skip to content

Commit

Permalink
Merge pull request #1261 from tilburgsciencehub/codebox-download-colo…
Browse files Browse the repository at this point in the history
…rs-1241

Codebox download colors 1241
  • Loading branch information
hannesdatta authored Aug 23, 2024
2 parents 6f8c0bc + c97ff08 commit 69a4975
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 17 deletions.
14 changes: 12 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functions import build_data_dict, fetch_contributions_for_the_single_contributor, generate_table_of_contents, get_breadcrumbs, find_related_articles, calculate_reading_time, fetch_meta_data, recently_published
import os
from models import db, articles, Contributors, blogs, Topics
from html_parser import htmlize
from html_parser import htmlize, r_to_html_plaintext
from redirectstsh import setup_redirects
from utils import get_git_commit_hash
import redirects_config
Expand Down Expand Up @@ -144,7 +144,17 @@ def topic_single(first_level_topic_path, second_level_topic_path, third_level_to
article = articles.query.filter_by(path=article_path).first()
meta_data = fetch_meta_data(article)
related_articles = None
if article:
table_of_contents = None
content = None
reading_time = 0
if article is None:
file_path = 'content/topics/' + first_level_topic_path + '/' + second_level_topic_path + '/' + third_level_topic_path + '/' + article_path
## open file and retrieve R code content
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
code_content = r_to_html_plaintext(content)
return render_template('code_topic.html', content=code_content)
else:
related_articles = find_related_articles(article_path, articles, Topics)
content = htmlize(article.content)
table_of_contents = generate_table_of_contents(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: "This topic provides background and motivation for the difference-i
keywords: "causal inference, difference-in-difference, DID, R, regression, model, canonical DiD, difference in means table, potential outcomes framework, average treatment effect, ATE, ATT, ATU, treatment effects"
draft: false
weight: 1
author: "Roshini Sudhaharan, Valerie Vossen"
author: "Roshini Sudhaharan,Valerie Vossen"
aliases:
- /canonical-DiD
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Please develop your content in English.

### Start Your Content With Our Templates

Please use our [building block templates](https://raw.githubusercontent.com/tilburgsciencehub/tsh-website/master/content/topics/more-tutorials/contribute-to-tilburg-science-hub/building-block-shell.md) or [tutorial template](https://raw.githubusercontent.com/tilburgsciencehub/tsh-website/master/content/topics/more-tutorials/contribute-to-tilburg-science-hub/tutorial-shell.md) when developing new building blocks or tutorials.
Please use our [building block templates](https://raw.githubusercontent.com/tilburgsciencehub/tsh-website/master/content/topics/Collaborate-share/Project-management/engage-open-science/contribute-to-tilburg-science-hub/building-block-shell.md) or [tutorial template](https://raw.githubusercontent.com/tilburgsciencehub/tsh-website/master/content/topics/Collaborate-share/Project-management/engage-open-science/contribute-to-tilburg-science-hub/tutorial-shell.md) when developing new building blocks or tutorials.

<!-- ### Contribute via Git Pull Requests
Expand Down
47 changes: 36 additions & 11 deletions html_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import html
import re
from bs4 import BeautifulSoup
import markdown
Expand All @@ -24,6 +25,15 @@ def convert_code_blocks_to_html(md_content):
# Function to replace code block shortcodes with HTML
def replace_codeblock(match):
code_content = match.group(1).strip()
# Regular expression to match '[R-link]' followed directly by content within parentheses
pattern = r'\[R-link\]\(([^)]+)\)'

# Search for the pattern in the text
link_match = re.search(pattern, code_content)
code_content_to_open = None
if link_match:
# Extract the content inside the parentheses
code_content_to_open = link_match.group(1).strip()
language_matches = re.finditer(r'```(\w+)(.*?)```', code_content, re.DOTALL)
code_blocks = []
tab_nav = []
Expand All @@ -48,23 +58,28 @@ def replace_codeblock(match):
languages_processed.append(language)

idx += 1

code_content = ''.join(code_blocks)
tab_nav_html = f'<ul class="nav nav-tabs mb-3" id="pills-tab" role="tablist">\n' \
f' {" ".join(tab_nav)}\n' \
f'</ul>'

## Only display download button when there is code content for that
download_button_html = f'<a class="downloadCodeBtn" style="margin-right: 20px" href="../{code_content_to_open}"><img src="/img/download.svg"></a>' if code_content_to_open else ''

return f'<div class="codeblock">\n' \
f'<div class="d-flex justify-content-between">\n' \
f' {tab_nav_html}\n' \
f' <div class="float-right d-flex align-items-center">\n' \
f' <a class="copyCodeBtn" href="#0"><img src="/img/copy-code.svg"></a>\n' \
f' </div>\n' \
f'</div>\n' \
f' <div class="inner">\n' \
f' {code_content}\n' \
f' </div>\n' \
f'</div>'
f'<div class="d-flex justify-content-between">\n' \
f' {tab_nav_html}\n' \
f' <div class="float-right d-flex">\n' \
f' {download_button_html}\n' \
f' <a class="copyCodeBtn" href="#0"><img src="/img/copy-code.svg"></a>\n' \
f' </div>\n' \
f'</div>\n' \
f' <div class="inner">\n' \
f' {code_content}\n' \
f' </div>\n' \
f'</div>'


md_content = codeblock_pattern.sub(replace_codeblock, md_content)
return md_content
Expand Down Expand Up @@ -384,6 +399,16 @@ def remove_empty_pre_code_tags(html_content):
cleaned_content = re.sub(empty_pre_code_pattern, '', html_content)
return cleaned_content

# Parameters:
# - r_content: String R code content
# Returns:
# - String with R code enclosed in <pre><code></code></pre> tags
def r_to_html_plaintext(r_content):
# Escape HTML special characters to display as plain text
escaped_content = html.escape(r_content)
# Wrap the content in <pre> and <code> tags for HTML
html_content = f"<pre><code>{escaped_content}</code></pre>"
return html_content
# Main function to convert Markdown file content to HTML
# Parameters:
# - md_file_content: String containing Markdown file content
Expand Down
2 changes: 1 addition & 1 deletion static/img/copy-code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion static/img/download.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions templates/code_topic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>{{ content|safe }}</div>

0 comments on commit 69a4975

Please sign in to comment.