This code is based on wordpressXML2HTML.
| WordPress element | HTML element |
|---|---|
| Post title | <h2> |
| Post date | <b> |
| Post content | <p> |
-
Place the XML file inside this project folder.
-
Run the following command in your shell from the project's root folder:
go run main.go -f FILE -o OUTPUT [-n NUMBER] [-sd STARTDATE] [-ed ENDDATE]
For example:
go run main.go -f wordpress.xml -o new-file.html -n 10 -sd 2017-01-01 -ed 2017-12-31
This script supports optional filtering by date and limiting the number of entries processed. Input and output file names are mandatory.
Available parameters:
-f: Name of the XML file exported from WordPress.-o: Name of the output HTML file.-n: Number of entries to convert.-sd: Start date for processing entries (format:YYYY-MM-DD).-ed: End date for processing entries (format:YYYY-MM-DD).
To run this code locally, you need to install Go.
Currently, there is no way to separate pages from posts, so they will be formatted as equal elements in the output file. If necessary, manually remove pages you do not wish to include.
Posts without a publication date in the XML file will be ignored and excluded from the output file.
Media files should be exported separately and manually added to the HTML file.
-
Struct Definitions (
WordpressExport,Channel,Item):- These structs define the structure of the XML data exported from WordPress. Each
Itemrepresents a post with fields such asTitle,Link,PubDate,Description, andContent.
- These structs define the structure of the XML data exported from WordPress. Each
-
Command-line Flags:
- The
flagpackage is used to parse command-line arguments (-f,-o,-n,-sd,-ed) corresponding to the XML input file, output HTML file, maximum number of entries to process, start date, and end date respectively.
- The
-
Date Parsing:
- The
parseDatefunction converts date strings inYYYY-MM-DDformat totime.Timeobjects. It handles errors and returns a zerotime.Time{}object if the date cannot be parsed.
- The
-
XML Parsing:
- The XML file (
fileName) is read and parsed into aWordpressExportstruct usingxml.Unmarshal.
- The XML file (
-
Processing and Output:
- For each post (
Item) inWordpressExport.Channel.Items:- The
PubDateis parsed into atime.Timeobject. - Entries are filtered based on the provided
startDateandendDate. - Valid entries are formatted into HTML and written to the output file (
outFileName).
- The
- For each post (
-
HTML Generation:
- The HTML file begins with
<html><body>and ends with</body></html>. - Each post's title (
<h2>), date (<b>), and content (<p>) are formatted accordingly.
- The HTML file begins with
-
Error Handling:
- Errors related to file operations (
os.Open,os.Create), XML parsing (xml.Unmarshal), and date parsing (time.Parse) are handled with error messages printed to standard output.
- Errors related to file operations (