[Sync] Article post creation updated. Kazimir updated: video is not part of markdown

This commit is contained in:
Roman Timashev committed 2017-10-13 17:33:27 +03:00
1 parent 52a80d6496
commit 8627db4ccd
5 files changed
+81 -109

No files matched your search

+41 -35
View File
@@ -1,54 +1,45 @@
import lxml.html
import requests
from markdown import markdown
from kazimir.video import VideoExtension
from kazimir.figure import FigureExtension
from kazimir.instagram import InstagramExtension
from sync.data import request
def markdown_to_html(text):
return markdown(text, extensions=[
FigureExtension(),
VideoExtension(),
InstagramExtension(),
])
def kazimir_to_html(text):
html = make(text)
return typo(html)
def get_images_src(text: str) -> [str]:
html = markdown(text)
post_html = lxml.html.fromstring(html)
images = post_html.cssselect('img')
return [img.get('src') for img in images]
def html_from_tree(tree):
html = lxml.html.tostring(tree, encoding='unicode')
html = typo(html)
return html
def typo(html):
url = 'http://api.tmshv.com/typograph/v1'
res = requests.post(
url=url,
headers={
'Content-Type': 'text/plain; charset=utf-8',
},
data=html.encode('utf-8')
)
if res.status_code == 200:
return res.text
return html
def make(text):
def kazimir_to_html(text: str) -> str:
tokens = text.split('\n')
tokens = markup_tokens(tokens)
tokens = join_tokens(tokens)
tokens = merge_tokens(tokens)
return compile_tokens(tokens)
# return tokens
def html_from_tree(tree):
html = lxml.html.tostring(tree, encoding='unicode')
return html
async def typo(text):
url = 'http://www.typograf.ru/webservice/'
params = dict(text=text)
return await request.post(url, params, {'chr': 'utf-8'})
def join_tokens(tokens):
@@ -110,6 +101,8 @@ def compile_tokens(tokens: list):
fn = compile_image_collection
elif tag == 'image':
fn = compile_image
elif tag == 'url':
fn = compile_url
else:
fn = compile_any
compiled.append(fn(ts))
@@ -203,11 +196,25 @@ def compile_image_collection(token):
def compile_image(token):
img = markdown_to_html(token['data'])
return '''
<div class="kazimir__image">
{}
</div>
'''.format(img)
tpl = '''<div class="kazimir__image">{}</div>'''
return tpl.format(img)
def compile_url(token):
return compile_youtube(token)
def compile_youtube(token):
"""
<div class="kazimir-video">
<iframe src="http://www.youtube.com/embed/9otNWTHOJi8" frameborder="0" allowfullscreen></iframe>
</div>
"""
url = token['data']
tpl = '''<div class="kazimir-video"><iframe src="{url}" frameborder="0" allowfullscreen></iframe></div>'''
return tpl.format(url=url)
def compile_any(token):
@@ -246,4 +253,3 @@ More text
print(i)
else:
print(ts)
-38
View File
@@ -1,38 +0,0 @@
from markdown import Extension
from markdown.inlinepatterns import Pattern
from markdown.util import etree
class VideoExtension(Extension):
def add_inline(self, md, name, klass, re):
pattern = klass(re)
pattern.md = md
pattern.ext = self
md.inlinePatterns.add(name, pattern, "<reference")
def extendMarkdown(self, md, md_globals):
youtube = r'([^(]|^)https?://www\.youtube\.com/watch\?\S*v=(?P<youtubeargs>[A-Za-z0-9_&=-]+)\S*'
self.add_inline(md, 'youtube', Youtube, youtube)
class Youtube(Pattern):
def handleMatch(self, m):
url = 'https://www.youtube.com/embed/%s' % m.group('youtubeargs')
return flex_video(url)
def flex_video(url):
"""
<div class="kazimir-video">
<iframe src="http://www.youtube.com/embed/9otNWTHOJi8" frameborder="0" allowfullscreen></iframe>
</div>
"""
iframe = etree.Element('iframe')
iframe.set('src', url)
iframe.set('frameborder', '0')
iframe.set('allowfullscreen', '')
obj = etree.Element('div')
obj.set('class', "kazimir__video")
obj.append(iframe)
return obj