Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo-1
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo-1
Commits
a6fa5fc9
Unverified
Commit
a6fa5fc9
authored
Jan 08, 2020
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
#19
parent
c662a8c1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
10 deletions
+52
-10
src/main/java/org/b3log/solo/processor/StaticSiteProcessor.java
...in/java/org/b3log/solo/processor/StaticSiteProcessor.java
+52
-10
No files found.
src/main/java/org/b3log/solo/processor/StaticSiteProcessor.java
View file @
a6fa5fc9
...
...
@@ -24,15 +24,24 @@ import org.b3log.latke.http.RequestContext;
import
org.b3log.latke.http.annotation.Before
;
import
org.b3log.latke.http.annotation.RequestProcessing
;
import
org.b3log.latke.http.annotation.RequestProcessor
;
import
org.b3log.latke.ioc.BeanManager
;
import
org.b3log.latke.logging.Level
;
import
org.b3log.latke.logging.Logger
;
import
org.b3log.latke.util.CollectionUtils
;
import
org.b3log.solo.model.Article
;
import
org.b3log.solo.model.Option
;
import
org.b3log.solo.processor.console.ConsoleAuthAdvice
;
import
org.b3log.solo.service.ArticleQueryService
;
import
org.b3log.solo.service.OptionQueryService
;
import
org.b3log.solo.util.Mocks
;
import
org.b3log.solo.util.Solos
;
import
org.json.JSONObject
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.OutputStream
;
import
java.nio.charset.StandardCharsets
;
import
java.util.List
;
/**
* Static site processor. HTML 静态站点生成 https://github.com/88250/solo/issues/19
...
...
@@ -84,16 +93,18 @@ public class StaticSiteProcessor {
requestFile
(
"/blog/info"
);
requestFile
(
"/manifest.json"
);
requestArticles
();
Latkes
.
setServerScheme
(
"http"
);
Latkes
.
setServerHost
(
"localhost"
);
Latkes
.
setServerPort
(
"8080"
);
copySkin
();
copy
JS
();
copy
Images
();
copy
File
(
"sw.js"
);
copy
File
(
"robots.txt"
);
copy
File
(
"CHANGE_LOGS.md"
);
genSkins
();
gen
JS
();
gen
Images
();
gen
File
(
"sw.js"
);
gen
File
(
"robots.txt"
);
gen
File
(
"CHANGE_LOGS.md"
);
LOGGER
.
log
(
Level
.
INFO
,
"Static site generated [dir="
+
staticSitePath
+
"]"
);
...
...
@@ -104,35 +115,66 @@ public class StaticSiteProcessor {
}
}
private
static
void
requestArticles
()
throws
Exception
{
final
BeanManager
beanManager
=
BeanManager
.
getInstance
();
final
ArticleQueryService
articleQueryService
=
beanManager
.
getReference
(
ArticleQueryService
.
class
);
final
OptionQueryService
optionQueryService
=
beanManager
.
getReference
(
OptionQueryService
.
class
);
final
JSONObject
preference
=
optionQueryService
.
getPreference
();
final
int
pageSize
=
preference
.
getInt
(
Option
.
ID_C_ARTICLE_LIST_DISPLAY_COUNT
);
final
int
windowSize
=
preference
.
getInt
(
Option
.
ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE
);
int
currentPageNum
=
1
;
while
(
true
)
{
final
JSONObject
requestJSONObject
=
Solos
.
buildPaginationRequest
(
String
.
valueOf
(
currentPageNum
)
+
'/'
+
pageSize
+
'/'
+
windowSize
);
requestJSONObject
.
put
(
Article
.
ARTICLE_STATUS
,
Article
.
ARTICLE_STATUS_C_PUBLISHED
);
requestJSONObject
.
put
(
Option
.
ID_C_ENABLE_ARTICLE_UPDATE_HINT
,
false
);
final
JSONObject
result
=
articleQueryService
.
getArticles
(
requestJSONObject
);
final
List
<
JSONObject
>
articles
=
CollectionUtils
.
jsonArrayToList
(
result
.
getJSONArray
(
Article
.
ARTICLES
));
if
(
articles
.
isEmpty
())
{
break
;
}
for
(
final
JSONObject
article
:
articles
)
{
final
String
permalink
=
article
.
optString
(
Article
.
ARTICLE_PERMALINK
);
requestFile
(
permalink
);
}
currentPageNum
++;
}
}
private
static
void
requestFile
(
final
String
uri
)
throws
Exception
{
FileUtils
.
forceMkdirParent
(
new
File
(
staticSitePath
+
uri
));
final
OutputStream
outputStream
=
new
FileOutputStream
(
staticSitePath
+
uri
);
String
html
=
Mocks
.
mockRequest
(
uri
);
IOUtils
.
write
(
html
,
outputStream
,
StandardCharsets
.
UTF_8
);
outputStream
.
close
();
LOGGER
.
log
(
Level
.
INFO
,
"Generated a file ["
+
uri
+
"]"
);
}
private
static
void
copySkin
()
throws
Exception
{
private
static
void
genSkins
()
throws
Exception
{
FileUtils
.
deleteDirectory
(
new
File
(
staticSitePath
+
"/skins"
));
FileUtils
.
forceMkdir
(
new
File
(
staticSitePath
+
"/skins"
));
FileUtils
.
copyDirectory
(
new
File
(
StaticSiteProcessor
.
class
.
getResource
(
"/skins"
).
toURI
()),
new
File
(
staticSitePath
+
"/skins"
));
LOGGER
.
log
(
Level
.
INFO
,
"Generated skins"
);
}
private
static
void
copy
JS
()
throws
Exception
{
private
static
void
gen
JS
()
throws
Exception
{
FileUtils
.
deleteDirectory
(
new
File
(
staticSitePath
+
"/js"
));
FileUtils
.
forceMkdir
(
new
File
(
staticSitePath
+
"/js"
));
FileUtils
.
copyDirectory
(
new
File
(
StaticSiteProcessor
.
class
.
getResource
(
"/js"
).
toURI
()),
new
File
(
staticSitePath
+
"/js"
));
LOGGER
.
log
(
Level
.
INFO
,
"Generated js"
);
}
private
static
void
copy
Images
()
throws
Exception
{
private
static
void
gen
Images
()
throws
Exception
{
FileUtils
.
deleteDirectory
(
new
File
(
staticSitePath
+
"/images"
));
FileUtils
.
forceMkdir
(
new
File
(
staticSitePath
+
"/images"
));
FileUtils
.
copyDirectory
(
new
File
(
StaticSiteProcessor
.
class
.
getResource
(
"/images"
).
toURI
()),
new
File
(
staticSitePath
+
"/images"
));
LOGGER
.
log
(
Level
.
INFO
,
"Generated images"
);
}
private
static
void
copy
File
(
final
String
file
)
throws
Exception
{
private
static
void
gen
File
(
final
String
file
)
throws
Exception
{
FileUtils
.
forceMkdirParent
(
new
File
(
staticSitePath
+
"/"
+
file
));
final
String
staticSitePath
=
StaticSiteProcessor
.
class
.
getResource
(
"/"
+
STATIC_SITE
).
toURI
().
getPath
();
FileUtils
.
copyFile
(
new
File
(
sourcePath
+
"/"
+
file
),
new
File
(
staticSitePath
+
"/"
+
file
));
LOGGER
.
log
(
Level
.
INFO
,
"Generated a file ["
+
file
+
"]"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment