time: relative_short(); fmt: handle $vweb.html()

pull/5351/head
Alexander Medvednikov 2020-06-11 20:26:46 +02:00
parent 3bf9b28773
commit 72fdb09e46
4 changed files with 42 additions and 12 deletions

View File

@ -189,6 +189,32 @@ fn since(t Time) int {
// relative returns a string representation of difference between time // relative returns a string representation of difference between time
// and current time. // and current time.
pub fn (t Time) relative() string { pub fn (t Time) relative() string {
now := time.now()
secs := now.unix - t.unix
if secs <= 30 {
// right now or in the future
// TODO handle time in the future
return 'now'
}
if secs < 60 {
return '1m'
}
if secs < 3600 {
return '${secs/60} minutes ago'
}
if secs < 3600 * 24 {
return '${secs/3600} hours ago'
}
if secs < 3600 * 24 * 5 {
return '${secs/3600/24} days ago'
}
if secs > 3600 * 24 * 10000 {
return ''
}
return t.md()
}
pub fn (t Time) relative_short() string {
now := time.now() now := time.now()
secs := now.unix - t.unix secs := now.unix - t.unix
if secs <= 30 { if secs <= 30 {

View File

@ -571,7 +571,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
ast.CharLiteral { ast.CharLiteral {
f.write('`$it.val`') f.write('`$it.val`')
} }
ast.ComptimeCall {} ast.ComptimeCall {
if it.is_vweb {
f.write('$' + 'vweb.html()')
}
}
ast.ConcatExpr { ast.ConcatExpr {
for i, val in it.vals { for i, val in it.vals {
if i != 0 { if i != 0 {

View File

@ -1369,7 +1369,7 @@ fn (mut p Parser) return_stmt() ast.Return {
} }
} }
const( const (
// modules which allow globals by default // modules which allow globals by default
global_enabled_mods = ['rand'] global_enabled_mods = ['rand']
) )
@ -1377,8 +1377,8 @@ const(
// left hand side of `=` or `:=` in `a,b,c := 1,2,3` // left hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (mut p Parser) global_decl() ast.GlobalDecl { fn (mut p Parser) global_decl() ast.GlobalDecl {
if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v && if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v &&
p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals &&
!p.pref.enable_globals && p.mod !in global_enabled_mods { !p.pref.is_fmt && p.mod !in global_enabled_mods {
p.error('use `v --enable-globals ...` to enable globals') p.error('use `v --enable-globals ...` to enable globals')
} }
start_pos := p.tok.position() start_pos := p.tok.position()

View File

@ -28,15 +28,15 @@ enum State {
pub fn compile_template(html_, fn_name string) string { pub fn compile_template(html_, fn_name string) string {
// lines := os.read_lines(path) // lines := os.read_lines(path)
mut html := html_ mut html := html_.trim_space()
mut header := '' mut header := ''
if os.exists('templates/header.html') && html.contains('@header') { if os.exists('templates/header.html') && html.contains('@header') {
h := os.read_file('templates/header.html') or { h := os.read_file('templates/header.html') or {
panic('reading file templates/header.html failed') panic('reading file templates/header.html failed')
} }
header = h.replace("\'", '"') header = h.trim_space().replace("\'", '"')
html = header + html html = header + html
} }
mut lines := html.split_into_lines() mut lines := html.split_into_lines()
mut s := strings.new_builder(1000) mut s := strings.new_builder(1000)