Created
June 13, 2020 22:09
-
-
Save s1-en/13b561d404a0bd2bd863a9b66ce31c96 to your computer and use it in GitHub Desktop.
HUGO postmaker v1.1 by s1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 [92mHUGO postmaker %v% (by s1)[0m | |
echo. | |
echo [93mFor more info, visit: | |
echo https://discourse.gohugo.io/t/26242/[0m | |
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 [91m ERROR: Invalid command "[93m%cmnd%[91m" provided.[0m | |
goto quit | |
:nan | |
echo [92mUse "%~n0 /?" or "%~n0 help" for help.[0m | |
goto quit | |
:nofile | |
echo [91m ERROR: No post filename provided.[0m | |
goto quit | |
:posts | |
set name=%name:/=-% | |
set name=%name:\=-% | |
set t=%name% | |
echo [92mCreating post "[93m%t%[92m"...[0m | |
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 [92mCreating page "[93m%t%[92m"...[0m | |
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 [92mCreating section "[93m%t%[92m"...[0m | |
if %bundle%==false ( | |
if exist "%content_dir%\\%name%" ( | |
echo [91m ERROR: The %cmnd% "[93m%t%[91m" already exsts in this path.[0m | |
) 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 [91m ERROR: The %cmnd% "[93m%t%[91m" already exsts in this path.[0m | |
) else ( | |
hugo new "%p%.md" | |
) | |
set /a c=0 | |
for %%l in (%*) do ( | |
set /a c+=1 | |
if !c! geq %o% ( | |
echo [92mCreating language variant "[93m%%l[92m" for "[93m%t%[92m"...[0m | |
if exist "%content_dir%\\%p%.%%l.md" ( | |
echo [91m ERROR: The language variant "[93m%%l[91m" for "[93m%t%[91m" already exists.[0m | |
) else ( | |
copy "%content_dir%\\%p%.md" "%content_dir%\\%p%.%%l.md" /Y | |
) | |
) | |
) | |
goto quit | |
:help | |
echo [92mHUGO postmaker helps easily create posts structured inside folders. | |
echo. | |
echo [101mNOTICE[0m[92m: This needs to be placed in your HUGO project's main folder. | |
echo. | |
echo USAGE: [93m%~n0[92m [[93m-b[92m, [93mbundle[92m] ^<[93mcommand[92m^> ^<[93mpost^-filename[92m^> [^<[93mextra languages[92m^>] | |
echo (Don't add *.md at the end of filename.) | |
echo. | |
echo. | |
echo COMMANDS: | |
echo. | |
echo [93mb[92m, [93m-b[92m, (Optional) Use to specify that you want a page bundle created | |
echo [93mbundle[92m, instead of a single Markdown file. | |
echo [93m--bundle[92m | |
echo. | |
echo [93mpost[92m Creates a new post if it doesn't exist already. | |
echo [93mpage[92m Creates a new page if it doesn't exist already. | |
echo [93msection[92m Creates a new section if it doesn't exist already. | |
echo [93mhelp[92m Opens this help section. | |
echo [93mversion[92m Show version information. | |
echo. | |
echo. | |
echo EXTRA LANGUAGES: | |
echo. | |
echo Applies only to [93mpost[92m, [93mpage[92m and bundle mode [93msection[92m 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 [93mpost[92m command. | |
echo. | |
echo To change these additional configs, Open up "[93m%~nx0[92m" | |
echo in a text editor and change the values in the top part of the script. | |
echo It should be self explanatory. | |
echo. | |
echo [93mpost_section[92m, [93mpost_format[92m | |
echo You can set the hierarchy of your post's folder structure. By default, the | |
echo format used is "[93myyyy/MM/dd[92m" and the section is set to "[93mposts[92m". | |
echo. | |
echo [101mNOTICE[0m[92m: [93mpost_format[92m only accepts date format as of right now. | |
echo. | |
echo. | |
echo EXAMPLES: | |
echo. | |
echo [93m%~n0 post hello-world[92m | |
echo. | |
echo The above command creates: | |
echo - posts/2020/06/14/hello-world.md | |
echo. | |
echo [93m%~n0 post hello-world ja es de[92m | |
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 [93m%~n0 post -b hello-world ja es de[92m | |
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 [93m%~n0 page stuff/my-page[92m | |
echo. | |
echo The above command creates: | |
echo - stuff/my-page.md | |
echo. | |
echo [93m%~n0 page stuff/my-page ja [92m | |
echo. | |
echo The above command creates: | |
echo - stuff/my-page.md | |
echo - stuff/my-page.ja.md | |
echo. | |
echo [93m%~n0 section random[92m | |
echo. | |
echo The above command creates: | |
echo - random/ | |
echo. | |
echo [93m%~n0 -b section random[92m | |
echo. | |
echo The above command creates: | |
echo - random/_index.md | |
echo. | |
echo [93m%~n0 -b section random ja es[92m | |
echo. | |
echo The above command creates: | |
echo - random/_index.md | |
echo - random/_index.es.md[0m | |
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