Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

post new entry when the pushed entry has no EditURL #91

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,10 @@ func (b *broker) FetchRemoteEntries() ([]*entry, error) {
return entries, nil
}

const entryExt = ".md" // TODO regard re.ContentType

func (b *broker) LocalPath(e *entry) string {
extension := ".md" // TODO regard re.ContentType
paths := []string{b.LocalRoot}
if b.OmitDomain == nil || !*b.OmitDomain {
paths = append(paths, b.BlogID)
}
// If possible, for fixed pages, we would like to dig a directory such as page/ to place md files,
// but it is difficult to solve by a simple method such as prepending a "page/" string
// if the path does not begin with an entry string. That is because if you are operating
// a subdirectory in Hatena Blog Media, you do not know where the root of the blog is.
// e.g.
// - https://example.com/subblog/entry/blog-entry
// - https://example.com/subblog/fixed-page
paths = append(paths, e.URL.Path+extension)
return filepath.Join(paths...)
return filepath.Join(b.localRoot(), e.URL.Path+entryExt)
}

func (b *broker) StoreFresh(e *entry, path string) (bool, error) {
Expand Down
21 changes: 21 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ type config struct {
Blogs map[string]*blogConfig
}

func (c *config) detectBlogConfig(fpath string) *blogConfig {
var retBc *blogConfig
for blogID := range c.Blogs {
bc := c.Get(blogID)
if bc.LocalRoot != "" && strings.HasPrefix(fpath, bc.localRoot()) {
if retBc == nil || len(bc.localRoot()) > len(retBc.localRoot()) {
retBc = bc
}
}
}
return retBc
}

type blogConfig struct {
BlogID string `yaml:"-"`
LocalRoot string `yaml:"local_root"`
Expand All @@ -23,6 +36,14 @@ type blogConfig struct {
Owner string `yaml:"owner"`
}

func (bc *blogConfig) localRoot() string {
paths := []string{bc.LocalRoot}
if bc.OmitDomain == nil || !*bc.OmitDomain {
paths = append(paths, bc.BlogID)
}
return filepath.Join(paths...)
}

func loadConfig(r io.Reader, fpath string) (*config, error) {
bytes, err := io.ReadAll(r)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@ var commandPush = &cli.Command{
return err
}

if entry.EditURL == "" {
// post new entry
if !filepath.IsAbs(path) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return err
}
}
bc := conf.detectBlogConfig(path)
if bc == nil {
return fmt.Errorf("cannot find blog for %q", path)
}
// The entry directory is not always at the top of the localRoot, such as
// in the case of using subdirectory feature in BlogMedia. Therefore, the
// relative position from the entry directory is obtained as a custom path as below.
blogPath, _ := filepath.Rel(bc.localRoot(), path)
blogPath = "/" + filepath.ToSlash(blogPath)
stuffs := strings.SplitN(blogPath, "/entry/", 2)
if len(stuffs) != 2 {
return fmt.Errorf("%q is not a blog entry", path)
}
entry.CustomPath = strings.TrimSuffix(stuffs[1], entryExt)
b := newBroker(bc)
err = b.PostEntry(entry, false)
if err != nil {
return err
}
continue
}
blogID, err := entry.blogID()
if err != nil {
return err
Expand Down
Loading