phrdang / berkeley-class-site

https://phrdang.github.io/berkeley-class-site/
MIT License
0 stars 0 forks source link

Figure out how to hide solutions from included python files #14

Open phrdang opened 3 months ago

phrdang commented 3 months ago

In this commit I used the include tag that is built-in to Jekyll that allows you to import files as text into your Jekyll website. Ideally we want to be able to replace everything between # BEGIN SOLUTION and # END SOLUTION with "YOUR CODE HERE" or some other placeholder, like in the cs88 repo.

Originally I was planning to use a custom Jekyll plugin to accomplish this behavior, but since just-the-docs uses the github-pages gem, you can't use plugins that aren't whitelisted by github (including your own custom plugins).

So either we don't use the github-pages gem (you can still deploy your site with github pages but you have to have a repo with just the files contained in your _site directory) or figure out a way to do this kind of hackily... or figure out a different way to store python questions and solutions.

A WIP solution using liquid tags that I tried doing but isn't really working (spacing and indentation is wrong, and this is not a great long-term solution):

{% capture raw_python %}
{% include questions/another_question.py %}
{% endcapture %}

{% assign lines = raw_python | newline_to_br | split: '<br />' %}

{% assign skip_line = false %}

{% highlight python %}
{% for line in lines %}
{% assign processed = line | strip_html | strip %}
{% if processed == "# BEGIN SOLUTION" %}
    "YOUR CODE HERE"
    {% assign skip_line = true %}
{% elsif processed == "# END SOLUTION" %}
    {% assign skip_line = false %}
{% elsif skip_line == false %}
    {{ line }}
{% endif %}
{% endfor %}
{% endhighlight %}