mirror of
https://github.com/maaslalani/slides.git
synced 2026-01-10 06:48:01 -05:00
be able to run multiple code blocks in a single slide
This commit is contained in:
committed by
Maas Lalani
parent
4e3be668eb
commit
68adc0a622
@@ -21,7 +21,7 @@ type Result struct {
|
||||
}
|
||||
|
||||
// ?: means non-capture group
|
||||
var re = regexp.MustCompile("(?s)(?:```|~~~)(\\w+)\n(.*)\n(?:```|~~~)\\s?")
|
||||
var re = regexp.MustCompile("(?s)(?:```|~~~)(\\w+)\n(.*?)\n(?:```|~~~)\\s?")
|
||||
|
||||
var (
|
||||
ErrParse = errors.New("Error: could not parse code block")
|
||||
@@ -29,19 +29,28 @@ var (
|
||||
|
||||
// Parse takes a block of markdown and returns an array of Block's with code
|
||||
// and associated languages
|
||||
func Parse(markdown string) (Block, error) {
|
||||
match := re.FindStringSubmatch(markdown)
|
||||
func Parse(markdown string) ([]Block, error) {
|
||||
matchs := re.FindAllStringSubmatch(markdown, -1)
|
||||
|
||||
var rv []Block
|
||||
for _, match := range matchs {
|
||||
// There was either no language specified or no code block
|
||||
// Either way, we cannot execute the expression
|
||||
if len(match) < 3 {
|
||||
continue
|
||||
}
|
||||
rv = append(rv, Block{
|
||||
Language: match[1],
|
||||
Code: match[2],
|
||||
})
|
||||
|
||||
// There was either no language specified or no code block
|
||||
// Either way, we cannot execute the expression
|
||||
if len(match) < 3 {
|
||||
return Block{}, ErrParse
|
||||
}
|
||||
|
||||
return Block{
|
||||
Language: match[1],
|
||||
Code: match[2],
|
||||
}, nil
|
||||
if len(rv) == 0 {
|
||||
return nil, ErrParse
|
||||
}
|
||||
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -113,15 +113,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
m.VirtualText = ""
|
||||
case "ctrl+e":
|
||||
// Run code block
|
||||
block, err := code.Parse(m.Slides[m.Page])
|
||||
// Run code blocks
|
||||
blocks, err := code.Parse(m.Slides[m.Page])
|
||||
if err != nil {
|
||||
// We couldn't parse the code block on the screen
|
||||
m.VirtualText = "\n" + err.Error()
|
||||
return m, nil
|
||||
}
|
||||
res := code.Execute(block)
|
||||
m.VirtualText = res.Out
|
||||
var outs []string
|
||||
for _, block := range blocks {
|
||||
res := code.Execute(block)
|
||||
outs = append(outs, res.Out)
|
||||
}
|
||||
m.VirtualText = strings.Join(outs, "\n")
|
||||
}
|
||||
|
||||
case fileWatchMsg:
|
||||
|
||||
Reference in New Issue
Block a user