This commit is contained in:
yumoqing 2021-01-27 17:39:48 +08:00
parent a817b5e1e9
commit 9a6e7fe45d
5 changed files with 169 additions and 4 deletions

View File

@ -165,6 +165,12 @@ class Text(Label):
if self.bgcolor: if self.bgcolor:
self.color = self.bgcolor self.color = self.bgcolor
def get_wraped_size(self):
if self.text:
self._label.refresh()
return self._label.size
return (None,None)
def on_size(self,o,size): def on_size(self,o,size):
# super().on_size(o,size) # super().on_size(o,size)
if self.wrap: if self.wrap:
@ -345,7 +351,7 @@ class HTTPDataHandler(EventDispatcher):
def getDataHandler(url,**kwargs): def getDataHandler(url,**kwargs):
if url.startswith('file://'): if url.startswith('file://'):
return FILEDataHandler(url,**kwargs) return FILEDataHandler(url,**kwargs)
return HTTPDataHandler(url, **kwaegs) return HTTPDataHandler(url, **kwargs)
def device_type(): def device_type():
if platform != "android" and platform != "ios": if platform != "android" and platform != "ios":

157
kivyblocks/markdown.py Normal file
View File

@ -0,0 +1,157 @@
from kivy.properties import StringProperty
from kivy.factory import Factory
from kivy.clock import Clock
from .baseWidget import ScrollWidget, getDataHandler
from .utils import CSize
import re
class Markdown(ScrollWidget):
"""
# Markdown
MArkdown widget using to render a markdown file and show it
description file format
{
"widgettype":"Markdown",
"options":{
"source": the markdown file
other options
}
}
"""
source = StringProperty(None)
def __init__(self, **kw):
ScrollWidget.__init__(self, **kw)
self.bind(source=self.load_text)
if self.source:
Clock.schedule_once(self.load_text, 0.3)
def load_text(self, *args):
print('source fired, hahaha', *args)
self.clear_widgets()
h = getDataHandler(self.source)
h.bind(on_success=self.update)
h.bind(on_error=self.show_error)
h.handle()
def show_error(self, o, e):
print('load_text(), error=',e)
def update(self, o, text):
print('text=',text, type(text))
if not text:
return
for l in text.split('\n'):
l = ''.join([i for i in l if i != '\r'])
self.parse_line(l)
def parse_title(self,txt, level):
w = Factory.Blocks().widgetBuild({
"widgettype":f"Title{level}",
"options":{
"text":txt,
"size_hint_x":None,
"width":self.width,
"size_hint_y":None,
"markup":True,
"wrap":True,
"halign":"left",
"valign":"middle"
}
})
_,h = w.get_wraped_size()
clen = CSize(1)
if h is None or h < clen:
h = clen
w.height = h
print(w,h,w.height)
w.bind(on_ref_press=self.open_new_md)
self.add_widget(w)
def parse_line(self, l):
if l.startswith('###### '):
t = self.mktext_bbtext(l[7:])
return self.parse_title(t,6)
if l.startswith('##### '):
t = self.mktext2bbtext(l[6:])
return self.parse_title(t,5)
if l.startswith('#### '):
t = self.mktext2bbtext(l[5:])
return self.parse_title(t,4)
if l.startswith('### '):
t = self.mktext2bbtext(l[4:])
return self.parse_title(t,3)
if l.startswith('## '):
t = self.mktext2bbtext(l[3:])
return self.parse_title(t,2)
if l.startswith('# '):
t = self.mktext2bbtext(l[2:])
return self.parse_title(t,1)
t = self.mktext2bbtext(l)
w = Factory.Blocks().widgetBuild({
"widgettype":"Text",
"options":{
"text":t,
"wrap":True,
"size_hint_x":None,
"width":self.width,
"markup":True,
"valign":"middle",
"halign":"left"
}
})
_,h = w.get_wraped_size()
clen = CSize(1)
if h is None or h < clen:
h = clen
w.height = h
w.bind(on_ref_press=self.open_new_md)
self.add_widget(w)
def open_new_md(self, o, value):
print(value,'is it a link')
self.source = value
def mktext2bbtext(self,mdtext):
"""
convert markdown text to bbtag text kivy Label Widget recognized
markdown syntax
*XXX* Italic
**XXX** Bold
***XXX*** Bold + Italic
> XXXX reference
>> XXXX reference inside reference
[-*] XXX list
[0-9]*[.] XXX list
*** split line
--- split line
___ split line
[.*](url) link
![.*](url) image
[![.*](url)](url) image link
!v[.*](url) video* plan to extend
!a[.*](url) audio* plan to extend
bb tag syntax
[b]XXX[/b] Bold
[i]XXX[/i] Italic
[sub]XXX[/sub]
"""
mdtext = re.sub('\*\*\*(.*)\*\*\*', \
lambda x: '[b][i]'+x.group(1)+'[/i][/b]', \
mdtext)
mdtext = re.sub('\*\*(.*)\*\*', \
lambda x: '[b]'+x.group(1)+'[/b]', \
mdtext)
mdtext = re.sub('\*(.*)\*', \
lambda x: '[i]'+x.group(1)+'[/i]', \
mdtext)
mdtext = re.sub('([^!]?)\[(.*)\]\((.*)\)', \
lambda x:x.group(1) + '[ref='+x.group(3)+'][color=00ff00]'+ x.group(2)+'[/color][/ref]', \
mdtext)
return mdtext

View File

@ -232,7 +232,7 @@ sub-widget's description file format
return True return True
def pop(self, o, *args): def pop(self, o, *args):
if len(self.sub_widgets) < 1: if len(self.sub_widgets) < 2:
return return
self.clear_widgets() self.clear_widgets()
diss_w = self.sub_widgets[-1] diss_w = self.sub_widgets[-1]

View File

@ -23,8 +23,10 @@ from .mapview import MapView
from .chart2d import Chart2d from .chart2d import Chart2d
from .message import Conform from .message import Conform
from .pagepanel import PagePanel from .pagepanel import PagePanel
from .markdown import Markdown
r = Factory.register r = Factory.register
r('Markdown', Markdown)
r('PagePanel', PagePanel) r('PagePanel', PagePanel)
r('Conform', Conform) r('Conform', Conform)
r('Chart2d', Chart2d) r('Chart2d', Chart2d)

View File

@ -5,9 +5,9 @@ from kivy.graphics import Color, Ellipse,Rectangle
class ScrollWidget(ScrollView): class ScrollWidget(ScrollView):
def __init__(self,**kw): def __init__(self,**kw):
self._inner = BoxLayout(orientation='vertical',padding=2,
spacing=2,size_hint=(None,None))
super(ScrollWidget,self).__init__(**kw) super(ScrollWidget,self).__init__(**kw)
self._inner = BoxLayout(orientation='vertical',padding=5,
spacing=8,size_hint=(None,None))
self._inner.bind( self._inner.bind(
minimum_height=self._inner.setter('height')) minimum_height=self._inner.setter('height'))
self._inner.bind( self._inner.bind(