Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subtitle field and allow interactive specification of headers. #460

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions knowledge_repo/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'gitpython', # Git abstraction
'tabulate', # Rendering information prettily in knowledge_repo script
'pyyaml', # Used to configure knowledge repositories
'cooked_input', # Used for interactive input from user in CLI tooling

# Flask App Dependencies
'flask', # Main flask framework
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Add subtitle field to post database.

Revision ID: d15f6cac07e1
Revises: 009eafe4838f
Create Date: 2018-10-03 12:31:18.462880

"""

# revision identifiers, used by Alembic.
revision = 'd15f6cac07e1'
down_revision = '009eafe4838f'

from alembic import op
import sqlalchemy as sa


def upgrade():
op.add_column('posts', sa.Column('subtitle', sa.Text(), nullable=True))


def downgrade():
op.drop_column('posts', 'subtitle')
2 changes: 2 additions & 0 deletions knowledge_repo/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ class Post(db.Model):
revision = db.Column(db.Integer())

title = db.Column(db.Text())
subtitle = db.Column(db.Text())
tldr = db.Column(db.Text)
keywords = db.Column(db.Text)
thumbnail = db.Column(db.Text())
Expand Down Expand Up @@ -597,6 +598,7 @@ def update_metadata_from_kp(self, kp):
self.repository = kp.repository_uri
self.revision = kp.revision
self.title = headers['title']
self.subtitle = headers.get('subtitle')
self.tldr = headers['tldr']
self.authors = headers.get('authors', [])
self.tags = headers.get('tags', [])
Expand Down
13 changes: 7 additions & 6 deletions knowledge_repo/app/static/css/pages/base.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.container-fluid {
max-width: 110em;
max-width: 100em;
margin: auto;
}

Expand Down Expand Up @@ -307,7 +307,7 @@
body {
color: #565a5c;
background-color: whitesmoke;
font-size: 14px;
font-size: 12pt;
font-family: 'Lato', sans-serif;
/*background-color: #F8F8F8;*/
}
Expand All @@ -328,11 +328,12 @@ ul {
margin-bottom: 10px;
}

h1 { font-size: 2em; margin: .67em 0 }
h1 { font-size: 1.8em; margin: .67em 0 }
h2 { font-size: 1.5em; margin: .75em 0 }
h3 { font-size: 1.17em; margin: .83em 0 }
h5 { font-size: .83em; margin: 1.5em 0 }
h6 { font-size: .75em; margin: 1.67em 0 }
h3 { font-size: 1.3em; margin: .83em 0 }
h4 { font-size: 1.1em; margin: 1em 0 }
h5 { font-size: 1em; margin: 1.5em 0 }
h6 { font-size: 1em; margin: 1.67em 0; font-style: italic;}
h1, h2, h3, h4,
h5, h6 { font-weight: bolder }

Expand Down
26 changes: 20 additions & 6 deletions knowledge_repo/app/static/css/pages/markdown-base.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
margin-bottom: 2em;
position: relative;
box-shadow: 0px 2px 3px rgba(0,0,0,0.1);
font-size: 12pt;
}

@media (max-width: 992px) {
Expand All @@ -19,24 +20,37 @@
}
}

.renderedMarkdown span.title,
.renderedMarkdown h1,
.renderedMarkdown h2,
.renderedMarkdown h3,
.renderedMarkdown h4,
.renderedMarkdown h5,
.renderedMarkdown h6 {
color: black;
margin-top: 1.5em;
margin-bottom: 0.5em;
font-weight: bold;
}

.renderedMarkdown h1:nth-of-type(1) {
.renderedMarkdown span.title:nth-of-type(1) {
margin-top: 1em;
text-align: center;
display: block;
font-size: 2.2em;
margin-top: .67em;
margin-bottom: .3em;
}

.renderedMarkdown h2 {
.renderedMarkdown span.subtitle {
text-align: center;
display: block;
font-size: 1.5em;
font-style: italic;
margin-bottom: .3em;
}

.renderedMarkdown h1 {
border-bottom: 1px solid #eee;
padding-bottom: 0.3em;
}
Expand Down Expand Up @@ -171,8 +185,8 @@

.renderedMarkdown .codehilite + .code-output {
margin-top: -1em;
margin-left: -21px;
margin-right: -21px;
margin-left: -1.5em;
margin-right: -1.5em;
font-family: monospace;
}

Expand Down Expand Up @@ -230,8 +244,8 @@

.renderedMarkdown .codehilite {
margin: 1em;
margin-left: -21px;
margin-right: -21px;
margin-left: -1.5em;
margin-right: -1.5em;
padding: 0px;
}

Expand Down
3 changes: 1 addition & 2 deletions knowledge_repo/app/templates/markdown-rendered.html
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ <h2> {{ comments | length }} Comments </h2>
})

//Turn all the headers to be links
//h1 = Title, don't want that
var all_headers = [$("h2"), $("h3"), $("h4"), $("h5"), $("h6")]
var all_headers = [$("h1"), $("h2"), $("h3"), $("h4"), $("h5"), $("h6")]
$.each(all_headers, function(index, value){
$.each(value, function(i, v){
var inner_html = v.innerHTML
Expand Down
22 changes: 13 additions & 9 deletions knowledge_repo/app/utils/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pygments
from flask import url_for
from jinja2 import Template
from knowledge_repo.post import KnowledgePost

MARKDOWN_EXTENSIONS = ['extra',
Expand Down Expand Up @@ -33,29 +34,32 @@ def render_post_tldr(post):

def render_post_header(post):

header_template = u"""
header_template = Template(u"""
<div class='metadata'>
<h1>{title}</h1>
<span class='authors'>{authors}</span>
<span class='date_created'>{date_created}</span>
<span class='date_updated'>(Last Updated: {date_updated})</span>
<span class='tldr'>{tldr}</span>
<span class='title'>{{title}}</span>
{% if subtitle %}<span class='subtitle'>{{subtitle}}</span>{% endif %}
<span class='authors'>{{authors}}</span>
<span class='date_created'>{{date_created}}</span>
<span class='date_updated'>(Last Updated: {{date_updated}})</span>
<span class='tldr'>{{tldr}}</span>
<span class='tags'></span>
</div>
"""
""")

def get_authors(usernames, authors):
authors = [u"<a href='{}'>{}</a>".format(url_for('index.render_feed', authors=username), author) for username, author in zip(usernames, authors)]
return u' and '.join(u', '.join(authors).rsplit(', ', 1))

if isinstance(post, KnowledgePost):
return header_template.format(title=post.headers['title'],
return header_template.render(title=post.headers['title'],
subtitle=post.headers.get('subtitle'),
authors=get_authors(post.headers['authors'], post.headers['authors']),
date_created=post.headers['created_at'].strftime("%B %d, %Y"),
date_updated=post.headers['updated_at'].strftime("%B %d, %Y"),
tldr=render_post_tldr(post))
else:
return header_template.format(title=post.title,
return header_template.render(title=post.title,
subtitle=post.subtitle,
authors=get_authors([author.identifier for author in post.authors], [author.format_name for author in post.authors]),
date_created=post.created_at.strftime("%B %d, %Y"),
date_updated=post.updated_at.strftime("%B %d, %Y"),
Expand Down
14 changes: 9 additions & 5 deletions knowledge_repo/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ def get_format(filename, format=None):
class KnowledgePostConverter(with_metaclass(SubclassRegisteringABCMeta, object)):
_registry_keys = None # File extensions

def __init__(self, kp, format=None, postprocessors=None, **kwargs):
def __init__(self, kp, format=None, postprocessors=None, interactive=False, **kwargs):
check_dependencies(self.dependencies, "Whoops! You are missing some dependencies required to use `{}` instances.".format(self.__class__.__name__))
self.kp = kp
self.format = format
if postprocessors is None:
postprocessors = [('extract_images', {}), ('format_checks', {})]
self.postprocessors = postprocessors
self.interactive = interactive
self.init(**kwargs)

@property
Expand Down Expand Up @@ -60,6 +61,9 @@ def __getattribute__(self, attr):
return self.__get_wrapped_with_postprocessors(object.__getattribute__(self, attr))
return object.__getattribute__(self, attr)

def kp_write(self, md, headers=None, images={}):
return self.kp.write(md, headers=headers, images=images, interactive=self.interactive)

def from_file(self, filename, **opts):
raise NotImplementedError

Expand All @@ -73,12 +77,12 @@ def to_string(self, **opts):
raise NotImplementedError

@classmethod
def for_file(cls, kp, filename, format=None, postprocessors=None):
return cls.for_format(kp, get_format(filename, format), postprocessors=postprocessors)
def for_file(cls, kp, filename, format=None, postprocessors=None, interactive=False):
return cls.for_format(kp, get_format(filename, format), postprocessors=postprocessors, interactive=interactive)

@classmethod
def for_format(cls, kp, format, postprocessors=None):
def for_format(cls, kp, format, postprocessors=None, interactive=False):
if format.lower() not in cls._registry:
raise ValueError("The knowledge repository does not support files of type '{}'. Supported types are: {}."
.format(format, ','.join(list(cls._registry.keys()))))
return cls._get_subclass_for(format.lower())(kp, format=format, postprocessors=postprocessors)
return cls._get_subclass_for(format.lower())(kp, format=format, postprocessors=postprocessors, interactive=interactive)
2 changes: 1 addition & 1 deletion knowledge_repo/converters/ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def from_file(self, filename):
dl], template_file='full.tpl')
(body, resources) = md_exporter.from_notebook_node(nb)

self.kp.write(body, images={name.split(
self.kp_write(body, images={name.split(
'images/')[1]: data for name, data in resources.get('outputs', {}).items()})

# Add cleaned ipynb file
Expand Down
2 changes: 1 addition & 1 deletion knowledge_repo/converters/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class MdConverter(KnowledgePostConverter):

def from_file(self, filename):
with open(filename) as f:
self.kp.write(f.read())
self.kp_write(f.read())
2 changes: 2 additions & 0 deletions knowledge_repo/converters/pkp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def to_string(self):
return data.read()

def from_file(self, filename):
# Note: header checks are not applied here, since it should not be
# possible to create portable knowledge post with incorrect headers.
zf = zipfile.ZipFile(filename, 'r')

for ref in zf.namelist():
Expand Down
2 changes: 1 addition & 1 deletion knowledge_repo/converters/rmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def from_file(self, filename, rebuild=True):
Rmd_filename = tmp_path

with open(Rmd_filename) as f:
self.kp.write(f.read())
self.kp_write(f.read())
self.kp.add_srcfile(filename)

# Clean up temporary file
Expand Down
Loading