github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/resources/page/testhelpers_test.go (about)

     1  // Copyright 2023 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package page
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"html/template"
    20  	"path"
    21  	"path/filepath"
    22  	"time"
    23  
    24  	"github.com/gohugoio/hugo/hugofs/files"
    25  	"github.com/gohugoio/hugo/identity"
    26  	"github.com/gohugoio/hugo/markup/tableofcontents"
    27  	"github.com/gohugoio/hugo/tpl"
    28  
    29  	"github.com/gohugoio/hugo/resources/resource"
    30  
    31  	"github.com/gohugoio/hugo/navigation"
    32  
    33  	"github.com/gohugoio/hugo/common/hugo"
    34  	"github.com/gohugoio/hugo/common/maps"
    35  	"github.com/gohugoio/hugo/config"
    36  	"github.com/gohugoio/hugo/hugofs"
    37  	"github.com/gohugoio/hugo/langs"
    38  	"github.com/gohugoio/hugo/media"
    39  	"github.com/gohugoio/hugo/related"
    40  
    41  	"github.com/gohugoio/hugo/source"
    42  )
    43  
    44  var (
    45  	_ resource.LengthProvider = (*testPage)(nil)
    46  	_ Page                    = (*testPage)(nil)
    47  )
    48  
    49  var relatedDocsHandler = NewRelatedDocsHandler(related.DefaultConfig)
    50  
    51  func newTestPage() *testPage {
    52  	return newTestPageWithFile("/a/b/c.md")
    53  }
    54  
    55  func newTestPageWithFile(filename string) *testPage {
    56  	filename = filepath.FromSlash(filename)
    57  	file := source.NewTestFile(filename)
    58  
    59  	l, err := langs.NewLanguage(
    60  		"en",
    61  		"en",
    62  		"UTC",
    63  		langs.LanguageConfig{
    64  			LanguageName: "English",
    65  		},
    66  	)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  
    71  	return &testPage{
    72  		params: make(map[string]any),
    73  		data:   make(map[string]any),
    74  		file:   file,
    75  		currentSection: &testPage{
    76  			sectionEntries: []string{"a", "b", "c"},
    77  		},
    78  		site: testSite{l: l},
    79  	}
    80  }
    81  
    82  type testPage struct {
    83  	kind        string
    84  	description string
    85  	title       string
    86  	linkTitle   string
    87  	lang        string
    88  	section     string
    89  	site        testSite
    90  
    91  	content string
    92  
    93  	fuzzyWordCount int
    94  
    95  	path string
    96  
    97  	slug string
    98  
    99  	// Dates
   100  	date       time.Time
   101  	lastMod    time.Time
   102  	expiryDate time.Time
   103  	pubDate    time.Time
   104  
   105  	weight int
   106  
   107  	params map[string]any
   108  	data   map[string]any
   109  
   110  	file source.File
   111  
   112  	currentSection *testPage
   113  	sectionEntries []string
   114  }
   115  
   116  func (p *testPage) Err() resource.ResourceError {
   117  	return nil
   118  }
   119  
   120  func (p *testPage) Aliases() []string {
   121  	panic("testpage: not implemented")
   122  }
   123  
   124  func (p *testPage) AllTranslations() Pages {
   125  	panic("testpage: not implemented")
   126  }
   127  
   128  func (p *testPage) AlternativeOutputFormats() OutputFormats {
   129  	panic("testpage: not implemented")
   130  }
   131  
   132  func (p *testPage) Author() Author {
   133  	return Author{}
   134  }
   135  
   136  func (p *testPage) Authors() AuthorList {
   137  	return nil
   138  }
   139  
   140  func (p *testPage) BaseFileName() string {
   141  	panic("testpage: not implemented")
   142  }
   143  
   144  func (p *testPage) BundleType() files.ContentClass {
   145  	panic("testpage: not implemented")
   146  }
   147  
   148  func (p *testPage) Content(context.Context) (any, error) {
   149  	panic("testpage: not implemented")
   150  }
   151  
   152  func (p *testPage) ContentBaseName() string {
   153  	panic("testpage: not implemented")
   154  }
   155  
   156  func (p *testPage) CurrentSection() Page {
   157  	return p.currentSection
   158  }
   159  
   160  func (p *testPage) Data() any {
   161  	return p.data
   162  }
   163  
   164  func (p *testPage) Sitemap() config.SitemapConfig {
   165  	return config.SitemapConfig{}
   166  }
   167  
   168  func (p *testPage) Layout() string {
   169  	return ""
   170  }
   171  
   172  func (p *testPage) Date() time.Time {
   173  	return p.date
   174  }
   175  
   176  func (p *testPage) Description() string {
   177  	return ""
   178  }
   179  
   180  func (p *testPage) Dir() string {
   181  	panic("testpage: not implemented")
   182  }
   183  
   184  func (p *testPage) Draft() bool {
   185  	panic("testpage: not implemented")
   186  }
   187  
   188  func (p *testPage) Eq(other any) bool {
   189  	return p == other
   190  }
   191  
   192  func (p *testPage) ExpiryDate() time.Time {
   193  	return p.expiryDate
   194  }
   195  
   196  func (p *testPage) Ext() string {
   197  	panic("testpage: not implemented")
   198  }
   199  
   200  func (p *testPage) Extension() string {
   201  	panic("testpage: not implemented")
   202  }
   203  
   204  func (p *testPage) File() source.File {
   205  	return p.file
   206  }
   207  
   208  func (p *testPage) FileInfo() hugofs.FileMetaInfo {
   209  	panic("testpage: not implemented")
   210  }
   211  
   212  func (p *testPage) Filename() string {
   213  	panic("testpage: not implemented")
   214  }
   215  
   216  func (p *testPage) FirstSection() Page {
   217  	panic("testpage: not implemented")
   218  }
   219  
   220  func (p *testPage) FuzzyWordCount(context.Context) int {
   221  	return p.fuzzyWordCount
   222  }
   223  
   224  func (p *testPage) GetPage(ref string) (Page, error) {
   225  	panic("testpage: not implemented")
   226  }
   227  
   228  func (p *testPage) GetPageWithTemplateInfo(info tpl.Info, ref string) (Page, error) {
   229  	panic("testpage: not implemented")
   230  }
   231  
   232  func (p *testPage) GetParam(key string) any {
   233  	panic("testpage: not implemented")
   234  }
   235  
   236  func (p *testPage) GetTerms(taxonomy string) Pages {
   237  	panic("testpage: not implemented")
   238  }
   239  
   240  func (p *testPage) GetRelatedDocsHandler() *RelatedDocsHandler {
   241  	return relatedDocsHandler
   242  }
   243  
   244  func (p *testPage) GitInfo() source.GitInfo {
   245  	return source.GitInfo{}
   246  }
   247  
   248  func (p *testPage) CodeOwners() []string {
   249  	return nil
   250  }
   251  
   252  func (p *testPage) HasMenuCurrent(menuID string, me *navigation.MenuEntry) bool {
   253  	panic("testpage: not implemented")
   254  }
   255  
   256  func (p *testPage) HasShortcode(name string) bool {
   257  	panic("testpage: not implemented")
   258  }
   259  
   260  func (p *testPage) Hugo() hugo.HugoInfo {
   261  	panic("testpage: not implemented")
   262  }
   263  
   264  func (p *testPage) InSection(other any) (bool, error) {
   265  	panic("testpage: not implemented")
   266  }
   267  
   268  func (p *testPage) IsAncestor(other any) (bool, error) {
   269  	panic("testpage: not implemented")
   270  }
   271  
   272  func (p *testPage) IsDescendant(other any) (bool, error) {
   273  	panic("testpage: not implemented")
   274  }
   275  
   276  func (p *testPage) IsDraft() bool {
   277  	return false
   278  }
   279  
   280  func (p *testPage) IsHome() bool {
   281  	panic("testpage: not implemented")
   282  }
   283  
   284  func (p *testPage) IsMenuCurrent(menuID string, inme *navigation.MenuEntry) bool {
   285  	panic("testpage: not implemented")
   286  }
   287  
   288  func (p *testPage) IsNode() bool {
   289  	panic("testpage: not implemented")
   290  }
   291  
   292  func (p *testPage) IsPage() bool {
   293  	panic("testpage: not implemented")
   294  }
   295  
   296  func (p *testPage) IsSection() bool {
   297  	panic("testpage: not implemented")
   298  }
   299  
   300  func (p *testPage) IsTranslated() bool {
   301  	panic("testpage: not implemented")
   302  }
   303  
   304  func (p *testPage) Keywords() []string {
   305  	return nil
   306  }
   307  
   308  func (p *testPage) Kind() string {
   309  	return p.kind
   310  }
   311  
   312  func (p *testPage) Lang() string {
   313  	return p.lang
   314  }
   315  
   316  func (p *testPage) Language() *langs.Language {
   317  	panic("testpage: not implemented")
   318  }
   319  
   320  func (p *testPage) LanguagePrefix() string {
   321  	return ""
   322  }
   323  
   324  func (p *testPage) Fragments(context.Context) *tableofcontents.Fragments {
   325  	return nil
   326  }
   327  
   328  func (p *testPage) HeadingsFiltered(context.Context) tableofcontents.Headings {
   329  	return nil
   330  }
   331  
   332  func (p *testPage) Lastmod() time.Time {
   333  	return p.lastMod
   334  }
   335  
   336  func (p *testPage) Len(context.Context) int {
   337  	return len(p.content)
   338  }
   339  
   340  func (p *testPage) LinkTitle() string {
   341  	if p.linkTitle == "" {
   342  		if p.title == "" {
   343  			return p.path
   344  		}
   345  		return p.title
   346  	}
   347  	return p.linkTitle
   348  }
   349  
   350  func (p *testPage) LogicalName() string {
   351  	panic("testpage: not implemented")
   352  }
   353  
   354  func (p *testPage) MediaType() media.Type {
   355  	panic("testpage: not implemented")
   356  }
   357  
   358  func (p *testPage) Menus() navigation.PageMenus {
   359  	return navigation.PageMenus{}
   360  }
   361  
   362  func (p *testPage) Name() string {
   363  	panic("testpage: not implemented")
   364  }
   365  
   366  func (p *testPage) Next() Page {
   367  	panic("testpage: not implemented")
   368  }
   369  
   370  func (p *testPage) NextInSection() Page {
   371  	return nil
   372  }
   373  
   374  func (p *testPage) NextPage() Page {
   375  	return nil
   376  }
   377  
   378  func (p *testPage) OutputFormats() OutputFormats {
   379  	panic("testpage: not implemented")
   380  }
   381  
   382  func (p *testPage) Pages() Pages {
   383  	panic("testpage: not implemented")
   384  }
   385  
   386  func (p *testPage) RegularPages() Pages {
   387  	panic("testpage: not implemented")
   388  }
   389  
   390  func (p *testPage) RegularPagesRecursive() Pages {
   391  	panic("testpage: not implemented")
   392  }
   393  
   394  func (p *testPage) Paginate(seq any, options ...any) (*Pager, error) {
   395  	return nil, nil
   396  }
   397  
   398  func (p *testPage) Paginator(options ...any) (*Pager, error) {
   399  	return nil, nil
   400  }
   401  
   402  func (p *testPage) Param(key any) (any, error) {
   403  	return resource.Param(p, nil, key)
   404  }
   405  
   406  func (p *testPage) Params() maps.Params {
   407  	return p.params
   408  }
   409  
   410  func (p *testPage) Page() Page {
   411  	return p
   412  }
   413  
   414  func (p *testPage) Parent() Page {
   415  	panic("testpage: not implemented")
   416  }
   417  
   418  func (p *testPage) Ancestors() Pages {
   419  	panic("testpage: not implemented")
   420  }
   421  
   422  func (p *testPage) Path() string {
   423  	return p.path
   424  }
   425  
   426  func (p *testPage) Pathc() string {
   427  	return p.path
   428  }
   429  
   430  func (p *testPage) Permalink() string {
   431  	panic("testpage: not implemented")
   432  }
   433  
   434  func (p *testPage) Plain(context.Context) string {
   435  	panic("testpage: not implemented")
   436  }
   437  
   438  func (p *testPage) PlainWords(context.Context) []string {
   439  	panic("testpage: not implemented")
   440  }
   441  
   442  func (p *testPage) Prev() Page {
   443  	panic("testpage: not implemented")
   444  }
   445  
   446  func (p *testPage) PrevInSection() Page {
   447  	return nil
   448  }
   449  
   450  func (p *testPage) PrevPage() Page {
   451  	return nil
   452  }
   453  
   454  func (p *testPage) PublishDate() time.Time {
   455  	return p.pubDate
   456  }
   457  
   458  func (p *testPage) RSSLink() template.URL {
   459  	return ""
   460  }
   461  
   462  func (p *testPage) RawContent() string {
   463  	panic("testpage: not implemented")
   464  }
   465  
   466  func (p *testPage) RenderShortcodes(context.Context) (template.HTML, error) {
   467  	panic("testpage: not implemented")
   468  }
   469  
   470  func (p *testPage) ReadingTime(context.Context) int {
   471  	panic("testpage: not implemented")
   472  }
   473  
   474  func (p *testPage) Ref(argsm map[string]any) (string, error) {
   475  	panic("testpage: not implemented")
   476  }
   477  
   478  func (p *testPage) RefFrom(argsm map[string]any, source any) (string, error) {
   479  	return "", nil
   480  }
   481  
   482  func (p *testPage) RelPermalink() string {
   483  	panic("testpage: not implemented")
   484  }
   485  
   486  func (p *testPage) RelRef(argsm map[string]any) (string, error) {
   487  	panic("testpage: not implemented")
   488  }
   489  
   490  func (p *testPage) RelRefFrom(argsm map[string]any, source any) (string, error) {
   491  	return "", nil
   492  }
   493  
   494  func (p *testPage) Render(ctx context.Context, layout ...string) (template.HTML, error) {
   495  	panic("testpage: not implemented")
   496  }
   497  
   498  func (p *testPage) RenderString(ctx context.Context, args ...any) (template.HTML, error) {
   499  	panic("testpage: not implemented")
   500  }
   501  
   502  func (p *testPage) ResourceType() string {
   503  	panic("testpage: not implemented")
   504  }
   505  
   506  func (p *testPage) Resources() resource.Resources {
   507  	panic("testpage: not implemented")
   508  }
   509  
   510  func (p *testPage) Scratch() *maps.Scratch {
   511  	panic("testpage: not implemented")
   512  }
   513  
   514  func (p *testPage) Store() *maps.Scratch {
   515  	panic("testpage: not implemented")
   516  }
   517  
   518  func (p *testPage) RelatedKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {
   519  	v, err := p.Param(cfg.Name)
   520  	if err != nil {
   521  		return nil, err
   522  	}
   523  
   524  	return cfg.ToKeywords(v)
   525  }
   526  
   527  func (p *testPage) Section() string {
   528  	return p.section
   529  }
   530  
   531  func (p *testPage) Sections() Pages {
   532  	panic("testpage: not implemented")
   533  }
   534  
   535  func (p *testPage) SectionsEntries() []string {
   536  	return p.sectionEntries
   537  }
   538  
   539  func (p *testPage) SectionsPath() string {
   540  	return path.Join(p.sectionEntries...)
   541  }
   542  
   543  func (p *testPage) Site() Site {
   544  	return p.site
   545  }
   546  
   547  func (p *testPage) Sites() Sites {
   548  	panic("testpage: not implemented")
   549  }
   550  
   551  func (p *testPage) Slug() string {
   552  	return p.slug
   553  }
   554  
   555  func (p *testPage) String() string {
   556  	return p.path
   557  }
   558  
   559  func (p *testPage) Summary(context.Context) template.HTML {
   560  	panic("testpage: not implemented")
   561  }
   562  
   563  func (p *testPage) TableOfContents(context.Context) template.HTML {
   564  	panic("testpage: not implemented")
   565  }
   566  
   567  func (p *testPage) Title() string {
   568  	return p.title
   569  }
   570  
   571  func (p *testPage) TranslationBaseName() string {
   572  	panic("testpage: not implemented")
   573  }
   574  
   575  func (p *testPage) TranslationKey() string {
   576  	return p.path
   577  }
   578  
   579  func (p *testPage) Translations() Pages {
   580  	panic("testpage: not implemented")
   581  }
   582  
   583  func (p *testPage) Truncated(context.Context) bool {
   584  	panic("testpage: not implemented")
   585  }
   586  
   587  func (p *testPage) Type() string {
   588  	return p.section
   589  }
   590  
   591  func (p *testPage) URL() string {
   592  	return ""
   593  }
   594  
   595  func (p *testPage) UniqueID() string {
   596  	panic("testpage: not implemented")
   597  }
   598  
   599  func (p *testPage) Weight() int {
   600  	return p.weight
   601  }
   602  
   603  func (p *testPage) WordCount(context.Context) int {
   604  	panic("testpage: not implemented")
   605  }
   606  
   607  func (p *testPage) GetIdentity() identity.Identity {
   608  	panic("testpage: not implemented")
   609  }
   610  
   611  func createTestPages(num int) Pages {
   612  	pages := make(Pages, num)
   613  
   614  	for i := 0; i < num; i++ {
   615  		m := &testPage{
   616  			path:           fmt.Sprintf("/x/y/z/p%d.md", i),
   617  			weight:         5,
   618  			fuzzyWordCount: i + 2, // magic
   619  		}
   620  
   621  		if i%2 == 0 {
   622  			m.weight = 10
   623  		}
   624  		pages[i] = m
   625  
   626  	}
   627  
   628  	return pages
   629  }