Skip to content

Instantly share code, notes, and snippets.

@s1-en
Created June 13, 2020 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1-en/13b561d404a0bd2bd863a9b66ce31c96 to your computer and use it in GitHub Desktop.
Save s1-en/13b561d404a0bd2bd863a9b66ce31c96 to your computer and use it in GitHub Desktop.
HUGO postmaker v1.1 by s1
@echo off
setlocal ENABLEDELAYEDEXPANSION
rem ----------------------------------------------
rem - HUGO postmaker by s1 [mail: s.1@null.net] -
rem - -
rem - Set the proper values in this section -
rem ----------------------------------------------
set post_section=posts
set post_format=yyyy/MM/dd
rem ----------------------------------------------
rem - In case you changed default `contentDir` -
rem ----------------------------------------------
set content_dir=content
rem ----------------------------------------------
rem - If you need help, just post on the thread: -
rem - https://discourse.gohugo.io/t/26242/ -
rem ----------------------------------------------
rem ----------------------------------------------
rem - Don't modify below if you're unsure. -
rem ----------------------------------------------
set v=1.1
if /i "%~1"=="/v" goto version
if /i "%~1"=="-v" goto version
if /i "%~1"=="--version" goto version
if /i "%~1"=="version" goto version
set post_format=%post_format:/=\\\\%
set content_dir=%content_dir:/=\\\\%
echo HUGO postmaker %v% (by s1)
echo.
echo For more info, visit:
echo https://discourse.gohugo.io/t/26242/
echo.
if /i "%~1"=="/?" goto help
if /i "%~1"=="-h" goto help
if /i "%~1"=="--help" goto help
if /i "%~1"=="help" goto help
set bundle=false
if /i "%~1"=="bundle" set bundle=true
if /i "%~1"=="--bundle" set bundle=true
if /i "%~1"=="-b" set bundle=true
if /i "%~1"=="b" set bundle=true
if %bundle%==true (
set cmnd=%~2
set name=%~3
set /a o=4
) else (
set cmnd=%~1
set name=%~2
set /a o=3
)
if /i "%cmnd%"=="" goto nan
if /i "%name%"=="" goto nofile
set name=%name::=-%
set name=%name:?=-%
set name=%name:"=-%
set name=%name:.=-%
set name=%name: =-%
if /i "%cmnd%"=="post" goto posts
if /i "%cmnd%"=="page" goto pages
if /i "%cmnd%"=="section" goto sections
echo  ERROR: Invalid command "%cmnd%" provided.
goto quit
:nan
echo Use "%~n0 /?" or "%~n0 help" for help.
goto quit
:nofile
echo  ERROR: No post filename provided.
goto quit
:posts
set name=%name:/=-%
set name=%name:\=-%
set t=%name%
echo Creating post "%t%"...
for /f %%a in ('powershell -Command "Get-Date -format %post_format%"') do set d=%%a
if %bundle%==true (
set p=%post_section%\\%d%\\%name%\\index
) else (
set p=%post_section%\\%d%\\%name%
)
goto create
:pages
set name=%name:/=\\%
for %%F in ("%name%") do set t=%%~nF
echo Creating page "%t%"...
if %bundle%==true (
set p=%name%\\index
) else (
set p=%name%
)
goto create
:sections
set name=%name:/=\\%
for %%F in ("%name%") do set t=%%~nF
echo Creating section "%t%"...
if %bundle%==false (
if exist "%content_dir%\\%name%" (
echo  ERROR: The %cmnd% "%t%" already exsts in this path.
) else (
echo mkdir "%content_dir%\\%name%"
mkdir "%content_dir%\\%name%"
)
goto quit
) else (
set p=%name%\\_index
)
goto create
:create
if exist "%content_dir%\\%p%.md" (
echo  ERROR: The %cmnd% "%t%" already exsts in this path.
) else (
hugo new "%p%.md"
)
set /a c=0
for %%l in (%*) do (
set /a c+=1
if !c! geq %o% (
echo Creating language variant "%%l" for "%t%"...
if exist "%content_dir%\\%p%.%%l.md" (
echo  ERROR: The language variant "%%l" for "%t%" already exists.
) else (
copy "%content_dir%\\%p%.md" "%content_dir%\\%p%.%%l.md" /Y
)
)
)
goto quit
:help
echo HUGO postmaker helps easily create posts structured inside folders.
echo.
echo NOTICE: This needs to be placed in your HUGO project's main folder.
echo.
echo USAGE: %~n0 [-b, bundle] ^<command^> ^<post^-filename^> [^<extra languages^>]
echo (Don't add *.md at the end of filename.)
echo.
echo.
echo COMMANDS:
echo.
echo b, -b, (Optional) Use to specify that you want a page bundle created
echo bundle, instead of a single Markdown file.
echo --bundle
echo.
echo post Creates a new post if it doesn't exist already.
echo page Creates a new page if it doesn't exist already.
echo section Creates a new section if it doesn't exist already.
echo help Opens this help section.
echo version Show version information.
echo.
echo.
echo EXTRA LANGUAGES:
echo.
echo Applies only to post, page and bundle mode section commands.
echo.
echo You can add any space-delimited language codes and postmaker will create a
echo copy of the created Markdown file while adding the language code to its
echo filename. (See usage examples below.)
echo.
echo.
echo ADDITIONAL CONFIG:
echo.
echo Applies only to post command.
echo.
echo To change these additional configs, Open up "%~nx0"
echo in a text editor and change the values in the top part of the script.
echo It should be self explanatory.
echo.
echo post_section, post_format
echo You can set the hierarchy of your post's folder structure. By default, the
echo format used is "yyyy/MM/dd" and the section is set to "posts".
echo.
echo NOTICE: post_format only accepts date format as of right now.
echo.
echo.
echo EXAMPLES:
echo.
echo %~n0 post hello-world
echo.
echo The above command creates:
echo - posts/2020/06/14/hello-world.md
echo.
echo %~n0 post hello-world ja es de
echo.
echo The above command creates:
echo - posts/2020/06/14/hello-world.md
echo - posts/2020/06/14/hello-world.ja.md
echo - posts/2020/06/14/hello-world.es.md
echo - posts/2020/06/14/hello-world.de.md
echo.
echo %~n0 post -b hello-world ja es de
echo.
echo The above command creates:
echo - posts/2020/06/14/hello-world/index.md
echo - posts/2020/06/14/hello-world/index.ja.md
echo - posts/2020/06/14/hello-world/index.es.md
echo - posts/2020/06/14/hello-world/index.de.md
echo.
echo %~n0 page stuff/my-page
echo.
echo The above command creates:
echo - stuff/my-page.md
echo.
echo %~n0 page stuff/my-page ja 
echo.
echo The above command creates:
echo - stuff/my-page.md
echo - stuff/my-page.ja.md
echo.
echo %~n0 section random
echo.
echo The above command creates:
echo - random/
echo.
echo %~n0 -b section random
echo.
echo The above command creates:
echo - random/_index.md
echo.
echo %~n0 -b section random ja es
echo.
echo The above command creates:
echo - random/_index.md
echo - random/_index.es.md
echo.
goto quit
:version
echo HUGO postmaker
echo version: %v%
echo author: s1 [mail: s.1@null.net]
goto quit
:quit
endlocal
exit /b 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment