While migrating some photo gallery data from my old website to Hugo, I wanted to convert some exported csv data to yaml for using in Hugo content.
I found this post helpful - Hugo: Insert data into content with a shortcode. But my use case was slightly different. I was creating a new layout while trying to reuse an existing partial template in a Hugo theme.
Quick Script #
I installed powershell-yaml module from PSGallery.
Install-Module -Name powershell-yaml
Import-Module powershell-yamlImport csv data. My data didn't have a header and had some fields I didn't care about.
$header = 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'photoPath', 'photoDate', 'photoTitle', 'photoDescription', 'C13'
$data = Import-Csv -Path .\hawaii.csv -Header $headerCreate a Hash Table and append only the relevant data from csv data.
$pictures = @()
foreach ($row in $data) {$pictures += @{ 'title' = $row.photoTitle; 'description' = $row.photoDescription; 'image' = $row.photoPath; 'thumb' = $row.photoPath;}}
$yml = @{ 'title' = "Hawaii"; 'style' = "style1 medium lightbox onscroll-fade-in"; 'content' = "<em>Dec 2021. Oahu, HI, USA.</em>"; 'pictures' = $pictures}Convert to Yaml and output to a file
ConvertTo-Yaml $yml | Out-File hawaii.ymlReferences #
Some references I used for my setup
- https://www.powershellgallery.com/packages/powershell-yaml/0.4.2
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv
- Modify YML files with PowerShell
- Working With Powershell Objects to Create Yaml
- https://stackoverflow.com/questions/10655788/powershell-set-content-and-out-file-what-is-the-difference
🙏🙏🙏
Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.
Published