Commit graph

58 commits

Author SHA1 Message Date
Joe Mooring
7bf1abfc55 tpl/strings: Improve type checking 2024-04-04 18:34:55 +02:00
Joe Mooring
6624979e1b tpl/strings: Create strings.Diff template function
Closes #12330
2024-04-02 18:25:44 +02:00
Bjørn Erik Pedersen
a795acbcd8 all: Run gofumpt -l -w . 2024-01-28 23:14:09 +01:00
Bjørn Erik Pedersen
7285e74090
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.

Closes #11455
Closes #11549

This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.

The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.

A list of the notable new features:

* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.

New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.

This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):

Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.

Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).

Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2024-01-27 16:28:14 +01:00
Bjørn Erik Pedersen
4d7af757c9
Add a common regexp cache
```
BenchmarkGetOrCompileRegexp-10    	73959368	        13.71 ns/op	       0 B/op	       0 allocs/op
BenchmarkCompileRegexp-10         	 3143529	       380.1 ns/op	     872 B/op	      10 allocs/op
```
2023-07-27 19:23:12 +02:00
Bjørn Erik Pedersen
241b21b0fd Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code.

Also,

* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.

Closes #10896
Closes #10620
2023-05-16 18:01:29 +02:00
Joe Mooring
5c7b79cf7f tpl/strings: Clarify findRESubmatch description 2023-04-27 22:02:41 +02:00
Jonathan Fisher
d171d1543d tpl: Add hasSuffix alias
strings.HasPrefix already has an alias of hasPrefix
but strings.HasSuffix has no such alias.
This PR adds a hasSuffix alias to the tpl function with corresponding
function documentation.
It also adds a Minor update to the hasPrefix function documentation
re: keywords and relatedfuncs.

 Completes https://github.com/gohugoio/hugo/issues/10474
2023-03-08 10:18:34 +01:00
Khayyam Saleem
c0d15a2897
strings: fix Truncate behavior for formatted html
Before this fix, strings.Truncate would erroneously re-include
attributes from the opening tag in the closing tag when closing
formatted html, due to a bug in how tagnames were extracted from the
regex capture group for html tags used in `truncate.go`. This change
ensures that only the tagname is retained and all attributes are discarded
when storing the tags for closing them later.

Fixes #10399
2023-03-01 17:59:50 +01:00
Bjørn Erik Pedersen
2a61910e82
tpl/strings: Adjust benchmark 2023-03-01 17:59:02 +01:00
Bjørn Erik Pedersen
079d1b6540
tpl/strings: Add BenchmarkTruncate 2023-03-01 17:51:09 +01:00
Bjørn Erik Pedersen
ce524d0b5e Add a page template func
Fixes #9339
2023-02-25 19:53:18 +01:00
Bjørn Erik Pedersen
fce0890484 tpl/strings: Add strings.ContainsNonSpace 2023-02-05 20:08:44 +01:00
Bjørn Erik Pedersen
2fb40ece5d tpl/strings: Add findRESubmatch
Fixes #10594
2023-01-17 12:14:11 +01:00
Bjørn Erik Pedersen
e402d91ee1 Misc doc, code refactoring to improve documentation 2023-01-04 18:01:26 +01:00
Joe Mooring
b8d5c378ba tpl: Use consistent delimiter spacing in examples 2022-11-25 09:54:35 +01:00
Bjørn Erik Pedersen
3854a6fa6c Fix Plainify edge cases
This commit replaces the main part of `helpers.StripHTML` with Go's implementation in its html/template package.

It's a little slower, but correctness is more important:

```bash
BenchmarkStripHTMLOld-10    	  680316	      1764 ns/op	     728 B/op	       4 allocs/op
BenchmarkStripHTMLNew-10    	  384520	      3099 ns/op	    2089 B/op	      10 allocs/op
```

Fixes #9199
Fixes #9909
Closes #9410
2022-05-25 17:55:23 +02:00
Bjørn Erik Pedersen
f2946da9e8 Improve error messages, esp. when the server is running
* Add file context to minifier errors when publishing
* Misc fixes (see issues)
* Allow custom server error template in layouts/server/error.html

To get to this, this commit also cleans up and simplifies the code surrounding errors and files. This also removes the usage of `github.com/pkg/errors`, mostly because of https://github.com/pkg/errors/issues/223 -- but also because most of this is now built-in to Go.

Fixes #9852
Fixes #9857
Fixes #9863
2022-05-06 19:43:22 +02:00
Bjørn Erik Pedersen
6eea32bd6b tpl: Improve godoc 2022-05-06 19:43:22 +02:00
Bjørn Erik Pedersen
b80853de90
all: gofmt -w -r 'interface{} -> any' .
Updates #9687
2022-03-17 22:03:27 +01:00
Bjørn Erik Pedersen
10928a4f78 Remove the trailing new line in .Code
Fixes #9572
2022-02-26 21:54:36 +01:00
Bjørn Erik Pedersen
08fdca9d93 Add Markdown diagrams and render hooks for code blocks
You can now create custom hook templates for code blocks, either one for all (`render-codeblock.html`) or for a given code language (e.g. `render-codeblock-go.html`).

We also used this new hook to add support for diagrams in Hugo:

* Goat (Go ASCII Tool) is built-in and enabled by default; just create a fenced code block with the language `goat` and start draw your Ascii diagrams.
* Another popular alternative for diagrams in Markdown, Mermaid (supported by GitHub), can also be implemented with a simple template. See the Hugo documentation for more information.

Updates #7765
Closes #9538
Fixes #9553
Fixes #8520
Fixes #6702
Fixes #9558
2022-02-24 18:59:50 +01:00
Bjørn Erik Pedersen
6c70e1f22f Fix error handling for the time func alias
Fixes #8835
2021-08-01 13:39:30 +02:00
Bjørn Erik Pedersen
d392893cd7
Misc config loading fixes
The main motivation behind this is simplicity and correctnes, but the new small config library is also faster:

```
BenchmarkDefaultConfigProvider/Viper-16         	  252418	      4546 ns/op	    2720 B/op	      30 allocs/op
BenchmarkDefaultConfigProvider/Custom-16        	  450756	      2651 ns/op	    1008 B/op	       6 allocs/op
```

Fixes #8633
Fixes #8618
Fixes #8630
Updates #8591
Closes #6680
Closes #5192
2021-06-14 17:00:32 +02:00
Julien Midedji
7a2c10ae60
tpl: Fix countwords to handle special chars
Fixes #8479
2021-05-03 09:10:06 +02:00
Bjørn Erik Pedersen
7f8530039a
tpl: Add method mappings for strings.Contains, strings.ContainsAny
Just to confirm/document the function signature.
2021-02-23 09:36:43 +01:00
Cameron Moore
8a26ab0bc5 tpl: Do not return errors in substr for out-of-bounds cases
Most other substr implementations don't error out in out-of-bounds cases
but simply return an empty string (or a value that's printed as an empty
string). We'll follow their lead and not exit template execution.  Allow
the user decide what to do with the empty result.

Fixes #8113
2021-01-08 10:13:10 +01:00
Cameron Moore
788e50ad3a tpl: Add missing test scenario for strings.Substr 2021-01-08 10:13:10 +01:00
Phil Davis
04b89857e1
all: Fix minor typos 2020-12-16 12:11:32 +01:00
Bjørn Erik Pedersen
d90e37e0c6 all: Format code with gofumpt
See https://github.com/mvdan/gofumpt
2020-12-03 13:12:58 +01:00
Cameron Moore
5862fd2a60 tpl: Fix substr when length parameter is zero
When length parameter is zero, always return an empty string.

Updates #7993
2020-12-01 23:20:04 +01:00
Cameron Moore
64789fb5dc tpl: Refactor and fix substr logic
Fix miscalculations when start is negative.  Results should now match
PHP substr.

Fixes #7993
2020-11-28 11:53:46 +01:00
Cameron Moore
cdfd1c99ba tpl: Add limit support to replaceRE
Go stdlib doesn't contain a limited replace in the regexp package, but
we can accomplish the same thing with ReplaceAllStringFunc.

Fixes #7586
2020-08-28 18:57:56 +02:00
Cameron Moore
f9ebaaed1b tpl: Add limit option to replace template function
Updates #7586
2020-08-28 08:55:58 +02:00
Bjørn Erik Pedersen
028b356787
tpl/strings: Add strings.Count
Fixes #7453
2020-07-03 10:35:46 +02:00
Bjørn Erik Pedersen
167c01530b
Create lightweight forks of text/template and html/template
This commit also removes support for Ace and Amber templates.

Updates #6594
2019-12-12 09:59:34 +01:00
Bjørn Erik Pedersen
9e57182705
tests: Convert from testify to quicktest 2019-08-12 13:26:32 +02:00
Cameron Moore
30a7c9ea37 tpl: Add godoc packages comments
Also fix package name in tpl/templates.
2018-11-30 08:56:30 +01:00
Bjørn Erik Pedersen
35fbfb19a1
commands: Show server error info in browser
The main item in this commit is showing of errors with a file context when running `hugo server`.

This can be turned off: `hugo server --disableBrowserError` (can also be set in `config.toml`).

But to get there, the error handling in Hugo needed a revision. There are some items left TODO for commits soon to follow, most notable errors in content and config files.

Fixes #5284
Fixes #5290
See #5325
See #5324
2018-10-16 22:10:56 +02:00
Bjørn Erik Pedersen
e5d66074ce
tpl/strings: Add strings.FirstUpper
Fixes #5174
2018-09-07 09:08:14 +02:00
David E. Wheeler
019bd5576b tpl/strings: strings.RuneCount 2018-06-04 20:47:03 +03:00
Bjørn Erik Pedersen
0c6c98e401
tpl/strings: Remove overflow check in strings.Repeat
The test fails on 32 bit systems. Let it panic instead.
2018-06-03 23:23:48 +03:00
Bjørn Erik Pedersen
90c7749085
tpl/strings: Adjust the overflow validation in strings.Repeat
This now matches the validation in the stdlib, but we return an error instead.
2018-06-03 10:35:45 +03:00
David E. Wheeler
13435a6f60 tpl: Add strings.Repeat 2018-06-03 09:55:37 +03:00
Bjørn Erik Pedersen
02f2735f68
tpl/strings: Fix broken test 2017-12-17 18:06:55 +01:00
Victor Kropp
22cd89adc4 Make chomp return the type it receives
fixes #2187
2017-12-16 23:59:36 +01:00
Cameron Moore
29a2da0593 tpl: Cleanup strings.TrimPrefix and TrimSuffix
These funcs were added during the move to namespaces but were
undocumented.  This commit fixes the order of the arguments and adds the
funcs to the method mapping.
2017-08-18 14:21:58 +02:00
Cameron Moore
7674ad7382 tpl: Add strings.TrimLeft and TrimRight 2017-08-18 09:12:04 +02:00
Jorin Vogel
81c13171a9 Add some missing doc comments
As pointed out by the linter, some exported functions and types are
missing doc comments.
The linter warnings have been reduced from 194 to 116.
Not all missing comments have been added in this commit though.
2017-08-03 15:57:51 +02:00
Bjørn Erik Pedersen
8fb594bfb0 Make the title case style guide configurable
This works for the `title` func and the other places where Hugo makes title case.

* AP style (new default)
* Chicago style
* Go style (what we have today)

Fixes #989
2017-07-31 22:16:46 +02:00