2025-01-13 16:31:23 -03:00
|
|
|
// Copyright 2025-present Gustavo "Guz" L. de Mello
|
|
|
|
|
// Copyright 2025-present The Lored.dev Contributors
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
package plugins
|
2025-01-09 10:16:59 -03:00
|
|
|
|
|
|
|
|
import (
|
2025-01-09 10:41:02 -03:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2025-01-09 10:16:59 -03:00
|
|
|
"io"
|
2025-01-23 09:48:45 -03:00
|
|
|
"io/fs"
|
2025-01-22 19:35:44 -03:00
|
|
|
|
|
|
|
|
"forge.capytal.company/loreddev/x/blogo/plugin"
|
2025-01-09 10:16:59 -03:00
|
|
|
)
|
|
|
|
|
|
2025-01-22 19:35:44 -03:00
|
|
|
const plainTextName = "blogo-plaintext-renderer"
|
2025-01-13 16:37:52 -03:00
|
|
|
|
2025-01-13 16:38:53 -03:00
|
|
|
type painText struct{}
|
2025-01-09 10:22:00 -03:00
|
|
|
|
2025-01-23 14:54:35 -03:00
|
|
|
func NewPlainText() plugin.Renderer {
|
2025-01-13 16:38:53 -03:00
|
|
|
return &painText{}
|
2025-01-09 10:22:00 -03:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 16:38:53 -03:00
|
|
|
func (p *painText) Name() string {
|
2025-01-22 19:35:44 -03:00
|
|
|
return plainTextName
|
2025-01-13 09:32:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 16:38:53 -03:00
|
|
|
func (p *painText) Render(f fs.File, w io.Writer) error {
|
2025-01-09 10:41:02 -03:00
|
|
|
if d, ok := f.(fs.ReadDirFile); ok {
|
|
|
|
|
return p.renderDirectory(d, w)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
|
for {
|
|
|
|
|
n, err := f.Read(buf)
|
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = w.Write(buf[:n])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 16:38:53 -03:00
|
|
|
func (p *painText) renderDirectory(f fs.ReadDirFile, w io.Writer) error {
|
2025-01-09 10:41:02 -03:00
|
|
|
es, err := f.ReadDir(-1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, e := range es {
|
2025-01-09 11:05:45 -03:00
|
|
|
_, err := w.Write([]byte(fmt.Sprintf("%s\n", e.Name())))
|
2025-01-09 10:41:02 -03:00
|
|
|
if err != nil {
|
|
|
|
|
return errors.Join(
|
|
|
|
|
fmt.Errorf("failed to write directory file list, file %s", e.Name()),
|
|
|
|
|
err,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 10:22:00 -03:00
|
|
|
return nil
|
|
|
|
|
}
|