commit
						5d5a90d2b1
					
				
							
								
								
									
										214
									
								
								LICENSE.md
								
								
								
								
							
							
						
						
									
										214
									
								
								LICENSE.md
								
								
								
								
							| 
						 | 
				
			
			@ -1,21 +1,201 @@
 | 
			
		|||
The MIT License (MIT)
 | 
			
		||||
Apache License
 | 
			
		||||
Version 2.0, January 2004
 | 
			
		||||
http://www.apache.org/licenses/
 | 
			
		||||
 | 
			
		||||
Copyright (c) 2015 - 2016 Henrique Dias <hacdias@gmail.com>
 | 
			
		||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
 | 
			
		||||
 | 
			
		||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
in the Software without restriction, including without limitation the rights
 | 
			
		||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
furnished to do so, subject to the following conditions:
 | 
			
		||||
1. Definitions.
 | 
			
		||||
 | 
			
		||||
The above copyright notice and this permission notice shall be included in
 | 
			
		||||
all copies or substantial portions of the Software.
 | 
			
		||||
"License" shall mean the terms and conditions for use, reproduction,
 | 
			
		||||
and distribution as defined by Sections 1 through 9 of this document.
 | 
			
		||||
 | 
			
		||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
			
		||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
THE SOFTWARE.
 | 
			
		||||
"Licensor" shall mean the copyright owner or entity authorized by
 | 
			
		||||
the copyright owner that is granting the License.
 | 
			
		||||
 | 
			
		||||
"Legal Entity" shall mean the union of the acting entity and all
 | 
			
		||||
other entities that control, are controlled by, or are under common
 | 
			
		||||
control with that entity. For the purposes of this definition,
 | 
			
		||||
"control" means (i) the power, direct or indirect, to cause the
 | 
			
		||||
direction or management of such entity, whether by contract or
 | 
			
		||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
 | 
			
		||||
outstanding shares, or (iii) beneficial ownership of such entity.
 | 
			
		||||
 | 
			
		||||
"You" (or "Your") shall mean an individual or Legal Entity
 | 
			
		||||
exercising permissions granted by this License.
 | 
			
		||||
 | 
			
		||||
"Source" form shall mean the preferred form for making modifications,
 | 
			
		||||
including but not limited to software source code, documentation
 | 
			
		||||
source, and configuration files.
 | 
			
		||||
 | 
			
		||||
"Object" form shall mean any form resulting from mechanical
 | 
			
		||||
transformation or translation of a Source form, including but
 | 
			
		||||
not limited to compiled object code, generated documentation,
 | 
			
		||||
and conversions to other media types.
 | 
			
		||||
 | 
			
		||||
"Work" shall mean the work of authorship, whether in Source or
 | 
			
		||||
Object form, made available under the License, as indicated by a
 | 
			
		||||
copyright notice that is included in or attached to the work
 | 
			
		||||
(an example is provided in the Appendix below).
 | 
			
		||||
 | 
			
		||||
"Derivative Works" shall mean any work, whether in Source or Object
 | 
			
		||||
form, that is based on (or derived from) the Work and for which the
 | 
			
		||||
editorial revisions, annotations, elaborations, or other modifications
 | 
			
		||||
represent, as a whole, an original work of authorship. For the purposes
 | 
			
		||||
of this License, Derivative Works shall not include works that remain
 | 
			
		||||
separable from, or merely link (or bind by name) to the interfaces of,
 | 
			
		||||
the Work and Derivative Works thereof.
 | 
			
		||||
 | 
			
		||||
"Contribution" shall mean any work of authorship, including
 | 
			
		||||
the original version of the Work and any modifications or additions
 | 
			
		||||
to that Work or Derivative Works thereof, that is intentionally
 | 
			
		||||
submitted to Licensor for inclusion in the Work by the copyright owner
 | 
			
		||||
or by an individual or Legal Entity authorized to submit on behalf of
 | 
			
		||||
the copyright owner. For the purposes of this definition, "submitted"
 | 
			
		||||
means any form of electronic, verbal, or written communication sent
 | 
			
		||||
to the Licensor or its representatives, including but not limited to
 | 
			
		||||
communication on electronic mailing lists, source code control systems,
 | 
			
		||||
and issue tracking systems that are managed by, or on behalf of, the
 | 
			
		||||
Licensor for the purpose of discussing and improving the Work, but
 | 
			
		||||
excluding communication that is conspicuously marked or otherwise
 | 
			
		||||
designated in writing by the copyright owner as "Not a Contribution."
 | 
			
		||||
 | 
			
		||||
"Contributor" shall mean Licensor and any individual or Legal Entity
 | 
			
		||||
on behalf of whom a Contribution has been received by Licensor and
 | 
			
		||||
subsequently incorporated within the Work.
 | 
			
		||||
 | 
			
		||||
2. Grant of Copyright License. Subject to the terms and conditions of
 | 
			
		||||
this License, each Contributor hereby grants to You a perpetual,
 | 
			
		||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 | 
			
		||||
copyright license to reproduce, prepare Derivative Works of,
 | 
			
		||||
publicly display, publicly perform, sublicense, and distribute the
 | 
			
		||||
Work and such Derivative Works in Source or Object form.
 | 
			
		||||
 | 
			
		||||
3. Grant of Patent License. Subject to the terms and conditions of
 | 
			
		||||
this License, each Contributor hereby grants to You a perpetual,
 | 
			
		||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
 | 
			
		||||
(except as stated in this section) patent license to make, have made,
 | 
			
		||||
use, offer to sell, sell, import, and otherwise transfer the Work,
 | 
			
		||||
where such license applies only to those patent claims licensable
 | 
			
		||||
by such Contributor that are necessarily infringed by their
 | 
			
		||||
Contribution(s) alone or by combination of their Contribution(s)
 | 
			
		||||
with the Work to which such Contribution(s) was submitted. If You
 | 
			
		||||
institute patent litigation against any entity (including a
 | 
			
		||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
 | 
			
		||||
or a Contribution incorporated within the Work constitutes direct
 | 
			
		||||
or contributory patent infringement, then any patent licenses
 | 
			
		||||
granted to You under this License for that Work shall terminate
 | 
			
		||||
as of the date such litigation is filed.
 | 
			
		||||
 | 
			
		||||
4. Redistribution. You may reproduce and distribute copies of the
 | 
			
		||||
Work or Derivative Works thereof in any medium, with or without
 | 
			
		||||
modifications, and in Source or Object form, provided that You
 | 
			
		||||
meet the following conditions:
 | 
			
		||||
 | 
			
		||||
(a) You must give any other recipients of the Work or
 | 
			
		||||
Derivative Works a copy of this License; and
 | 
			
		||||
 | 
			
		||||
(b) You must cause any modified files to carry prominent notices
 | 
			
		||||
stating that You changed the files; and
 | 
			
		||||
 | 
			
		||||
(c) You must retain, in the Source form of any Derivative Works
 | 
			
		||||
that You distribute, all copyright, patent, trademark, and
 | 
			
		||||
attribution notices from the Source form of the Work,
 | 
			
		||||
excluding those notices that do not pertain to any part of
 | 
			
		||||
the Derivative Works; and
 | 
			
		||||
 | 
			
		||||
(d) If the Work includes a "NOTICE" text file as part of its
 | 
			
		||||
distribution, then any Derivative Works that You distribute must
 | 
			
		||||
include a readable copy of the attribution notices contained
 | 
			
		||||
within such NOTICE file, excluding those notices that do not
 | 
			
		||||
pertain to any part of the Derivative Works, in at least one
 | 
			
		||||
of the following places: within a NOTICE text file distributed
 | 
			
		||||
as part of the Derivative Works; within the Source form or
 | 
			
		||||
documentation, if provided along with the Derivative Works; or,
 | 
			
		||||
within a display generated by the Derivative Works, if and
 | 
			
		||||
wherever such third-party notices normally appear. The contents
 | 
			
		||||
of the NOTICE file are for informational purposes only and
 | 
			
		||||
do not modify the License. You may add Your own attribution
 | 
			
		||||
notices within Derivative Works that You distribute, alongside
 | 
			
		||||
or as an addendum to the NOTICE text from the Work, provided
 | 
			
		||||
that such additional attribution notices cannot be construed
 | 
			
		||||
as modifying the License.
 | 
			
		||||
 | 
			
		||||
You may add Your own copyright statement to Your modifications and
 | 
			
		||||
may provide additional or different license terms and conditions
 | 
			
		||||
for use, reproduction, or distribution of Your modifications, or
 | 
			
		||||
for any such Derivative Works as a whole, provided Your use,
 | 
			
		||||
reproduction, and distribution of the Work otherwise complies with
 | 
			
		||||
the conditions stated in this License.
 | 
			
		||||
 | 
			
		||||
5. Submission of Contributions. Unless You explicitly state otherwise,
 | 
			
		||||
any Contribution intentionally submitted for inclusion in the Work
 | 
			
		||||
by You to the Licensor shall be under the terms and conditions of
 | 
			
		||||
this License, without any additional terms or conditions.
 | 
			
		||||
Notwithstanding the above, nothing herein shall supersede or modify
 | 
			
		||||
the terms of any separate license agreement you may have executed
 | 
			
		||||
with Licensor regarding such Contributions.
 | 
			
		||||
 | 
			
		||||
6. Trademarks. This License does not grant permission to use the trade
 | 
			
		||||
names, trademarks, service marks, or product names of the Licensor,
 | 
			
		||||
except as required for reasonable and customary use in describing the
 | 
			
		||||
origin of the Work and reproducing the content of the NOTICE file.
 | 
			
		||||
 | 
			
		||||
7. Disclaimer of Warranty. Unless required by applicable law or
 | 
			
		||||
agreed to in writing, Licensor provides the Work (and each
 | 
			
		||||
Contributor provides its Contributions) on an "AS IS" BASIS,
 | 
			
		||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 | 
			
		||||
implied, including, without limitation, any warranties or conditions
 | 
			
		||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
 | 
			
		||||
PARTICULAR PURPOSE. You are solely responsible for determining the
 | 
			
		||||
appropriateness of using or redistributing the Work and assume any
 | 
			
		||||
risks associated with Your exercise of permissions under this License.
 | 
			
		||||
 | 
			
		||||
8. Limitation of Liability. In no event and under no legal theory,
 | 
			
		||||
whether in tort (including negligence), contract, or otherwise,
 | 
			
		||||
unless required by applicable law (such as deliberate and grossly
 | 
			
		||||
negligent acts) or agreed to in writing, shall any Contributor be
 | 
			
		||||
liable to You for damages, including any direct, indirect, special,
 | 
			
		||||
incidental, or consequential damages of any character arising as a
 | 
			
		||||
result of this License or out of the use or inability to use the
 | 
			
		||||
Work (including but not limited to damages for loss of goodwill,
 | 
			
		||||
work stoppage, computer failure or malfunction, or any and all
 | 
			
		||||
other commercial damages or losses), even if such Contributor
 | 
			
		||||
has been advised of the possibility of such damages.
 | 
			
		||||
 | 
			
		||||
9. Accepting Warranty or Additional Liability. While redistributing
 | 
			
		||||
the Work or Derivative Works thereof, You may choose to offer,
 | 
			
		||||
and charge a fee for, acceptance of support, warranty, indemnity,
 | 
			
		||||
or other liability obligations and/or rights consistent with this
 | 
			
		||||
License. However, in accepting such obligations, You may act only
 | 
			
		||||
on Your own behalf and on Your sole responsibility, not on behalf
 | 
			
		||||
of any other Contributor, and only if You agree to indemnify,
 | 
			
		||||
defend, and hold each Contributor harmless for any liability
 | 
			
		||||
incurred by, or claims asserted against, such Contributor by reason
 | 
			
		||||
of your accepting any such warranty or additional liability.
 | 
			
		||||
 | 
			
		||||
END OF TERMS AND CONDITIONS
 | 
			
		||||
 | 
			
		||||
APPENDIX: How to apply the Apache License to your work.
 | 
			
		||||
 | 
			
		||||
To apply the Apache License to your work, attach the following
 | 
			
		||||
boilerplate notice, with the fields enclosed by brackets "{}"
 | 
			
		||||
replaced with your own identifying information. (Don't include
 | 
			
		||||
the brackets!)  The text should be enclosed in the appropriate
 | 
			
		||||
comment syntax for the file format. We also recommend that a
 | 
			
		||||
file or class name and description of purpose be included on the
 | 
			
		||||
same "printed page" as the copyright notice for easier
 | 
			
		||||
identification within third-party archives.
 | 
			
		||||
 | 
			
		||||
Copyright {yyyy} {name of copyright owner}
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
				
			
			@ -168,7 +168,7 @@ $(document).on('page:browse', function() {
 | 
			
		|||
      });
 | 
			
		||||
 | 
			
		||||
      $.pjax({
 | 
			
		||||
        url: window.location.pathname.replace("browse", "edit") + filename,
 | 
			
		||||
        url: data.Location,
 | 
			
		||||
        container: '#content'
 | 
			
		||||
      })
 | 
			
		||||
    }).fail(function(data) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,10 +4,12 @@ import (
 | 
			
		|||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"mime/multipart"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/config"
 | 
			
		||||
| 
						 | 
				
			
			@ -47,55 +49,45 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
 | 
			
		|||
	filename := info["filename"].(string)
 | 
			
		||||
	filename = strings.TrimPrefix(filename, "/")
 | 
			
		||||
	filename = strings.TrimSuffix(filename, "/")
 | 
			
		||||
	url := "/admin/edit/" + r.URL.Path + filename
 | 
			
		||||
	filename = c.Path + r.URL.Path + filename
 | 
			
		||||
 | 
			
		||||
	// Check if the archetype is defined
 | 
			
		||||
	if info["archetype"] != "" {
 | 
			
		||||
		// Sanitize the archetype path
 | 
			
		||||
	if strings.HasPrefix(filename, c.Path+"content/") &&
 | 
			
		||||
		(strings.HasSuffix(filename, ".md") || strings.HasSuffix(filename, ".markdown")) {
 | 
			
		||||
 | 
			
		||||
		filename = strings.Replace(filename, c.Path+"content/", "", 1)
 | 
			
		||||
		args := []string{"new", filename}
 | 
			
		||||
		archetype := info["archetype"].(string)
 | 
			
		||||
		archetype = strings.Replace(archetype, "/archetypes", "", 1)
 | 
			
		||||
		archetype = strings.Replace(archetype, "archetypes", "", 1)
 | 
			
		||||
		archetype = strings.TrimPrefix(archetype, "/")
 | 
			
		||||
		archetype = strings.TrimSuffix(archetype, "/")
 | 
			
		||||
		archetype = c.Path + "archetypes/" + archetype
 | 
			
		||||
 | 
			
		||||
		// Check if the archetype ending with .markdown exists
 | 
			
		||||
		if _, err := os.Stat(archetype + ".markdown"); err == nil {
 | 
			
		||||
			err = utils.CopyFile(archetype+".markdown", filename)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return http.StatusInternalServerError, err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			w.Header().Set("Location", "/admin/edit/"+filename)
 | 
			
		||||
			w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
			w.Write([]byte("{}"))
 | 
			
		||||
			return 201, nil
 | 
			
		||||
		if archetype != "" {
 | 
			
		||||
			args = append(args, "--kind", archetype)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Check if the archetype ending with .md exists
 | 
			
		||||
		if _, err := os.Stat(archetype + ".md"); err == nil {
 | 
			
		||||
			err = utils.CopyFile(archetype+".md", filename)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return http.StatusInternalServerError, err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			w.Header().Set("Location", "/admin/edit/"+filename)
 | 
			
		||||
			w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
			w.Write([]byte("{}"))
 | 
			
		||||
			return 201, nil
 | 
			
		||||
		if err := utils.RunCommand(c.Hugo, args, c.Path); err != nil {
 | 
			
		||||
			return http.StatusInternalServerError, err
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		var err error
 | 
			
		||||
 | 
			
		||||
		if filepath.Ext(filename) == "" {
 | 
			
		||||
			err = os.MkdirAll(filename, 0755)
 | 
			
		||||
			url = strings.Replace(url, "edit", "browse", 1)
 | 
			
		||||
		} else {
 | 
			
		||||
			var wf *os.File
 | 
			
		||||
			wf, err = os.Create(filename)
 | 
			
		||||
			defer wf.Close()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return http.StatusInternalServerError, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	wf, err := os.Create(filename)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return http.StatusInternalServerError, err
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println(url)
 | 
			
		||||
 | 
			
		||||
	defer wf.Close()
 | 
			
		||||
 | 
			
		||||
	w.Header().Set("Location", "/admin/edit/"+filename)
 | 
			
		||||
	w.Header().Set("Content-Type", "application/json")
 | 
			
		||||
	w.Write([]byte("{}"))
 | 
			
		||||
	w.Write([]byte("{\"Location\": \"" + url + "\"}"))
 | 
			
		||||
	return http.StatusOK, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ package config
 | 
			
		|||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/insthugo"
 | 
			
		||||
	"github.com/mholt/caddy/caddy/setup"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -12,6 +13,7 @@ type Config struct {
 | 
			
		|||
	Path   string   // Hugo files path
 | 
			
		||||
	Styles string   // Admin styles path
 | 
			
		||||
	Args   []string // Hugo arguments
 | 
			
		||||
	Hugo   string   // Hugo executable path
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ParseHugo parses the configuration file
 | 
			
		||||
| 
						 | 
				
			
			@ -21,6 +23,8 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
 | 
			
		|||
		Path:   "./",
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conf.Hugo = insthugo.GetPath()
 | 
			
		||||
 | 
			
		||||
	for c.Next() {
 | 
			
		||||
		args := c.RemainingArgs()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -50,11 +54,10 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
 | 
			
		|||
					value = c.Val()
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				conf.Args = append(conf.Args, key, value)
 | 
			
		||||
				conf.Args = append(conf.Args, key+"="+value)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	conf.Args = append([]string{"--source", conf.Path}, conf.Args...)
 | 
			
		||||
	return conf, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -144,7 +144,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int
 | 
			
		|||
				return
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			utils.Run(c)
 | 
			
		||||
			go utils.Run(c, false)
 | 
			
		||||
		})
 | 
			
		||||
		scheduler.Start()
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										27
									
								
								hugo.go
								
								
								
								
							
							
						
						
									
										27
									
								
								hugo.go
								
								
								
								
							| 
						 | 
				
			
			@ -21,8 +21,6 @@ import (
 | 
			
		|||
	"github.com/hacdias/caddy-hugo/utils"
 | 
			
		||||
	"github.com/mholt/caddy/caddy/setup"
 | 
			
		||||
	"github.com/mholt/caddy/middleware"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	"github.com/spf13/hugo/commands"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Setup is the init function of Caddy plugins and it configures the whole
 | 
			
		||||
| 
						 | 
				
			
			@ -32,28 +30,29 @@ func Setup(c *setup.Controller) (middleware.Middleware, error) {
 | 
			
		|||
 | 
			
		||||
	// Checks if there is an Hugo website in the path that is provided.
 | 
			
		||||
	// If not, a new website will be created.
 | 
			
		||||
	create := false
 | 
			
		||||
	create := true
 | 
			
		||||
 | 
			
		||||
	if _, err := os.Stat(config.Path + "config.yaml"); os.IsNotExist(err) {
 | 
			
		||||
		create = true
 | 
			
		||||
	if _, err := os.Stat(config.Path + "config.yaml"); err == nil {
 | 
			
		||||
		create = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err := os.Stat(config.Path + "config.json"); os.IsNotExist(err) {
 | 
			
		||||
		create = true
 | 
			
		||||
	if _, err := os.Stat(config.Path + "config.json"); err == nil {
 | 
			
		||||
		create = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err := os.Stat(config.Path + "config.toml"); os.IsNotExist(err) {
 | 
			
		||||
		create = true
 | 
			
		||||
	if _, err := os.Stat(config.Path + "config.toml"); err == nil {
 | 
			
		||||
		create = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if create {
 | 
			
		||||
		cmd := &cobra.Command{}
 | 
			
		||||
		cmd.Flags().Bool("force", true, "")
 | 
			
		||||
		commands.NewSite(cmd, []string{config.Path})
 | 
			
		||||
		err := utils.RunCommand(config.Hugo, []string{"new", "site", config.Path, "--force"}, ".")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Panic(err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Generates the Hugo website for the first time the plugin is activated.
 | 
			
		||||
	utils.Run(config)
 | 
			
		||||
	go utils.Run(config, true)
 | 
			
		||||
 | 
			
		||||
	return func(next middleware.Handler) middleware.Handler {
 | 
			
		||||
		return &CaddyHugo{Next: next, Config: config}
 | 
			
		||||
| 
						 | 
				
			
			@ -139,7 +138,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
 | 
			
		|||
		// Whenever the header "X-Regenerate" is true, the website should be
 | 
			
		||||
		// regenerated. Used in edit and settings, for example.
 | 
			
		||||
		if r.Header.Get("X-Regenerate") == "true" {
 | 
			
		||||
			utils.Run(h.Config)
 | 
			
		||||
			go utils.Run(h.Config, false)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,288 @@
 | 
			
		|||
package insthugo
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"archive/zip"
 | 
			
		||||
	"compress/gzip"
 | 
			
		||||
	"crypto/sha256"
 | 
			
		||||
	"encoding/hex"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"os/user"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"runtime"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	version = "0.15"
 | 
			
		||||
	baseurl = "https://github.com/spf13/hugo/releases/download/v" + version + "/"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	caddy, bin, temp, hugo, tempfile, zipname, exename string
 | 
			
		||||
	sha256Hash                                         = map[string]string{
 | 
			
		||||
		"hugo_0.15_darwin_386.zip":              "f9b7353f9b64e7aece5f7981e5aa97dc4b31974ce76251edc070e77691bc03e2",
 | 
			
		||||
		"hugo_0.15_darwin_amd64.zip":            "aeecd6a12d86ab920f5b04e9486474bbe478dc246cdc2242799849b84c61c6f1",
 | 
			
		||||
		"hugo_0.15_dragonfly_amd64.zip":         "e380343789f2b2e0c366c8e1eeb251ccd90eea53dac191ff85d8177b130e53bc",
 | 
			
		||||
		"hugo_0.15_freebsd_386.zip":             "98f9210bfa3dcb48bd154879ea1cfe1b0ed8a3d891fdeacbdb4c3fc69b72aac4",
 | 
			
		||||
		"hugo_0.15_freebsd_amd64.zip":           "aa6a3028899e76e6920b9b5a64c29e14017ae34120efa67276e614e3a69cb100",
 | 
			
		||||
		"hugo_0.15_freebsd_arm.zip":             "de52e1b07caf778bdc3bdb07f39119cd5a1739c8822ebe311cd4f667c43588ac",
 | 
			
		||||
		"hugo_0.15_linux_386.tar.gz":            "af28c4cbb16db765535113f361a38b2249c634ce2d3798dcf5b795de6e4b7ecf",
 | 
			
		||||
		"hugo_0.15_linux_amd64.tar.gz":          "32a6335bd76f72867efdec9306a8a7eb7b9498a2e0478105efa96c1febadb09b",
 | 
			
		||||
		"hugo_0.15_linux_arm.tar.gz":            "886dd1a843c057a46c541011183dd558469250580e81450eedbd1a4d041e9234",
 | 
			
		||||
		"hugo_0.15_netbsd_386.zip":              "6245f5db16b33a09466f149d5b7b68a7899d6d624903de9e7e70c4b6ea869a72",
 | 
			
		||||
		"hugo_0.15_netbsd_amd64.zip":            "103ea8d81d2a3d707c05e3dd68c98fcf8146ddd36b49bf0e65d9874cee230c88",
 | 
			
		||||
		"hugo_0.15_netbsd_arm.zip":              "9c9b5cf4ea3b6169be1b5fc924251a247d9c140dd8a45aa5175031878585ff0a",
 | 
			
		||||
		"hugo_0.15_openbsd_386.zip":             "81dfdb3048a27a61b249650241fe4e8da1eda31a3a7311c615eb419f1cdd06b1",
 | 
			
		||||
		"hugo_0.15_openbsd_amd64.zip":           "e7447cde0dd7628b05b25b86938018774d8db8156ab1330b364e0e2c6501ad87",
 | 
			
		||||
		"hugo_0.15_windows_386_32-bit-only.zip": "0a72f9a1a929f36c0e52fb1c6272b4d37a2bd1a6bd19ce57a6e7b6803b434756",
 | 
			
		||||
		"hugo_0.15_windows_amd64.zip":           "9f03602e48ae2199e06431d7436fb3b9464538c0d44aac9a76eb98e1d4d5d727",
 | 
			
		||||
	}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// GetPath retrives the Hugo path for the user or install it if it's not found
 | 
			
		||||
func GetPath() string {
 | 
			
		||||
	initializeVariables()
 | 
			
		||||
 | 
			
		||||
	// Check if Hugo is already on $PATH
 | 
			
		||||
	if hugo, err := exec.LookPath("hugo"); err == nil {
 | 
			
		||||
		return hugo
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if Hugo is on $HOME/.caddy/bin
 | 
			
		||||
	if _, err := os.Stat(hugo); err == nil {
 | 
			
		||||
		return hugo
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Unable to find Hugo on your computer.")
 | 
			
		||||
 | 
			
		||||
	// Create the neccessary folders
 | 
			
		||||
	os.MkdirAll(caddy, 0774)
 | 
			
		||||
	os.Mkdir(bin, 0774)
 | 
			
		||||
	os.Mkdir(temp, 0774)
 | 
			
		||||
 | 
			
		||||
	downloadHugo()
 | 
			
		||||
	checkSHA256()
 | 
			
		||||
 | 
			
		||||
	fmt.Print("Unziping... ")
 | 
			
		||||
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	// Unzip or Ungzip the file
 | 
			
		||||
	switch runtime.GOOS {
 | 
			
		||||
	case "darwin", "windows":
 | 
			
		||||
		err = unzip(tempfile, temp)
 | 
			
		||||
	default:
 | 
			
		||||
		err = ungzip(tempfile, temp)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("done.")
 | 
			
		||||
 | 
			
		||||
	var exetorename string
 | 
			
		||||
 | 
			
		||||
	err = filepath.Walk(temp, func(path string, f os.FileInfo, err error) error {
 | 
			
		||||
		if f.Name() == exename {
 | 
			
		||||
			exetorename = path
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return nil
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	err = os.Rename(exetorename, hugo)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Hugo installed at " + hugo)
 | 
			
		||||
	clean()
 | 
			
		||||
	return hugo
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func initializeVariables() {
 | 
			
		||||
	exename = "hugo_" + version + "_" + runtime.GOOS + "_" + runtime.GOARCH
 | 
			
		||||
	zipname = exename
 | 
			
		||||
 | 
			
		||||
	usr, err := user.Current()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	caddy = filepath.Join(usr.HomeDir, ".caddy")
 | 
			
		||||
	bin = filepath.Join(caddy, "bin")
 | 
			
		||||
	temp = filepath.Join(caddy, "temp")
 | 
			
		||||
	hugo = filepath.Join(bin, "hugo")
 | 
			
		||||
 | 
			
		||||
	switch runtime.GOOS {
 | 
			
		||||
	case "darwin":
 | 
			
		||||
		zipname += ".zip"
 | 
			
		||||
	case "windows":
 | 
			
		||||
		// At least for v0.15 version
 | 
			
		||||
		if runtime.GOARCH == "386" {
 | 
			
		||||
			zipname += "32-bit-only"
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		zipname += ".zip"
 | 
			
		||||
		exename += ".exe"
 | 
			
		||||
		hugo += ".exe"
 | 
			
		||||
	default:
 | 
			
		||||
		zipname += ".tar.gz"
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func downloadHugo() {
 | 
			
		||||
	tempfile = filepath.Join(temp, zipname)
 | 
			
		||||
 | 
			
		||||
	fmt.Print("Downloading Hugo from GitHub releases... ")
 | 
			
		||||
 | 
			
		||||
	// Create the file
 | 
			
		||||
	out, err := os.Create(tempfile)
 | 
			
		||||
	out.Chmod(0774)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		clean()
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
	defer out.Close()
 | 
			
		||||
 | 
			
		||||
	// Get the data
 | 
			
		||||
	resp, err := http.Get(baseurl + zipname)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("An error ocurred while downloading. If this error persists, try downloading Hugo from \"https://github.com/spf13/hugo/releases/\" and put the executable in " + bin + " and rename it to 'hugo' or 'hugo.exe' if you're on Windows.")
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	// Writer the body to file
 | 
			
		||||
	_, err = io.Copy(out, resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("downloaded.")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func checkSHA256() {
 | 
			
		||||
	fmt.Print("Checking SHA256...")
 | 
			
		||||
 | 
			
		||||
	hasher := sha256.New()
 | 
			
		||||
	f, err := os.Open(tempfile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
	if _, err := io.Copy(hasher, f); err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if hex.EncodeToString(hasher.Sum(nil)) != sha256Hash[zipname] {
 | 
			
		||||
		fmt.Println("can't verify SHA256.")
 | 
			
		||||
		os.Exit(-1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("checked!")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func unzip(src, dest string) error {
 | 
			
		||||
	r, err := zip.OpenReader(src)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := r.Close(); err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	os.MkdirAll(dest, 0755)
 | 
			
		||||
 | 
			
		||||
	// Closure to address file descriptors issue with all the deferred .Close() methods
 | 
			
		||||
	extractAndWriteFile := func(f *zip.File) error {
 | 
			
		||||
		rc, err := f.Open()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		defer func() {
 | 
			
		||||
			if err := rc.Close(); err != nil {
 | 
			
		||||
				panic(err)
 | 
			
		||||
			}
 | 
			
		||||
		}()
 | 
			
		||||
 | 
			
		||||
		path := filepath.Join(dest, f.Name)
 | 
			
		||||
 | 
			
		||||
		if f.FileInfo().IsDir() {
 | 
			
		||||
			os.MkdirAll(path, f.Mode())
 | 
			
		||||
		} else {
 | 
			
		||||
			if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
 | 
			
		||||
				os.MkdirAll(filepath.Dir(path), 0755)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			defer func() {
 | 
			
		||||
				if err := f.Close(); err != nil {
 | 
			
		||||
					panic(err)
 | 
			
		||||
				}
 | 
			
		||||
			}()
 | 
			
		||||
			_, err = io.Copy(f, rc)
 | 
			
		||||
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, f := range r.File {
 | 
			
		||||
		err := extractAndWriteFile(f)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ungzip(source, target string) error {
 | 
			
		||||
	reader, err := os.Open(source)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer reader.Close()
 | 
			
		||||
 | 
			
		||||
	archive, err := gzip.NewReader(reader)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer archive.Close()
 | 
			
		||||
 | 
			
		||||
	target = filepath.Join(target, archive.Name)
 | 
			
		||||
	writer, err := os.Create(target)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer writer.Close()
 | 
			
		||||
 | 
			
		||||
	_, err = io.Copy(writer, archive)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func clean() {
 | 
			
		||||
	fmt.Print("Removing temporary files... ")
 | 
			
		||||
	os.RemoveAll(temp)
 | 
			
		||||
	fmt.Println("done.")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -6,6 +6,7 @@ import (
 | 
			
		|||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"text/template"
 | 
			
		||||
| 
						 | 
				
			
			@ -13,8 +14,6 @@ import (
 | 
			
		|||
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/assets"
 | 
			
		||||
	"github.com/hacdias/caddy-hugo/config"
 | 
			
		||||
	"github.com/spf13/hugo/commands"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// CanBeEdited checks if the extension of a file is supported by the editor
 | 
			
		||||
| 
						 | 
				
			
			@ -165,17 +164,43 @@ func ParseComponents(r *http.Request) []string {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Run is used to run the static website generator
 | 
			
		||||
func Run(c *config.Config) {
 | 
			
		||||
func Run(c *config.Config, force bool) {
 | 
			
		||||
	os.RemoveAll(c.Path + "public")
 | 
			
		||||
 | 
			
		||||
	commands.MainSite = nil
 | 
			
		||||
	viper.Reset()
 | 
			
		||||
	commands.HugoCmd.ParseFlags(c.Args)
 | 
			
		||||
	if err := commands.HugoCmd.RunE(nil, nil); err != nil {
 | 
			
		||||
	// Prevent running if watching is enabled
 | 
			
		||||
	if b, pos := stringInSlice("--watch", c.Args); b && !force {
 | 
			
		||||
		if len(c.Args) > pos && c.Args[pos+1] != "false" {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if len(c.Args) == pos+1 {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := RunCommand(c.Hugo, c.Args, c.Path); err != nil {
 | 
			
		||||
		log.Panic(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RunCommand executes an external command
 | 
			
		||||
func RunCommand(command string, args []string, path string) error {
 | 
			
		||||
	cmd := exec.Command(command, args...)
 | 
			
		||||
	cmd.Dir = path
 | 
			
		||||
	cmd.Stdout = os.Stderr
 | 
			
		||||
	cmd.Stderr = os.Stderr
 | 
			
		||||
	return cmd.Run()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func stringInSlice(a string, list []string) (bool, int) {
 | 
			
		||||
	for i, b := range list {
 | 
			
		||||
		if b == a {
 | 
			
		||||
			return true, i
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false, 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var splitCapitalizeExceptions = map[string]string{
 | 
			
		||||
	"youtube":    "YouTube",
 | 
			
		||||
	"github":     "GitHub",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue