learning swift and other stuff

starting with Publish

Published June 2020

as this site is done with Publish by John Sundell the first article obviously has to explain how this was done.

i do not want to repeat what John already documented or will document shortly so i just address what problems i had starting with Publish and how i did solve them.

getting Publish to run

follow the installation guide here:

https://github.com/JohnSundell/Publish

configure your site

in main.swift configure your site:

// This type acts as the configuration for your website.
struct Mimorocks: Website {
    enum SectionID: String, WebsiteSectionID {
        // Add the sections that you want your website to contain here:
        case tips
        case products
        case aboutme
        case others
        case imprint
    }

    struct ItemMetadata: WebsiteItemMetadata {
        // Add any site-specific metadata that you want to use here.
    }

    // Update these properties to configure your website:
    var url = URL(string: "https://mimo.rocks")!
    var name = "mimo.rocks"
    var description = "Welcome"
    var language: Language { .english }
    var imagePath: Path? { nil }
}

try Mimorocks().publish(withTheme: .mimo)

do your own theme

even if it is great for a start you do not want to use the build in FoundationTheme. Build your own to play around with.

so add a new file to contain your theme extension. In my case Theme+Mimo.swift using a new HTMLFactoy MimosHTMLFactory



import Plot
import Publish

public extension Theme {

    /// my own mimo Theme
    static var mimo: Self {
        Theme(
            htmlFactory: MimosHTMLFactory()
        )
    }
}

for a start you could copy the standard FoundationHTMLFactory from Theme+Foundation.swift and modify it later for your needs.

private struct MimosHTMLFactory<Site: Website>: HTMLFactory {
  func makeIndexHTML(for index: Index,
                     context: PublishingContext<Site>) throws -> HTML {
                     ...
                     }
}

install Splash plugin

to use code decoration for Swift Code i choosed to use Johns own Splash

Follow the documentation here to do that.

for the style to have effect i included the css from the example CSS file in my exisiting Resources/styles.css .

to use the plugin you have to replace you site generation in main.swift from the simple

try Mimorocks().publish(withTheme: .mimo)

to somthing like

// This will generate my website using the mimo theme:
try Mimorocks().publish(using: [
    .installPlugin(.splash(withClassPrefix: "")),
    .addMarkdownFiles(),
    .copyResources(),
    .generateHTML(withTheme: .mimo)
])

tips

Resources from Resources directory are copied automatically.

You will not need to do this by code. In fact you'll get errors if you do so and your target files are not ambigous

do not do this if you already have Resources/styles.css

static var mimo: Self {
    Theme(
        htmlFactory: MimosHTMLFactory(),
        resourcePaths: ["Resources/mimoTheme/styles.css"]
    )
}