commit 6d6fccf2f7f117cc7a65d9dc78ef91832e8045f5 Author: HiPhish Date: Sat Jul 23 11:35:23 2022 +0200 Initial commit diff --git a/HACKING.rst b/HACKING.rst new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..be3f7b2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..8de220f --- /dev/null +++ b/README.rst @@ -0,0 +1,35 @@ +.. default-role:: code + + +################################################ + Hackable static site generator for Common Lisp +################################################ + +This is a static site generator. It is hackable. And complicated. + +In HSSG a website it not an HSSG project, it is a regular Common Lisp system +and HSSG acts as a library. This gives website projects full freedom in how +they want to build and structure their website. + +The HSSG system itself only provides the bare minimum of a useful static site +generator core. Additional higher-level features, such as a blog or a gallery, +can then be built on top of the core. These plugins are just regular Common +Lisp systems with their own dependencies, and a website project can depend on +them like on any other system. + + +Status of the project +##################### + +Things will change. Do not use in production unless you are willing to follow +`HEAD`. In fact, you probably should not be using it at all. How did you even +find it? Right now I am dogfooding my own product. + + +License +####### + +Release under the Affero GNU GPL version 3 or later. Refer to the LICENSE_ file +for details. + +.. _LICENSE: LICENSE diff --git a/TODO.rst b/TODO.rst new file mode 100644 index 0000000..ec5ee3e --- /dev/null +++ b/TODO.rst @@ -0,0 +1,29 @@ +.. default-role:: code + +################### + Things to be done +################### + +Blog +#### + +Fixes +===== + +- Make sure the `lisp` reader uses throwaway packages instead of polluting the + global namespace + +Features +======== + +- Template: docstrings in template + +Cleanup +======= + +- How much of each artifact's internals need to be exposed? Make accessor + functions? Constructor functions over `MAKE-INSTANCE`? +- A proper blog plugin interface; we probably only want a constructor function + which will then instantiate a blog artifact which in turn will instantiate + all its subordinate artifacts. Thus the blog artifact acts as a façade to the + entire blog plugin. diff --git a/hssg-blog-cmark.asd b/hssg-blog-cmark.asd new file mode 100644 index 0000000..c623b7b --- /dev/null +++ b/hssg-blog-cmark.asd @@ -0,0 +1,30 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; hssg-blog-cmark.asd CommonMark reader for blog content +;;;; definition Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG-BLOG. +;;;; +;;;; CL-HSSG-BLOG is free software: you can redistribute it and/or modify it +;;;; under the terms of the GNU Affero General Public License as published by +;;;; the Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG-BLOG is distributed in the hope that it will be useful, but +;;;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +;;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public +;;;; License for more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG-BLOG If not, see . + +(asdf:defsystem #:hssg-blog-cmark + :description "Blog extension to HSSG, CommonMark reader" + :author "HiPhish " + :license "AGPL-3.0-or-later" + :version "0.0.0" + :depends-on ("cmark" "cl-ppcre") + :serial t + :components ((module "src" + :components ((:module "cmark-reader" + :components ((:file "main"))))))) diff --git a/hssg-blog.asd b/hssg-blog.asd new file mode 100644 index 0000000..79ce648 --- /dev/null +++ b/hssg-blog.asd @@ -0,0 +1,56 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; hssg-blog.asd Hackable Static Site Generator blog extension system +;;;; definition Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG-BLOG. +;;;; +;;;; CL-HSSG-BLOG is free software: you can redistribute it and/or modify it +;;;; under the terms of the GNU Affero General Public License as published by +;;;; the Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG-BLOG is distributed in the hope that it will be useful, but +;;;; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +;;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public +;;;; License for more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG-BLOG If not, see . + +(asdf:defsystem #:hssg-blog + :description "Blog extension to HSSG" + :author "HiPhish " + :license "AGPL-3.0-or-later" + :version "0.0.0" + :depends-on ("hssg" "cmark" "cl-fad") + :serial t + :components ((module "src" + :components ((:module "blog" + :components ((:file "package") + (:file "config") + (:file "util") + (:file "i18n") + (:module "periods" + :components ((:file "classes") + (:file "protocol"))) + (:module "readers" + :components ((:file "readers") + (:file "lisp"))) + (:module "artifacts" + :components ((:file "classes") + (:file "util") + (:file "archive") + (:file "blog") + (:file "category") + (:file "index-page") + (:file "post") + (:file "rss-feed") + (:file "tag"))) + (:module "template" + :components ((:file "archive") + (:file "blog") + (:file "category-tag") + (:file "index") + (:file "post"))) + (:file "templates"))))))) diff --git a/hssg.asd b/hssg.asd new file mode 100644 index 0000000..1b9a19f --- /dev/null +++ b/hssg.asd @@ -0,0 +1,43 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; hssg.asd Hackable Static Site Generator system definition +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . + +(asdf:defsystem #:hssg + :description "A hackable static site generator for Common Lisp." + :author "HiPhish " + :license "AGPL-3.0-or-later" + :version "0.0.0" + :depends-on ("alexandria" "cl-fad" "local-time" "plump") + :serial t + :components ((:module "src" + :components ((:module "hssg" + :components ((:file "package") + (:file "config") + (:file "reader") + (:module "readers" + :components ((:file "lisp"))) + (:file "artifact") + (:module "artifacts" + :components ((:file "classes") + (:file "compound") + (:file "html") + (:file "xml") + (:file "verbatim") + (:file "directory"))) + (:file "template"))))))) diff --git a/src/blog/artifacts/archive.lisp b/src/blog/artifacts/archive.lisp new file mode 100644 index 0000000..066ea2a --- /dev/null +++ b/src/blog/artifacts/archive.lisp @@ -0,0 +1,44 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; archive.lisp Implementation of blog post archive artifact +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.archive) + +(defmethod hssg:write-artifact ((archive archive-page-artifact)) + (with-slots ((periods hssg.blog.artifacts:periods) + (blog hssg.blog.artifacts:blog)) + archive + (with-slots ((url hssg.blog.artifacts:url) + (output hssg.blog.artifacts:output) + (template hssg.blog.artifacts:template) + (initial hssg.blog.artifacts:initial)) + blog + (let ((html-page (make-instance 'hssg:html-artifact + :data `((:periods . ,periods) + (:blog . ,blog) + ,@initial) + :template (hssg:chain-templates + 'hssg.blog.template:archive + 'hssg.blog.template:blog-page + template) + :output (apply #'fad:merge-pathnames-as-file + `(,output + ,(fad:pathname-as-directory "archive") + ,(fad:pathname-as-file "index.html")))))) + (hssg:write-artifact html-page))))) diff --git a/src/blog/artifacts/blog.lisp b/src/blog/artifacts/blog.lisp new file mode 100644 index 0000000..1045511 --- /dev/null +++ b/src/blog/artifacts/blog.lisp @@ -0,0 +1,184 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; blog.lisp Implementation of the entire "blog" artifact +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifacts.blog) + +(defmethod hssg:write-artifact ((blog blog-artifact)) + (with-slots (posts categories tags periods output static) blog + (with-slots (items) categories + (setf items (sort items #'string<= :key #'hssg.blog.artifacts:category-name))) + (with-slots (items) tags + (setf items (sort items #'>= :key (lambda (tag) (length (slot-value tag 'hssg.blog.artifacts:posts)))))) + (dolist (post posts) + (write-artifact post)) + (dolist (index-page (hssg.blog.artifact.util:collect-index-pages posts blog '())) + (write-artifact index-page)) + (dolist (year-period periods) + (with-slots ((year hssg.blog.period:year) + (months hssg.blog.period:months)) + year-period + (let ((artifact (make-instance 'hssg.blog.artifacts:index-page-artifact + :url (list (format nil "~D" year)) + :posts (hssg.blog.period:period-posts year-period) + :number 1 + :total 1 + :prev nil + :next nil + :blog blog))) + (write-artifact artifact)) + (dolist (month-period (slot-value year-period 'hssg.blog.period:months)) + (with-slots ((month hssg.blog.period:month)) month-period + (let ((artifact (make-instance 'hssg.blog.artifacts:index-page-artifact + :url (list (format nil "~D" year) + (format nil "~2,'0D" month)) + :posts (hssg.blog.period:period-posts month-period) + :number 1 + :total 1 + :prev nil + :next nil + :blog blog))) + (write-artifact artifact)))))) + (write-artifact static) + (write-artifact categories) + (write-artifact tags) + (write-artifact (make-instance 'hssg.blog.artifacts:archive-page-artifact :periods periods :blog blog)) + (write-artifact (make-instance 'hssg.blog.artifacts:rss-feed-artifact :posts posts :blog blog)))) + +(defun directory-contents (path &key (test #'fad:directory-pathname-p)) + "Contents of a directory, sorted alphabetically in ascending order." + (declare (type pathname path) + (type (function (pathname) boolean) test)) + (sort (remove-if-not test (fad:list-directory path)) + #'string<= :key #'namestring)) + +(defun file-pathname-p (pathname) + "Whether PATHNAME names a file." + (not (fad:directory-pathname-p pathname))) + +(defun read-blog (title path url output template &key (description "") (initial '()) (top "")) + "Read the entire blog at file path PATH and write it to OUTPUT. All pages are + processed through the TEMPLATE." + (declare (type pathname path output) + (type string title top) + (type list initial url)) + (let ((result (make-instance 'hssg.blog.artifacts:blog-artifact + :title title :description description + :url url :output output + :template template :initial initial :top top)) + (last-post nil)) + (setf (slot-value (slot-value result 'hssg.blog.artifacts:categories) 'hssg.blog.artifacts:blog) result) + (setf (slot-value (slot-value result 'hssg.blog.artifacts:tags) 'hssg.blog.artifacts:blog) result) + (dolist (year (directory-contents path)) + (dolist (month (directory-contents year)) + (dolist (day (directory-contents month)) + (dolist (entry (directory-contents day :test #'file-pathname-p)) + (let ((post (read-post entry result))) + (when last-post + (setf (slot-value post 'hssg.blog.artifacts:previous) last-post) + (setf (slot-value last-post 'hssg.blog.artifacts:next) post)) + (add-blog-post result post) + (setf last-post post))) + (dolist (directory (directory-contents day :test #'fad:directory-pathname-p)) + (add-static + result + ;; The path needs to be relative, so we perform some PATHNAME trickery + (enough-namestring directory (fad:pathname-parent-directory year)) + path))))) + result)) + +(defun read-post (file-path blog) + "Reads one single blog post from file FILE-PATH, where the path of FILE-PATH + determines the date of the post and file name the slug of the post." + (declare (type pathname file-path) + (type blog-artifact blog)) + (let ((date-components (mapcar #'parse-integer + (last (pathname-directory file-path) 3))) + (post (funcall (gethash (pathname-type file-path) hssg.blog:*BLOG-POST-READERS*) + file-path))) + (hssg:let-metadata post + ((category :category hssg.blog.i18n:*default-category*) + (slug :slug (pathname-name file-path)) + (title :title) + (author :author) + (tags :tags) + (published :published (apply #'date-from-numbers date-components)) + (modified :modified) + (content :content)) + (make-instance 'hssg.blog.artifacts:post-artifact :blog blog :slug slug :title title :content content + :category category :tags tags :author author + :published published + :modified modified + :metadata post)))) + +(defun add-blog-post (blog post) + "Add a single blog post to the entire blog. The blog's author, category and + tags will also be registered." + (with-slots (posts tags authors categories) blog + (push post posts) + (with-slots ((category-name category) + (post-tags tags) + author) + post + (when category-name + (hssg.blog.artifact.category:add-post categories category-name post)) + (when post-tags + (dolist (tag post-tags) + (hssg.blog.artifact.tag:add-post tags tag post))) + (when author + (push post (gethash author authors)))) + ;; This assumes that incoming posts are ordered from oldest to newest! + (with-slots (periods) blog + (let ((year (local-time:timestamp-year (slot-value post 'hssg.blog.artifacts:published))) + (top-period (car periods))) + (when (or (not top-period) (not (= year (slot-value top-period 'hssg.blog.period:year)))) + (push (make-instance 'hssg.blog.period:year-period :year year) periods))) + (push-post (car periods) post)))) + +(defun add-static (blog pathname input-dir) + (with-slots (static (output-dir output)) blog + (with-slots ((artifacts hssg:artifacts)) static + (let ((artifact (hssg:make-directory-artifact + pathname + input-dir + output-dir))) + (push artifact artifacts))))) + +(defmethod push-post ((period hssg.blog.period:day-period) post) + (with-slots ((posts hssg.blog.period:posts)) period + (push post posts))) + +(defmethod push-post ((period hssg.blog.period:month-period) post) + (with-slots ((days hssg.blog.period:days)) period + (let ((day (local-time:timestamp-day (slot-value post 'hssg.blog.artifacts:published)))) + (let ((topmost (car days))) + (if (or (null topmost) (not (= day (slot-value topmost 'hssg.blog.period:day)))) + (let ((day-period (make-instance 'hssg.blog.period:day-period :day day))) + (push day-period days) + (push-post day-period post)) + (push-post topmost post)))))) + +(defmethod push-post ((period hssg.blog.period:year-period) post) + (with-slots ((months hssg.blog.period:months)) period + (let ((month (local-time:timestamp-month (slot-value post 'hssg.blog.artifacts:published)))) + (let ((topmost (car months))) + (if (or (null topmost) (not (= month (slot-value topmost 'hssg.blog.period:month)))) + (let ((month-period (make-instance 'hssg.blog.period:month-period :month month))) + (push month-period months) + (push-post month-period post)) + (push-post topmost post)))))) diff --git a/src/blog/artifacts/category.lisp b/src/blog/artifacts/category.lisp new file mode 100644 index 0000000..4553f1c --- /dev/null +++ b/src/blog/artifacts/category.lisp @@ -0,0 +1,67 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; config.lisp Mutable global variables for configuring the entire generator +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.category) + +(defmethod hssg:write-artifact ((category category-artifact)) + (with-slots ((blog hssg.blog.artifacts:blog) + (name hssg.blog.artifacts:name) + (posts hssg.blog.artifacts:posts)) + category + (dolist (index-page (hssg.blog.artifact.util:collect-index-pages + posts blog (list (hssg.blog.i18n:localise :url-categories) name))) + (hssg:write-artifact index-page)))) + +(defmethod hssg:write-artifact ((artifact categories-artifact)) + (with-slots ((items hssg.blog.artifacts:items) + (blog hssg.blog.artifacts:blog)) + artifact + (let ((artifact (make-instance 'hssg:html-artifact + :data `((:categories . ,items) + (:blog . ,blog) + ,@(slot-value blog 'hssg.blog.artifacts:initial)) + :template (hssg:chain-templates + 'hssg.blog.template:categories + 'hssg.blog.template:blog-page + (slot-value blog 'hssg.blog.artifacts:template)) + :output (fad:merge-pathnames-as-file + (slot-value blog 'hssg.blog.artifacts:output) + (fad:pathname-as-directory (hssg.blog.i18n:localise :url-categories)) + (fad:pathname-as-file "index.html"))))) + (hssg:write-artifact artifact)) + (dolist (category items) + (hssg:write-artifact category)))) + +(defun fetch-category (categories name) + "Retrieve the category object with a given NAME from a BLOG artifact; if + there is no category object it is created and registered first." + (declare (type hssg.blog.artifacts:categories-artifact categories) + (type string name)) + (with-slots ((items hssg.blog.artifacts:items) + (blog hssg.blog.artifacts:blog)) + categories + (or (find name items :test #'string= :key #'hssg.blog.artifacts:category-name) + (let ((result (make-instance 'category-artifact :name name :blog blog))) + (push result items) + result)))) + +(defun add-post (categories name post) + (with-slots ((posts hssg.blog.artifacts:posts)) + (fetch-category categories name) + (push post posts))) diff --git a/src/blog/artifacts/classes.lisp b/src/blog/artifacts/classes.lisp new file mode 100644 index 0000000..1909552 --- /dev/null +++ b/src/blog/artifacts/classes.lisp @@ -0,0 +1,143 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; config.lisp Mutable global variables for configuring the entire generator +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifacts) + +(defparameter *posts-per-page* 10 + "How many blog posts to display per index page") + +(defclass blog-artifact () + ((title :initarg :title :type string + :documentation "Human-readable title of the blog.") + (description :initarg :description :initform "" :type string + :documentation "Human-readable description of the blog") + (posts :initform '() :type list + :documentation "List of blog posts ordered from most recent to oldest.") + (categories :initform (make-instance 'categories-artifact) :type categories-artifact + :documentation "Collection of categories of the blog") + (tags :initform (make-instance 'tags-artifact) :type tags-artifact + :documentation "Collection of tags of the blog") + (authors :initform (make-hash-table :test 'equal) :type hash-table + :documentation "Maps an author's name to an unordered set of posts.") + (periods :initform '() :type list + :documentation "Year periods of the blog.") + (top :initarg :top :initform "" :type string + :documentation "Identifier to use as the top of the breadcrumbs") + (url :initarg :url :type list + :documentation "URL of the blog as a list of URL path strings, relative + to the root of the site.") + (output :initarg :output :type pathname + :documentation "Path to the blog, relative to the root of the site") + (template :initarg :template :initform #'hssg:identity-template :type hssg:template + :documentation "Base template to apply to all pages") + (initial :initarg :initial :initform '() :type list + :documentation "Initial metadata for all pages.") + (static :initform (make-instance 'hssg:compound-artifact) + :documentation "Static files which will be added to the blog verbatim.")) + (:documentation "Artifact which represents an entire blog.")) + +(defclass post-artifact () + ((blog :initarg :blog :type blog-artifact + :documentation "Blog this post belongs to.") + (slug :initarg :slug :type string + :documentation "Short string identifier, used for the URL; not necessarily unique.") + (title :initarg :title :type string + :documentation "Full title of the post") + (content :initform '() :initarg :content :type list + :documentation "Content of the post as SXML.") + (category :initform hssg.blog.i18n:*default-category* :initarg :category :type string + :documentation "Name of the blog post category.") + (tags :initform '() :initarg :tags :type list + :documentation "Names of the blog post tags") + (author :initform nil :initarg :author :type (or null string) + :documentation "Name of author of the blog post.") + (published :initarg :published :type local-time:timestamp) + (modified :initarg :modified :type (or null local-time:timestamp)) + (status :initform :published :initarg :status :type (member :published :draft) + :documentation "Status of the post, either :PUBLISHED (default) or :DRAFT.") + (previous :initform nil :initarg :prev :type (or null post-artifact) + :documentation "Previous blog post") + (next :initform nil :initarg :next :type (or null post-artifact) + :documentation "Next blog post") + (metadata :initform '() :initarg :metadata :type list + :documentation "Association list of extra metadata for the post page, will be + passed to the HTML template.")) + (:documentation "Artifact which represents a single blog post.")) + +(defclass categories-artifact () + ((items :initform '() :type list + :documentation "List of categories recorded this far.") + (blog :initarg :blog :type blog-artifact + :documentation "Blog these categories belong to.")) + (:documentation "Container of all individual category artifacts.")) + +(defclass category-artifact () + ((blog :initarg :blog :type blog-artifact + :documentation "Blog this category belongs to.") + (name :initarg :name :reader category-name :type string + :documentation "Name of the category") + (posts :initarg :posts :initform '() :type list + :documentation "Posts which belong to this category")) + (:documentation "A category of blog posts")) + +(defclass tags-artifact () + ((items :initform '() :type list + :documentation "List of tag artifacts recorded this far.") + (blog :initarg :blog :type blog-artifact + :documentation "Blog these categories belong to.")) + (:documentation "Container of all individual tag artifacts.")) + +(defclass tag-artifact () + ((blog :initarg :blog :type blog-artifact + :documentation "Blog this tag belongs to.") + (name :initarg :name :reader tag-name :type string + :documentation "Name of the tag") + (posts :initarg :posts :initform '() :type list + :documentation "Posts which belong to this tag")) + (:documentation "A tag of blog posts")) + +(defclass index-page-artifact () + ((url :initarg :url :type string + :documentation "Under which URL to save the index page (excluding index number).") + (posts :initarg :posts :type list + :documentation "Posts to display on this page") + (number :initarg :number :type (integer 1) + :documentation "Number of this page among all pages") + (total :initform 1 :initarg :total :type (integer 1) + :documentation "How many pages there are in total.") + (previous :initarg :prev :type (or null index-page-artifact) + :documentation "Previous page, if any") + (next :initarg :next :type (or null index-page-artifact) + :documentation "Next page, if any") + (blog :initarg :blog :type blog-artifact + :documentation "The blog instance this index belongs to.")) + (:documentation "A single page listing a number of posts with their preview.")) + +(defclass archive-page-artifact () + ((periods :initarg :periods :type list + :documentation "Association-tree of the blog's post periods.") + (blog :initarg :blog :type blog-artifact + :documentation "Blog this archive belongs to.")) + (:documentation "Archive page which lists all posts grouped by their period.")) + +(defclass rss-feed-artifact () + ((posts :initarg :posts) + (blog :initarg :blog :type blog-artifact + :documentation "Blog this RSS feed belongs to.")) + (:documentation "RSS feed of blog posts.")) diff --git a/src/blog/artifacts/index-page.lisp b/src/blog/artifacts/index-page.lisp new file mode 100644 index 0000000..019f028 --- /dev/null +++ b/src/blog/artifacts/index-page.lisp @@ -0,0 +1,55 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; index-page.lisp Implementation of the "index" page artifact +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.index-page) + +(defmethod hssg:write-artifact ((page index-page-artifact)) + (with-slots ((url hssg.blog.artifacts:url) + (blog hssg.blog.artifacts:blog) + (number hssg.blog.artifacts:number) + (total hssg.blog.artifacts:total) + (posts hssg.blog.artifacts:posts)) + page + (let ((artifact (make-instance 'hssg:html-artifact + :data `((:page . ,number) + (:pages . ,total) + (:blog . ,blog) + (:posts . ,posts) + (:breadcrumbs . ,(url-to-breadcrumbs url)) + ,@(slot-value blog 'hssg.blog.artifacts:initial)) + :template (hssg:chain-templates + 'hssg.blog.template:article-index + 'hssg.blog.template:index + 'hssg.blog.template:blog-page + (slot-value blog 'hssg.blog.artifacts:template)) + :output (apply #'fad:merge-pathnames-as-file + `(,(slot-value blog 'hssg.blog.artifacts:output) + ,@(mapcar #'fad:pathname-as-directory url) + ,(fad:pathname-as-file "index.html")))))) + (hssg:write-artifact artifact)))) + +(defun url-to-breadcrumbs (url) + "Converts a URL (list of strings) to breadcrums (list of a-lists)." + (maplist (lambda (items) + (let ((level (1- (length items)))) + (if (zerop level) + `((:title . ,(car items))) + `((:title . ,(car items)) + (:url . ,(format nil "~V@{~A/~:*~}" level "..")))))) + url)) diff --git a/src/blog/artifacts/post.lisp b/src/blog/artifacts/post.lisp new file mode 100644 index 0000000..65def4c --- /dev/null +++ b/src/blog/artifacts/post.lisp @@ -0,0 +1,62 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; post.lisp Implementation of the "blog post" page artifact +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.post) + +(defmethod hssg:write-artifact ((post post-artifact)) + (with-slots ((blog hssg.blog.artifacts:blog) + (slug hssg.blog.artifacts:slug) + (title hssg.blog.artifacts:title) + (content hssg.blog.artifacts:content) + (category hssg.blog.artifacts:category) + (tags hssg.blog.artifacts:tags) + (author hssg.blog.artifacts:author) + (published hssg.blog.artifacts:published) + (modified hssg.blog.artifacts:modified) + (status hssg.blog.artifacts:status) + (previous hssg.blog.artifacts:previous) + (next hssg.blog.artifacts:next) + (metadata hssg.blog.artifacts:metadata)) + post + (let ((artifact (make-instance 'hssg.artifact:html-artifact + :data `((:blog . ,blog) + (:post . ((:slug . ,slug) + (:title . ,title) + (:content . ,content) + (:category . ,category) + (:tags . ,tags) + (:author . ,author) + (:published . ,published) + (:modified . ,modified) + (:status . ,status) + ,@metadata)) + (:prev . ,previous) + (:next . ,next) + ,@(slot-value blog 'hssg.blog.artifacts:initial)) + :template (hssg:chain-templates + 'hssg.blog.template:post + 'hssg.blog.template:blog-page + (slot-value blog 'hssg.blog.artifacts:template)) + :output (fad:merge-pathnames-as-file + (slot-value blog 'hssg.blog.artifacts:output) + (hssg.blog.util:date->pathname published) + (fad:pathname-as-directory slug) + (fad:pathname-as-file "index.html"))))) + (hssg:write-artifact artifact)))) + diff --git a/src/blog/artifacts/rss-feed.lisp b/src/blog/artifacts/rss-feed.lisp new file mode 100644 index 0000000..a9098ef --- /dev/null +++ b/src/blog/artifacts/rss-feed.lisp @@ -0,0 +1,84 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; rss-feed.lisp Implementation of the "rss-feed" file artifact +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.rss-feed) + +(defmethod hssg:write-artifact ((rss-feed rss-feed-artifact)) + (with-slots ((posts hssg.blog.artifacts:posts) + (blog hssg.blog.artifacts:blog)) + rss-feed + (with-slots ((title hssg.blog.artifacts:title) + (description hssg.blog.artifacts:description) + (output hssg.blog.artifacts:output) + (url hssg.blog.artifacts:url)) + blog + (let ((artifact (make-instance 'hssg:xml-artifact + :output (apply #'fad:merge-pathnames-as-file + `(,output + ,(fad:pathname-as-file "rss.xml"))) + :data `((:rss :version "2.0") + (:channel + (:title ,title) + (:link + ,(format nil "~A/~{~A/~}" hssg:*site-url* url)) + (:description ,description) + (:generator "HSSG") + (:language ,hssg:*site-language*) + (:docs "https://www.rssboard.org/rss-2-0") + ("lastBuildDate" + ,(local-time:format-rfc1123-timestring nil (local-time:now))) + ,@(mapcar (lambda (post) + (post->rss-post post (cons hssg:*site-url* url))) + posts)))))) + (hssg:write-artifact artifact))))) + +(defun post->rss-post (post url) + "Converts one post artifact to S-XML suitable for a post inside the RSS feed" + (declare (type hssg.blog.artifacts:post-artifact post) + (type list url)) + (with-slots ((title hssg.blog.artifacts:title) + (slug hssg.blog.artifacts:slug) + (published hssg.blog.artifacts:published) + (category hssg.blog.artifacts:category) + (content hssg.blog.artifacts:content) + (blog hssg.blog.artifacts:blog)) + post + (with-slots ((top hssg.blog.artifacts:top)) + blog + (let ((link (format nil "~{~A/~}~A/~A/" url (hssg.blog.util:date->url published) slug))) + `(:item + (:title ,title) + (:link ,link) + (:description ,(detag (car content))) + ((:guid "isPermaLink" "true") ,link) + ("pubDate" ,(local-time:format-rfc1123-timestring nil published)) + ,(when category + `(:category ,category))))))) + +(defun detag (data) + "Strips DATA of all tags and concatenates the resulting strings. This leaves + only the plain-text content without any markup, which is necessary for RSS + feeds." + (declare (type (or string list) data) + (optimize (speed 3) (safety 0))) + (the string + (etypecase data + (string data) + (list + (apply #'concatenate 'string (mapcar #'detag (cdr data))))))) diff --git a/src/blog/artifacts/tag.lisp b/src/blog/artifacts/tag.lisp new file mode 100644 index 0000000..3a74c1b --- /dev/null +++ b/src/blog/artifacts/tag.lisp @@ -0,0 +1,67 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; tag.lisp Implementation of the "tag" page artifact +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.tag) + +(defmethod hssg:write-artifact ((tag tag-artifact)) + (with-slots ((blog hssg.blog.artifacts:blog) + (name hssg.blog.artifacts:name) + (posts hssg.blog.artifacts:posts)) + tag + (dolist (index-page (hssg.blog.artifact.util:collect-index-pages posts blog (list "tags" name))) + (hssg:write-artifact index-page)))) + +(defmethod hssg:write-artifact ((tags tags-artifact)) + (with-slots ((items hssg.blog.artifacts:items) + (blog hssg.blog.artifacts:blog)) + tags + (let ((artifact (make-instance 'hssg:html-artifact + :data `((:tags . ,items) + (:blog . ,blog) + ,@(slot-value blog 'hssg.blog.artifacts:initial)) + :template (hssg:chain-templates + 'hssg.blog.template:tags + 'hssg.blog.template:blog-page + (slot-value blog 'hssg.blog.artifacts:template)) + :output (fad:merge-pathnames-as-file + (slot-value blog 'hssg.blog.artifacts:output) + (fad:pathname-as-directory (hssg.blog.i18n:localise :url-tags)) + (fad:pathname-as-file "index.html"))))) + (hssg:write-artifact artifact)) + (dolist (tag items) + (hssg:write-artifact tag)))) + +(defun fetch-tag (tags name) + "Retrieve the tag object with a given NAME from a BLOG artifact; if there is + no tag object it is created and registered first." + (declare (type hssg.blog.artifacts:tags-artifact tags) + (type string name)) + (with-slots ((items hssg.blog.artifacts:items) + (blog hssg.blog.artifacts:blog)) + tags + (or (find name items :test #'string= :key #'hssg.blog.artifacts:tag-name) + (let ((result (make-instance 'tag-artifact :name name :blog blog))) + (push result items) + result)))) + +(defun add-post (tags name post) + "Adds a single POST to the TAGS collection under the key NAME." + (with-slots ((posts hssg.blog.artifacts:posts)) + (fetch-tag tags name) + (push post posts))) diff --git a/src/blog/artifacts/util.lisp b/src/blog/artifacts/util.lisp new file mode 100644 index 0000000..d83ce61 --- /dev/null +++ b/src/blog/artifacts/util.lisp @@ -0,0 +1,47 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; util.lisp Various utilities shared by all artifact implementations +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.artifact.util) + +(defun collect-index-pages (posts blog url) + "For a given list of POSTS break them into groups of size *POSTS-PER-PAGE* + and create an index page artifact for each one. Returns a list of index page + artifacts." + (declare (type list posts url) + (type hssg.blog.artifacts:blog-artifact blog)) + (do ((result '()) + (i 1 (incf i)) + previous) + ((null posts) (progn + (dolist (index result) + (setf (slot-value index 'hssg.blog.artifacts:total) (1- i))) + (nreverse result))) + (multiple-value-bind (current remainder) (hssg.blog.util:break-list posts hssg.blog:*posts-per-page*) + (let ((artifact (make-instance 'hssg.blog.artifacts:index-page-artifact + :url (if (= i 1) + url + (append url (list (format nil "~D" i)))) + :posts current :number i + :prev previous :next nil :blog blog))) + (when previous + (setf (slot-value previous 'hssg.blog.artifacts:next) artifact)) + (setf previous artifact) + (push artifact result)) + (setf posts remainder)))) + diff --git a/src/blog/config.lisp b/src/blog/config.lisp new file mode 100644 index 0000000..743707c --- /dev/null +++ b/src/blog/config.lisp @@ -0,0 +1,23 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; config.lisp Mutable global variables for configuring the blog generator +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog) + +(defparameter *posts-per-page* 10 + "How many blog posts to display per index page.") diff --git a/src/blog/i18n.lisp b/src/blog/i18n.lisp new file mode 100644 index 0000000..5751ea1 --- /dev/null +++ b/src/blog/i18n.lisp @@ -0,0 +1,84 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; i18n.lisp Internationalization interface of the blog plugin +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.i18n) + +;;; TODO: Does this really need its own package? Perhaps it would make more +;;; sense to directly make it part of the global main package instead. + +(defparameter *month-names* + '((1 . "January") + (2 . "February") + (3 . "March") + (4 . "April") + (5 . "May") + (6 . "June") + (7 . "July") + (8 . "August") + (9 . "September") + (10 . "October") + (11 . "November") + (12 . "December")) + "Association list which maps the number of a month to its name; starting at + January being 1.") + +(defparameter *default-category* "misc" + "Default category name to use for blog posts.") + +(defparameter *localisations* + '(("en" . ((:archive . "archive") + (:categories . "categories") + (:tags . "tags") + (:published . "published") + (:modified . "modified") + (:continue . "Continue reading…") + (:subscribe . "subscribe") + (:permalink . "permalink to") + (:draft . "draft") + (:draft-msg . ("This article is in draft status, set the " + (:code "status") + " metadatum to " + (:code "published") + " when ready.")) + (:url-archive . "archive") + (:url-categories . "categories") + (:url-tags . "tags") + (:url-authors . "authors")))) + "Association list of blog text navigation. Blog text is any text which is + inserted by default into the various blog pages, usually labels for + navigation or the header portion of a blog post. + + The key is the language code to use, the value is another association list. + Inside that other list the key is a unique identifier and the value is the + translation. + + There are separate identifiers for the display and for the URL. This is + intended for languages where it is desireable to differentiate between the + two, e.g. because the language uses a non-Latin alphabet or has non ASCII + characters.") + +(defun localise (identifier) + "Returns the localised value for IDENTIFIER in the current language of the + site." + (let ((localisation (assoc hssg:*site-language* *localisations* :test 'equal))) + (unless localisation + (return-from localise nil)) + (let ((entry (assoc identifier (cdr localisation)))) + (and entry (cdr entry))))) diff --git a/src/blog/package.lisp b/src/blog/package.lisp new file mode 100644 index 0000000..c664170 --- /dev/null +++ b/src/blog/package.lisp @@ -0,0 +1,137 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; package.lisp Package interfaces to the blog plugin +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(defpackage #:hssg.blog.util + (:documentation "Various helpers") + (:use :cl) + (:export range date->year break-list + date-from-numbers date->month date->day date->string date->url date->pathname + intersperse)) + +(defpackage #:hssg.blog.i18n + (:documentation "Internationalisation support") + (:use :cl) + (:export *month-names* *default-category* *localisations* localise)) + +(defpackage #:hssg.blog.period + (:use :cl) + (:export + year-period year months + month-period month days + day-period day posts + period-posts period-length)) + +(defpackage #:hssg.blog.artifacts + (:documentation "Helper package, implements all blog artifact classes") + (:use :cl :hssg.blog.util) + (:import-from #:hssg write-artifact) + (:export + blog-artifact title description posts categories tags authors periods top url output template initial static + post-artifact blog slug content category author published modified status previous next metadata + categories-artifact items + category-artifact name category-name + tags-artifact + tag-artifact tag-name + index-page-artifact number total + archive-page-artifact + rss-feed-artifact)) + +(defpackage #:hssg.blog.artifact.util + (:documentation "Helper package, various reusable utility functions.") + (:use :cl) + (:export collect-index-pages)) + +(defpackage #:hssg.blog.artifact.archive + (:use :cl) + (:import-from #:hssg.blog.artifacts archive-page-artifact)) + +(defpackage #:hssg.blog.artifacts.blog + (:use :cl) + (:import-from #:local-time encode-timestamp) + (:import-from #:hssg write-artifact) + (:import-from #:hssg.blog.util date-from-numbers date->year date->month date->day) + (:import-from #:hssg.blog.artifacts + blog-artifact posts categories tags authors periods title static output + categories-artifact items + category-artifact + tags-artifact tag-artifact + post-artifact blog slug content category tags author published modified) + (:export read-blog)) + +(defpackage #:hssg.blog.artifact.category + (:use :cl) + (:import-from #:hssg.blog.artifacts categories-artifact category-artifact) + (:export add-post)) + +(defpackage #:hssg.blog.artifact.index-page + (:use :cl) + (:import-from #:hssg.blog.artifacts index-page-artifact)) + +(defpackage #:hssg.blog.artifact.post + (:use :cl) + (:import-from #:hssg.blog.artifacts post-artifact)) + +(defpackage #:hssg.blog.artifact.rss-feed + (:use :cl) + (:import-from #:hssg.blog.artifacts rss-feed-artifact)) + +(defpackage #:hssg.blog.artifact.tag + (:use :cl) + (:import-from #:hssg.blog.artifacts tags-artifact tag-artifact) + (:export add-post)) + +(defpackage #:hssg.blog.template + (:documentation "Internal helper package, contains the SHTML templates.") + (:use :cl) + (:export blog-page post article-index index categories tags archive)) + +(defpackage #:hssg.blog.template.archive + (:documentation "Internal helper package.") + (:use :cl) + (:export archive-year->sxml archive-month->sxml archive-day->sxml archive-post->sxml)) + +(defpackage #:hssg.blog.template.blog + (:documentation "Internal helper package.") + (:use :cl) + (:export breadcrumb->sxml blog-category->sxml blog-tag->sxml period->sxml)) + +(defpackage #:hssg.blog.template.category&tag + (:documentation "Internal helper package.") + (:use :cl) + (:export category->sxml tag->sxml)) + +(defpackage #:hssg.blog.template.index + (:documentation "Internal helper package.") + (:use :cl) + (:export post->preview paginator)) + +(defpackage #:hssg.blog.template.post + (:documentation "Internal helper package.") + (:use :cl) + (:export post-tag->sxml pager->sxml)) + +(defpackage #:hssg.blog + (:documentation "CommonMark parser extension for HSSG; parses CommonMark + files to SXML trees") + (:use #:cl) + (:import-from #:hssg.blog.artifacts.blog read-blog) + (:export read-blog + *posts-per-page* + *blog-post-readers*)) diff --git a/src/blog/periods/classes.lisp b/src/blog/periods/classes.lisp new file mode 100644 index 0000000..396ebeb --- /dev/null +++ b/src/blog/periods/classes.lisp @@ -0,0 +1,42 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; classes.lisp Class definition for the various time periods a post can be in +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.period) + +(defclass year-period () + ((year :initarg :year :type integer + :documentation "The year in BC/AD.") + (months :initform '() :type list + :documentation "Months in the year which have blog posts.")) + (:documentation "A year's worth of blog posts")) + +(defclass month-period () + ((month :initarg :month :type (integer 1 12) + :documentation "The month, starting at 1 (January).") + (days :initform '() :initarg :days :type list + :documentation "Days in the month which have blog posts.")) + (:documentation "A months's worth of blog posts")) + +(defclass day-period () + ((day :initarg :day :type (integer 1 31) + :documentation "The day of the month.") + (posts :initform '() :initarg :posts :type list + :documentation "Posts of the day.")) + (:documentation "A day's worth of blog posts")) diff --git a/src/blog/periods/protocol.lisp b/src/blog/periods/protocol.lisp new file mode 100644 index 0000000..27ac648 --- /dev/null +++ b/src/blog/periods/protocol.lisp @@ -0,0 +1,39 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; protocol.lisp Implementation of the period class protocol +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.period) + +(defgeneric period-posts (period) + (:documentation "Returns all the posts cotained inside the PERIOD.") + (:method ((period year-period)) + (apply #'concatenate 'list (mapcar #'period-posts (slot-value period 'months)))) + (:method ((period month-period)) + (apply #'concatenate 'list (mapcar #'period-posts (slot-value period 'days)))) + (:method ((period day-period)) + (slot-value period 'posts))) + +(defgeneric period-length (period) + (:documentation "Returns the total number of posts for a given period") + (:method ((period year-period)) + (reduce #'+ (mapcar #'period-length (slot-value period 'months)))) + (:method ((period month-period)) + (reduce #'+ (mapcar #'period-length (slot-value period 'days)))) + (:method ((period day-period)) + (length (slot-value period 'posts)))) diff --git a/src/blog/readers/lisp.lisp b/src/blog/readers/lisp.lisp new file mode 100644 index 0000000..d8b1145 --- /dev/null +++ b/src/blog/readers/lisp.lisp @@ -0,0 +1,52 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; lisp.lisp Implementation of the lisp reader +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . + +(defpackage #:hssg.blog.reader.lisp + (:documentation "Helper package which exposes the Lisp blog post reader.") + (:use :cl) + (:export read-lisp)) +(in-package #:hssg.blog.reader.lisp) + +;;; TODO: should use the reader from the HSSG core +(defmacro with-package ((&rest using) &body body) + "Evaluate the BODY expressions within a separate package." + ;; FIXME: the new package is not actually used. + (let ((name (gensym)) + (package (gensym))) + `(let* ((,name (gensym)) + (,package (make-package ,name :use (list ,@using)))) + (unwind-protect (let ((*PACKAGE* ,package)) + ,@body) + (delete-package ,package))))) + + +(defun read-lisp (pathname) + "Reads the contents of a Common Lisp file into a blog post" + (declare (type pathname pathname)) + (let ((data (with-open-file (*STANDARD-INPUT* pathname) + (with-package (:cl) + (loop for exp = (read *STANDARD-INPUT* nil) + with value + if (null exp) return value + else do (setf value (eval exp))))))) + data)) + +(setf (gethash "lisp" hssg.blog:*BLOG-POST-READERS*) #'read-lisp) diff --git a/src/blog/readers/readers.lisp b/src/blog/readers/readers.lisp new file mode 100644 index 0000000..9ceced9 --- /dev/null +++ b/src/blog/readers/readers.lisp @@ -0,0 +1,25 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; readers.lisp Manage parsers for the different kinds of blog posts +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog) + +(defparameter *blog-post-readers* (make-hash-table :test 'equal) + "Maps a type string to a reader function; add new reader implementations + here.") diff --git a/src/blog/template/archive.lisp b/src/blog/template/archive.lisp new file mode 100644 index 0000000..abe13f7 --- /dev/null +++ b/src/blog/template/archive.lisp @@ -0,0 +1,60 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; archive.lisp Helper functions for the archive template +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.template.archive) + +(defun archive-year->sxml (period) + (declare (type hssg.blog.period:year-period period)) + (with-slots ((year hssg.blog.period:year) + (months hssg.blog.period:months)) + period + `(:li ((:a :href ,(format nil "../~D/" year)) + ,(format nil "~D" year)) + (:ul ,@(mapcar (lambda (month) + (archive-month->sxml month year)) + months))))) + +(defun archive-month->sxml (period year) + (declare (type hssg.blog.period:month-period period)) + (with-slots ((month hssg.blog.period:month) + (days hssg.blog.period:days)) + period + (let ((name (cdr (assoc month hssg.blog.i18n:*month-names*)))) + `(:li ((:a :href ,(format nil "../~D/~2,'0D/" year month)) + ,(format nil "~A" name)) + (:ul ,@(apply #'concatenate 'list (mapcar (lambda (day) + (archive-day->sxml day year month)) + days))))))) + +(defun archive-day->sxml (period year month) + (declare (type hssg.blog.period:day-period period)) + (with-slots ((day hssg.blog.period:day) + (posts hssg.blog.period:posts)) + period + (mapcar (lambda (post) + (archive-post->sxml post year month day)) + posts))) + +(defun archive-post->sxml (post year month day) + (declare (type hssg.blog.artifacts:post-artifact post) + (type integer year month day)) + (with-slots ((title hssg.blog.artifacts:title) + (slug hssg.blog.artifacts:slug)) post + `(:li ((:a :href ,(format nil "../~D/~2,'0D/~2,'0D/~A/" year month day slug)) + ,title)))) diff --git a/src/blog/template/blog.lisp b/src/blog/template/blog.lisp new file mode 100644 index 0000000..82948fb --- /dev/null +++ b/src/blog/template/blog.lisp @@ -0,0 +1,60 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; blog.lisp Helper functions for the blog page template +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.template.blog) + +(defun breadcrumb->sxml (item) + "Convert a breadcrumb entry from the items to an SXML tree" + (declare (type list item)) + (hssg:let-metadata item ((title :title) + (url :url)) + `((:li :class ,(if url "" "active")) + ,(if url + `((:a :href ,url) ,title) + title)))) + +(defun blog-category->sxml (category blog-url) + "Convert an item from the categories alist into an SXML tree." + (declare (type hssg.blog.artifacts:category-artifact category) + (type list blog-url)) + (with-slots ((name hssg.blog.artifacts:name) + (posts hssg.blog.artifacts:posts)) + category + `(:li + ((:a :href ,(format nil "/~{~A/~}~A/~A/" blog-url (hssg.blog.i18n:localise :url-categories) name)) + ,(format nil "~A (~D)" name (length posts)))))) + +(defun blog-tag->sxml (tag blog-url) + "Convert an item from the tags list into an SXML tree." + (declare (type hssg.blog.artifacts:tag-artifact tag) + (type list blog-url)) + (with-slots ((name hssg.blog.artifacts:name) + (posts hssg.blog.artifacts:posts)) + tag + `(:li + ((:a :href ,(format nil "/~{~A/~}~A/~A/" blog-url (hssg.blog.i18n:localise :url-tags) name)) + ,(format nil "~A (~D)" name (length posts)))))) + +(defun period->sxml (period blog-url) + (declare (type hssg.blog.period:year-period period) + (type list blog-url)) + (let ((year (slot-value period 'hssg.blog.period:year))) + `(:li + ((:a :href ,(format nil "/~{~A/~}~D/" blog-url year)) + ,(format nil "~D (~D)" year (hssg.blog.period:period-length period)))))) diff --git a/src/blog/template/category-tag.lisp b/src/blog/template/category-tag.lisp new file mode 100644 index 0000000..700544a --- /dev/null +++ b/src/blog/template/category-tag.lisp @@ -0,0 +1,39 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; category-tag.lisp Helper functions for the category- and tag templates +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.template.category&tag) + +(defun category->sxml (category) + (declare (type hssg.blog.artifacts:category-artifact category)) + (with-slots ((name hssg.blog.artifacts:name) + (posts hssg.blog.artifacts:posts)) + category + `(:li + ((:a :href ,(format nil "~A/" name)) ,name) + ,(format nil " (~D)" (length posts))))) + +(defun tag->sxml (tag) + (declare (type hssg.blog.artifacts:tag-artifact tag)) + (with-slots ((name hssg.blog.artifacts:name) + (posts hssg.blog.artifacts:posts)) + tag + `(:li + ((:a :href ,(format nil "~A/" name)) ,name) + ,(format nil " (~D)" (length posts))))) + diff --git a/src/blog/template/index.lisp b/src/blog/template/index.lisp new file mode 100644 index 0000000..64d5401 --- /dev/null +++ b/src/blog/template/index.lisp @@ -0,0 +1,105 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; index.lisp Helper functions for the index template +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.template.index) + +(defun post->preview (post) + "Converts one post artifact to an SHTML tree of its preview." + (declare (type hssg.blog.artifacts:post-artifact post)) + (with-slots ((title hssg.blog.artifacts:title) + (content hssg.blog.artifacts:content) + (published hssg.blog.artifacts:published) + (modified hssg.blog.artifacts:modified) + (blog hssg.blog.artifacts:blog) + (slug hssg.blog.artifacts:slug)) post + (with-slots ((url hssg.blog.artifacts:url)) blog + (let ((url (format nil "/~{~A/~}~A/~A/" url (hssg.blog.util:date->url published) slug))) + `(:li + (:article + (:h1 ((:a :href ,url) ,title)) + ((:header :class "blog-post-header") + (:p + ((:time :datetime ,(hssg.blog.util:date->string published)) + ,(format nil "~A: ~A" (hssg.blog.i18n:localise :published) + (hssg.blog.util:date->string published)))) + ,(if modified + `(:p + ((:time :datetime ,(hssg.blog.util:date->string modified)) + ,(format nil "~A: ~A" (hssg.blog.i18n:localise :modified) + (hssg.blog.util:date->string modified)))))) + ,(car content) + ,(if (cdr content) + `(:p + ((:a :href ,url) + ,(hssg.blog.i18n:localise :continue)))))))))) + +(defun paginator (current-page total-pages) + (declare (type (integer 1) current-page total-pages)) + (flet ((page-index->sxml (index &optional (label nil)) + `((:li :class ,(if (= index current-page) "active" "")) + ,(if (= index current-page) + ;; Only display the number, but don't make it a hyperlink + `(:a + ,(format nil "~A" index)) + ;; Make it a hyperlink + `((:a :href ,(cond + ((= 1 current-page) (format nil "./~A/" index)) + ((= 1 index) "..") + (t (format nil "../~A/" index)))) + ,(if label label (format nil "~A" index))))))) + (cond + ;; If there are fewer than eight pages display them all + ((< total-pages 8) + (mapcar #'page-index->sxml (hssg.blog.util:range 1 total-pages))) + ;; There are more than eight pages, but we are still in the first batch + ((< current-page 5) + `(;; Display a previous item if We are not on the first page + ,(if (> current-page 1) + (funcall #'page-index->sxml (1- current-page) `((:span :aria-hidden "true") "‹")) + '()) + ;; The first seven pages + ,@(mapcar #'page-index->sxml (hssg.blog.util:range 1 7)) + ;; The next page + ,(funcall #'page-index->sxml (1+ current-page) `((:span :aria-hidden "true") "›")) + ;; The last page + ,(funcall #'page-index->sxml total-pages `((:span :aria-hidden "true") "»")))) + ;; Similar to the previous case, but in the last batch + ((> current-page (- total-pages 4)) + `(;; The first page + ,(funcall #'page-index->sxml 1 `((:span :aria-hidden "true") "«")) + ;; The previous page + ,(funcall #'page-index->sxml (1- current-page) `((:span :aria-hidden "true") "‹")) + ;; The last seven pages + ,@(mapcar #'page-index->sxml (hssg.blog.util:range (- total-pages 6) total-pages)) + ;; Display a next page if we are not on the last page + ,(if (< current-page total-pages) + (funcall #'page-index->sxml (1+ current-page) `((:span :aria-hidden "true") "›")) + '()))) + ;; Somewhere in the middle, display everything + (t + `(;; The first page + ,(funcall #'page-index->sxml 1 `((:span :aria-hidden "true") "«")) + ;; The previous page + ,(funcall #'page-index->sxml (1- current-page) `((:span :aria-hidden "true") "‹")) + ;; ... + ,@(mapcar #'page-index->sxml (hssg.blog.util:range (- current-page 3) (+ current-page 3))) + ;; The next page + ,(funcall #'page-index->sxml (1+ current-page) `((:span :aria-hidden "true") "›")) + ;; The last page + ,(funcall #'page-index->sxml total-pages `((:span :aria-hidden "true") "»"))))))) diff --git a/src/blog/template/post.lisp b/src/blog/template/post.lisp new file mode 100644 index 0000000..090289e --- /dev/null +++ b/src/blog/template/post.lisp @@ -0,0 +1,55 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; post.lisp Helper functions for the post template +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.template.post) + +(defun post-tag->sxml (tag) + "Convert one TAG name into a hyperlink to the index of the tag." + (declare (type string tag)) + (let ((tags-url (hssg.blog.i18n:localise :url-tags))) + `((:a :href ,(format nil "../../../../~A/~A/" tags-url tag)) + ,tag))) + +(defun pager->sxml (prev next) + "Build the SXML tree for the pager of a blog post." + (declare (type (or null hssg.blog.artifacts:post-artifact) prev next) ) + (flet ((item->sxml (item rel symbol next?) + (declare (type hssg.blog.artifacts:post-artifact item) + (type string rel symbol) + (type boolean next?)) + (with-slots ((title hssg.blog.artifacts:title) + (slug hssg.blog.artifacts:slug) + (published hssg.blog.artifacts:published) + (blog hssg.blog.artifacts:blog)) + item + `((:a :href ,(format nil "/~{~A/~}~A/~A/" (slot-value blog 'hssg.blog.artifacts:url) + (hssg.blog.util:date->url published) + slug) + :rel ,rel + :style ,(format nil "float: ~A" (if next? "right" "left"))) + ,@(if next? + `(,title " " ((:span :aria-hidden "true") ,symbol)) + `(((:span :aria-hidden "true") ,symbol) " " ,title)))))) + `((:nav :class "blog-pager") + ,(if prev + (item->sxml prev "previous" "←" nil) + '((:a :hidden "hidden") "")) + ,(if next + (item->sxml next "next" "→" t) + '((:a :style "display: none;") ""))))) diff --git a/src/blog/templates.lisp b/src/blog/templates.lisp new file mode 100644 index 0000000..4ec8e65 --- /dev/null +++ b/src/blog/templates.lisp @@ -0,0 +1,245 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; templates.lisp Templates used by blog pages +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.template) + +(hssg:deftemplate blog-page (breadcrumbs content blog css) + "The blog template is a wrapper for all blog-related pages (individual + articles, archives, categories,...). A blog page itself may be further + processed by other templates. + + Base template for all blog-related pages, the result is to be spliced into a + page. + + Required metadata: + + - breadcrumbs List of a-lists; each a-list is a breadcrumb item + - content The content to display in the middle of the page, e.g. an + entire blog post, a listing of categories or a listing of blog + post previews. + - blog Metadata about the current blog to process. + - categories Association list of categories. The key is the name of the + category (a string), the value is a list of posts in that + category. + - tags the same as categories, except for tags." + (:css + (cons "/css/blog.css" css)) + (:content + (let ((url (slot-value blog 'hssg.blog.artifacts:url))) + `(((:div :class "blog") + ((:nav :class "breadcrumbs" :aria-label "Breadcrumbs") + (:ol + ,@(mapcar #'hssg.blog.template.blog:breadcrumb->sxml breadcrumbs))) + ,@content + ;; Left pillar, article navigation + ((:nav :class "blog-navigation" :aria-label "Blog navigation") + ;; This navigator contains links to the various archive types. + (:aside + (:span ,(format nil "~A:" (hssg.blog.i18n:localise :subscribe))) + " " + ((:a :href ,(format nil "/~{~A/~}rss.xml" url) :type "application/rss+xml") + "RSS")) + (:nav + (:h1 + ((:a :href ,(format nil "/~{~A/~}~A/" url (hssg.blog.i18n:localise :url-archive))) + ,(hssg.blog.i18n:localise :archive))) + (:ul + ; For each year display a year link to that year's archive. If the + ; year is the year of the current post display a sub-list for that year + ,@(mapcar (lambda (period) (funcall #'hssg.blog.template.blog:period->sxml period url)) + (slot-value blog 'hssg.blog.artifacts:periods)))) + (:nav + (:h1 + ((:a :href ,(format nil "/~{~A/~}~A/" url (hssg.blog.i18n:localise :url-categories))) + ,(hssg.blog.i18n:localise :categories))) + (:ul + ,@(let ((categories (slot-value (slot-value blog 'hssg.blog.artifacts:categories) + 'hssg.blog.artifacts:items))) + (mapcar #'hssg.blog.template.blog:blog-category->sxml + categories + (make-sequence 'list (length categories) :initial-element url))))) + (:nav + (:h1 + ((:a :href ,(format nil "/~{~A/~}~A/" url (hssg.blog.i18n:localise :url-tags))) + ,(hssg.blog.i18n:localise :tags))) + (:ul + ,@(let ((tags (slot-value (slot-value blog 'hssg.blog.artifacts:tags) 'hssg.blog.artifacts:items))) + (mapcar #'hssg.blog.template.blog:blog-tag->sxml + tags + (make-sequence 'list (length tags) :initial-element url))))))))))) + +(hssg:deftemplate archive (periods blog) + (:title (hssg.blog.i18n:localise :archive)) + (:url (hssg.blog.i18n:localise :url-archive)) + (:breadcrumbs `(((:title . ,(slot-value blog 'hssg.blog.artifacts:top)) + (:url . "../")) + ((:title . ,(hssg.blog.i18n:localise :archive))))) + (:content + `(((:main :id "archive") + (:ul + ,@(mapcar #'hssg.blog.template.archive:archive-year->sxml periods)))))) + +(hssg:deftemplate categories (blog categories) + "Concrete template for all categories in the blog. Does not generate any + actual content, only sets up the metadata for the next step in the pipeline. + + Required metadata: + - blog Information about the blog + - categories List of association list of categories" + (:title (hssg.blog.i18n:localise :categories)) + (:url (hssg.blog.i18n:localise :url-categories)) + (:breadcrumbs + `(((:title . ,(slot-value blog 'hssg.blog.artifacts:top)) + (:url . "../")) + ((:title . ,(hssg.blog.i18n:localise :categories))))) + (:content + `((:ul + ,@(mapcar #'hssg.blog.template.category&tag:category->sxml categories))))) + +(hssg:deftemplate tags (blog tags) + "Concrete template for all tags in the blog. Does not generate any actual + content, only sets up the metadata for the next step in the pipeline. + + Required metadata: + - blog Information about the blog + - tags List of association list of tags" + (:title (hssg.blog.i18n:localise :tags)) + (:url (hssg.blog.i18n:localise :url-tags)) + (:breadcrumbs + `(((:title . ,(slot-value blog 'hssg.blog.artifacts:top)) + (:url . "../")) + ((:title . ,(hssg.blog.i18n:localise :tags))))) + (:content + `((:ul + ,@(mapcar #'hssg.blog.template.category&tag:tag->sxml tags))))) + +(hssg:deftemplate article-index (pages page posts) + "Template for an article index; article indices list articles for a give + group (e.g. a category). The content is a sequence of articles, usually + showing their title with a link to the full article, and a preview of the + full article. + + Required metadata: + - pages Number of pages in the paginator + - page Number of the current pagination page + - posts List of posts contained inside this index page." + (:content `(((:main :class "blogpost-listing") + (:ul + ,@(mapcar #'hssg.blog.template.index:post->preview posts))) + ,(if (> pages 1) + `((:nav :class "paginator") + (:ul + ,@(funcall #'hssg.blog.template.index:paginator page pages))) + '())))) + +(hssg:deftemplate index (blog breadcrumbs) + "Template for the blog main index, to be spliced into the blog template. + + The main index of the blog is what the user first sees when visiting the + blog. It displays all articles (paginated of course) from newest to oldest." + (:breadcrumbs + `(((:title . ,(slot-value blog 'hssg.blog.artifacts:top)) + ;; Number of levels must be based on the number of previous elements + (:url . ,(format nil "~V@{~A/~:*~}" (length breadcrumbs) ".."))) + ,@breadcrumbs))) + + +(hssg:deftemplate post (blog post prev next) + "Template for an individual blog post. Required metadata: + + - blog Information on the blog + - post The current post to display + + Optional metadata: + + - prev Previous post + - next Next post" + + (:css + (hssg:let-metadata post ((css :css)) + css)) + (:periods + (with-slots ((periods hssg.blog.artifacts:periods )) + blog + periods)) + (:breadcrumbs + (hssg:let-metadata post ((title :title) + (date :published) + (slug :slug)) + `(((:title . ,(slot-value blog 'hssg.blog.artifacts:top)) + (:url . "../../../../")) + ((:title . ,(hssg.blog.util:date->year date)) + (:url . "../../../")) + ((:title . ,(hssg.blog.util:date->month date)) + (:url . "../../")) + ((:title . ,(hssg.blog.util:date->day date))) + ((:title . ,slug))))) + (:content + (hssg:let-metadata post ((status :status :published) + (title :title) + (date :published) + (modified :modified) + (category :category) + (tags :tags '()) + (author :author) + (content :content)) + `(((:main :class "blogpost") + (:article + (:h1 + ((:a :href "." :title ,(format nil "~A ~A" (hssg.blog.i18n:localise :permalink) title) :rel "bookmark") + ,title)) + ((:header :class "blog-post-header") + ;; Draft status notification + ,(if (eq status :draft) + `((:aside :class "alert alert-info" :role "alert") + (:strong (format nil "~A:" ,(hssg.blog.i18n:localise :draft))) + ,@(hssg.blog.i18n:localise :draft-msg)) + nil) + ;; Date of the blog post + ((:p :class "blog-post-published") + ,(format nil "~A: " (hssg.blog.i18n:localise :published)) + ((:time :datetime ,(hssg.blog.util:date->string date)) + ,(hssg.blog.util:date->string date))) + ,(if modified + `((:p :class "blog-post-modified") + ,(format nil "~A: " (hssg.blog.i18n:localise :modified)) + ((:time :datetime ,(hssg.blog.util:date->string modified)) + ,(hssg.blog.util:date->string modified))) + nil) + ;; Category of the blog post, if any + ,(if category + `((:p :class "blog-post-category") + ,(format nil "~A: " (hssg.blog.i18n:localise :categories)) + ((:a :href ,(format nil "../../../../~A/~A/" (hssg.blog.i18n:localise :url-categories) + category)) + ,category)) + nil) + ;; Tags of the blog post, if any + ,(if tags + `((:p :class "blog-post-tags") + ,(format nil "~A: " (hssg.blog.i18n:localise :tags)) + ,@(hssg.blog.util:intersperse (mapcar #'hssg.blog.template.post:post-tag->sxml tags) ", ")) + nil) + ;; Author of the blog post, if any + ,(if author nil nil) ; Just a stub + ) + ,@content) + ,(if (or prev next) + (hssg.blog.template.post:pager->sxml prev next) + nil)))))) diff --git a/src/blog/util.lisp b/src/blog/util.lisp new file mode 100644 index 0000000..bef99f8 --- /dev/null +++ b/src/blog/util.lisp @@ -0,0 +1,89 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; util.lisp Various utilities used throughout the blog plugin +;;;; +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.blog.util) + +(defun range (from to) + "Produce a list of integers FROM to TO (both inclusive)" + (loop for i from from to to collecting i)) + +(defun break-list (list n) + "Breaks a LIST in two where the first N elements are in a new list and the + remainder is the CDR of the original LIST." + (do ((lhs '()) + (rhs list) + (i n (decf i))) + ((or (zerop i) (null rhs)) + (values (nreverse lhs) rhs)) + (push (car rhs) lhs) + (pop rhs))) + +(defun date-components (date) + (values (local-time:timestamp-year date) + (local-time:timestamp-month date) + (local-time:timestamp-day date))) + +(defun date-from-numbers (year month day) + "Creates a date object with given integer YEAR, MONTH and DAY." + (local-time:encode-timestamp 0 0 0 0 day month year :timezone local-time:+utc-zone+)) + +(defun date->year (date) + "Format the year component a timestamp object to a string." + (local-time:format-timestring nil date :format '(:year))) + +(defun date->month (date) + "Format the month component a timestamp object to a string." + (multiple-value-bind (year month day) (date-components date) + (declare (ignore year day)) + (format nil "~2,'0D" month))) + +(defun date->day (date) + "Format a day component of timestamp object to a string" + (multiple-value-bind (year month day) (date-components date) + (declare (ignore year month)) + (format nil "~2,'0D" day))) + +(defun date->string (date) + "Format a timestamp object to a string of the form \"year-month-day\"." + (local-time:format-rfc3339-timestring nil date :omit-time-part t)) + +(defun date->url (date) + "Format a timestamp object to a URL path string of the form + \"year/month/day\"." + (multiple-value-bind (year month day) (date-components date) + (format nil "~D/~2,'0D/~2,'0D" year month day))) + +(defun date->pathname (date) + "Format a timestamp object to a directory pathname of the form + #p\"year/month/day\"." + (multiple-value-bind (year month day) (date-components date) + (let ((year (format nil "~D" year)) + (month (format nil "~2,'0D" month)) + (day (format nil "~2,'0D" day))) + (apply #'fad:merge-pathnames-as-directory + (mapcar #'fad:pathname-as-directory (list year month day)))))) + +(defun intersperse (list item) + "For a given LIST return a new list with ITEM interspersed between each list + item." + (let ((result '())) + (dolist (current list (nreverse (cdr result))) + (push current result) + (push item result)))) diff --git a/src/cmark-reader/main.lisp b/src/cmark-reader/main.lisp new file mode 100644 index 0000000..b9f821d --- /dev/null +++ b/src/cmark-reader/main.lisp @@ -0,0 +1,142 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; config.lisp Mutable global variables for configuring the entire generator +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . + +(defpackage #:hssg.blog.reader.cmark + (:documentation "Helper package which exposes the CommonMark blog post reader.") + (:use :cl) + (:export read-common-mark)) +(in-package #:hssg.blog.reader.cmark) + +(defmacro string-ecase (keyform &body cases) + "Like ECASE, except that it uses strings as keys." + (let ((key (gensym))) + `(let ((,key ,keyform)) + (cond + ,@(mapcar #'(lambda (case) + `((string-equal ,(car case) ,key) ,@(cdr case))) + cases) + (t (error "String key \"~A\" fell through" ,key)))))) + +(defun read-common-mark (file-name) + "Reads the contents of a CommonMark file into a blog post" + (declare (type pathname file-name)) + (with-open-file (input file-name :direction :input) + (let ((metadata (loop with line = (read-line input) + until (> (ppcre:count-matches "----*" line) 0) + if (> (length line) 0) + collect line + do (setf line (read-line input)))) + (content (cmark:parse-stream input))) + (cons (cons :content (read-cmark-node content)) + (mapcar #'parse-metadata-line metadata))))) + +(setf (gethash "md" hssg.blog:*BLOG-POST-READERS*) #'read-common-mark) + +(defun parse-metadata-line (line) + "Parses a metadata string and returns the resulting metadata pair." + (destructuring-bind (key value) (ppcre:split ":\\s+" line :limit 2) + (string-ecase key + ("title" (cons :title value)) + ("author" (cons :author value)) + ("published" (cons :published (local-time:parse-timestring value))) + ("modified" (cons :modified (local-time:parse-timestring value))) + ("category" (cons :category value)) + ("tags" (cons :tags (ppcre:split ",\\s*" value)))))) + +(defgeneric read-cmark-node (node) + (:documentation "Reads a CommonMark node into an SXML expression.") + (:method ((node cmark:node)) + (error "CommonMark reader not implemented for ~A" (class-of node)))) + +(defmethod read-cmark-node ((node cmark:document-node)) + (mapcar #'read-cmark-node (cmark:node-children node))) + +(defmethod read-cmark-node ((node cmark:block-quote-node)) + `(:blockquote ,@(mapcar #'read-cmark-node (cmark:node-children node)))) + +(defmethod read-cmark-node ((node cmark:list-node)) + (let ((type (cmark:node-list-type node)) + (start (cmark:node-list-start node))) + `(,(ecase type + (:cmark-bullet-list :ul) + (:cmark-ordered-list (if (and start (not (= start 1))) + (list :ol :start start) + :ol))) + ,@(mapcar #'read-cmark-node (cmark:node-children node))))) + +(defmethod read-cmark-node ((node cmark:item-node)) + `(:li ,@(mapcar #'read-cmark-node (cmark:node-children node)))) + +(defmethod read-cmark-node ((node cmark:code-block-node)) + `(:pre + (:code ,(cmark:node-literal node)))) + +(defmethod read-cmark-node ((node cmark:paragraph-node)) + `(:p ,@(mapcar #'read-cmark-node (cmark:node-children node)))) + +(defmethod read-cmark-node ((node cmark:heading-node)) + `(,(ecase (cmark:node-heading-level node) + (1 :h1) + (2 :h2) + (3 :h3) + (4 :h4) + (5 :h5) + (6 :h6) + (7 :h7)) + ,@(mapcar #'read-cmark-node (cmark:node-children node)))) + +(defmethod read-cmark-node ((node cmark:thematic-break-node)) + (list :br)) + +(defmethod read-cmark-node ((node cmark:text-node)) + (cmark:node-literal node)) + +(defmethod read-cmark-node ((node cmark:softbreak-node)) + " +") + +(defmethod read-cmark-node ((node cmark:linebreak-node)) + " +") + +(defmethod read-cmark-node ((node cmark:code-node)) + `(:code ,(cmark:node-literal node))) + +(defmethod read-cmark-node ((node cmark:emph-node)) + `(:em ,@(mapcar #'read-cmark-node (cmark:node-children node)))) + +(defmethod read-cmark-node ((node cmark:strong-node)) + `(:strong ,@(mapcar #'read-cmark-node (cmark:node-children node)))) + +(defmethod read-cmark-node ((node cmark:link-node)) + (let ((url (cmark:node-url node)) + (title (cmark:node-title node))) + `((:a :href ,url ,@(if (> (length title) 0) `(:title ,title) nil)) + ,@(mapcar #'read-cmark-node (cmark:node-children node))))) + +(defmethod read-cmark-node ((node cmark:image-node)) + (let ((url (cmark:node-url node)) + (title (cmark:node-title node)) + (children (cmark:node-children node))) + `((:img :src ,url + ,@(if (> (length title) 0) `(:title ,title) nil) + ,@(if children `(:alt ,@(mapcar #'read-cmark-node children)) nil))))) + +(hssg.reader:register-reader "md" #'read-common-mark) diff --git a/src/hssg/artifact.lisp b/src/hssg/artifact.lisp new file mode 100644 index 0000000..8db30b0 --- /dev/null +++ b/src/hssg/artifact.lisp @@ -0,0 +1,24 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; artifact.lisp Public artifact protocol +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.artifact) + +(defgeneric write-artifact (artifact) + (:documentation "Write the given ARTIFACT to disc. The artifact's state + determines where and how the artifact will be written.")) diff --git a/src/hssg/artifacts/classes.lisp b/src/hssg/artifacts/classes.lisp new file mode 100644 index 0000000..84f5b65 --- /dev/null +++ b/src/hssg/artifacts/classes.lisp @@ -0,0 +1,57 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; classes.lisp Artifact class definitions +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.artifact) + +(defclass verbatim-artifact () + ((input :reader verbatim-artifact-input :initarg :input :type pathname + :documentation "Path to the file to copy") + (output :initarg :output :type pathname + :documentation "Path where to write the produced artifact.")) + (:documentation "Artifact which copies one filed to another location")) + +(defclass directory-artifact () + ((input :reader directory-artifact-input :initarg :input :type pathname + :documentation "Path to the directory to copy recursively") + (output :initarg :output :type pathname + :documentation "Path where to write the produced artifact.")) + (:documentation "Artifact which copies a directory and its contents recursively to another location")) + +(defclass compound-artifact () + ((artifacts :initarg :artifacts :initform '() :type list + :documentation "List of wrapped artifacts")) + (:documentation + "A higher-order artifact which contains any number of other artifacts.")) + +(defclass html-artifact () + ((data :initarg :data :type list + :documentation "Alist of SXML trees of the page data; does not have to be +serializable by Spinneret initially, but needs to be eventually.") + (template :reader artifact-template :initarg :template :type function + :documentation "Template to apply to the data") + (output :reader artifact-output :initarg :output :type pathname + :documentation "Path where to write the produced artifact.")) + (:documentation "Will produce an HTML file.")) + +(defclass xml-artifact () + ((data :initarg :data :type list + :documentation "SXML tree of the XML file in PITHY-XML format.") + (output :initarg :output :type pathname + :documentation "Path where to write the produced artifact.")) + (:documentation "Will produce an XML file.")) diff --git a/src/hssg/artifacts/compound.lisp b/src/hssg/artifacts/compound.lisp new file mode 100644 index 0000000..b89abb9 --- /dev/null +++ b/src/hssg/artifacts/compound.lisp @@ -0,0 +1,26 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; compound.lisp Compound artifact implementation +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see .(in-package #:hssg.artifact) +(in-package #:hssg.artifact) + +(defmethod write-artifact ((wrapper compound-artifact)) + "Writes each of the artifacts inside the WRAPPER individually." + (with-slots ((artifacts hssg.artifact::artifacts)) wrapper + (dolist (artifact artifacts) + (write-artifact artifact)))) diff --git a/src/hssg/artifacts/directory.lisp b/src/hssg/artifacts/directory.lisp new file mode 100644 index 0000000..c11c933 --- /dev/null +++ b/src/hssg/artifacts/directory.lisp @@ -0,0 +1,48 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; verbatim.lisp Verbatim artifact implementation +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.artifact.directory) + +(defmethod hssg.artifact:write-artifact ((artifact hssg.artifact:directory-artifact)) + (with-slots ((input hssg.artifact::input) + (output hssg.artifact::output)) + artifact + ;; The INPUT contains the directory to copy, but we need its parent to + ;; construct file paths + (let ((content-dir (merge-pathnames (fad:pathname-parent-directory input))) + (output-dir (merge-pathnames (fad:pathname-parent-directory output)))) + (fad:walk-directory + input + (lambda (pathname) + (let* ((relative (enough-namestring pathname content-dir)) + (target (merge-pathnames relative output-dir))) + (ensure-directories-exist target) + (fad:copy-file pathname target :overwrite t)))))) + nil) + +(defun make-directory-artifact (file-path input-dir output-dir) + "Constructor for a verbatim directory artifact which copies an entire + directory FILE-PATH and its contents recursively from INPUT-DIR to + OUTPUT-DIR." + (declare (type (or string pathname) file-path input-dir output-dir)) + (let ((input (merge-pathnames file-path (fad:pathname-as-directory input-dir))) + (output (merge-pathnames file-path (fad:pathname-as-directory output-dir)))) + (make-instance 'hssg.artifact:directory-artifact + :input (fad:pathname-as-directory input) + :output (fad:pathname-as-directory output)))) diff --git a/src/hssg/artifacts/html.lisp b/src/hssg/artifacts/html.lisp new file mode 100644 index 0000000..369a8d6 --- /dev/null +++ b/src/hssg/artifacts/html.lisp @@ -0,0 +1,123 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; html.lisp HTML artifact implementation +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.artifact.html) + +(defun read-html-lisp (fpath output &optional &key (template #'identity) (initial nil)) + "Read the contents of an HTML document from a Lisp file." + (declare (type (or string pathname) fpath output)) + (let ((data (funcall (hssg.reader:get-reader "lisp") fpath))) + (make-instance 'hssg.artifact:html-artifact :data (append data initial) + :output output + :template template))) + +(defun normalize (symbol) + "Convert an atom to its lower-case representation." + (declare (type atom symbol) + (optimize (speed 3) (safety 0))) + (etypecase symbol + (string (string-downcase symbol)) + (symbol (string-downcase (symbol-name symbol))) + (atom (string-downcase (princ-to-string symbol))))) + +(defun set-attributes (node attributes) + "For a given NODE set its attributes, where ATTRIBUTES is an alternating list + of key-value items." + (declare (type plump:element node) + (type list attributes) + (optimize (speed 3) (safety 0))) + (loop for (attribute value) on attributes by #'cddr + when value + do (setf (plump:attribute node (normalize attribute)) + (if (eq value t) + (normalize attribute) + (princ-to-string value))))) + +(defun sexp->plump-tree (sexp &optional (parent (plump:make-root))) + "Convert an s-expression (atom or tree) into a tree of cl-plump nodes. The + conversion details depend on the type of s-expression: + - Strings become plain text nodes + - Symbols are first normalised to strings, then converted + - Lists become trees with children" + (declare (type plump:node parent) + (type (or string symbol list) sexp) + (optimize (speed 3) (safety 0))) + (the plump:element + (etypecase sexp + (string (plump:make-text-node parent sexp)) + (symbol (sexp->plump-tree (normalize sexp) parent)) + (list + (let* ((head (first sexp)) + (tail (rest sexp)) + (tag (if (atom head) head (first head))) + (attributes (if (atom head) '() (rest head))) + (node (plump:make-element + parent (normalize tag) + :children (plump:make-child-array (length tail))))) + (set-attributes node attributes) + ;; Children will be automatically added to NODE as a side effect + (loop for child in tail when child do (sexp->plump-tree child node)) + node))))) + +(defmethod hssg.artifact:write-artifact ((artifact hssg.artifact:html-artifact)) + (with-slots ((data hssg.artifact::data) + (template hssg.artifact::template) + (output hssg.artifact::output)) + artifact + (ensure-directories-exist output) + (with-open-file (*STANDARD-OUTPUT* output :direction :output :if-exists :supersede :if-does-not-exist :create) + (let ((plump:*tag-dispatchers* plump:*xml-tags*)) + (format t "~%") + (plump:serialize (sexp->plump-tree + (cdr (assoc :content (funcall template data))))))))) + +(defmacro static-page ((&rest bindings) &body content) + "Static page DSL macro + + This macro defines a static website with a syntax similar to Lisp's + let*-expressions. The first argument is a list of (BINDING KEY VALUE) + triples, the body is the content of the page. + + The bindings are used as metadata of the page. The BINDING binds the VALUE + within the scope of subsequent BINDINGs and the BODY. The KEY is the key + under which the VALUE will be stored. + + The body is a sequence of S-XML elements. All body expressions will be + grouped inside a list and stored under the key :CONTENT. + + The macro expands into an association list. The entries are the metadata + '(KEY . VALUE) pairs, and a special pair '(:CONTENT . CONTENT), where + CONTENT is a list of all content expressions. + + Example: + + (static-page ((title :title \"title\") + (author :author \"yo mama\")) + `(:p \"hello world\") + `(:p \"by \" ,author))" + ;; TODO: make it possible to not bind a BINDING, i.e. write (:key val) + ;; instead of (var :key val) if the binding is not needed. + + (let ((vars (mapcar #'car bindings)) + (keys (mapcar #'cadr bindings)) + (vals (mapcar #'caddr bindings))) + `(let* ,(mapcar (lambda (var val) `(,var ,val)) vars vals) + (list + ,@(mapcar (lambda (key var) `(cons ,key ,var)) keys vars) + (cons :content (list ,@(mapcar (lambda (c) `(,@c)) content))))))) diff --git a/src/hssg/artifacts/verbatim.lisp b/src/hssg/artifacts/verbatim.lisp new file mode 100644 index 0000000..60ab508 --- /dev/null +++ b/src/hssg/artifacts/verbatim.lisp @@ -0,0 +1,42 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; verbatim.lisp Verbatim artifact implementation +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.artifact.verbatim) + +;;; Maybe this could be a public config parameter? I could be useful for other +;;; file copy operations as well. +(defparameter *buffer-size* 512 + "Buffer size to use when copying a file. The file will be copied in chunks of + this size.") + +(defmethod hssg.artifact:write-artifact ((artifact hssg.artifact:verbatim-artifact)) + (with-slots ((input hssg.artifact::input) + (output hssg.artifact::output)) + artifact + (ensure-directories-exist output) + (fad:copy-file input output :overwrite t) + nil)) + +(defun make-verbatim-artifact (file-path input-dir output-dir) + "Constructor for a verbatim file artifact which copies to contents of + FILE-PATH, relative to INPUT-DIR to the same path relative to OUTPUT-DIR." + (declare (type (or string pathname) file-path input-dir output-dir)) + (let* ((input (merge-pathnames file-path (fad:pathname-as-directory input-dir))) + (output (merge-pathnames file-path (fad:pathname-as-directory output-dir)))) + (make-instance 'hssg.artifact:verbatim-artifact :input input :output output))) diff --git a/src/hssg/artifacts/xml.lisp b/src/hssg/artifacts/xml.lisp new file mode 100644 index 0000000..b946521 --- /dev/null +++ b/src/hssg/artifacts/xml.lisp @@ -0,0 +1,38 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; xml XML artifact implementation +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.artifact.xml) + +(defmethod hssg.artifact:write-artifact ((artifact hssg.artifact:xml-artifact)) + (with-slots ((data hssg.artifact::data) + (output hssg.artifact::output)) + artifact + (with-open-file (*STANDARD-OUTPUT* output :direction :output :if-exists :supersede :if-does-not-exist :create) + (plump:serialize (sexp->xml-tree data))))) + +(defun sexp->xml-tree (sexp &key (version "1.0") (encoding "UTF-8")) + "Converts one s-expression tree to an XML document tree. All the neccessary + attributes of the XML header node will set from the (optional) keyword + arguments ." + (let* ((result (plump-dom:make-root (plump:make-child-array 2))) + (header (plump:make-xml-header result))) + (plump:set-attribute header "version" version) + (plump:set-attribute header "encoding" encoding) + (hssg.artifact.html::sexp->plump-tree sexp result) ; Called for side effect only + result)) diff --git a/src/hssg/config.lisp b/src/hssg/config.lisp new file mode 100644 index 0000000..9659630 --- /dev/null +++ b/src/hssg/config.lisp @@ -0,0 +1,33 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; config.lisp Mutable global variables for configuring the entire generator +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg) + +(defparameter *site-url* "localhost" + "URL of the entire website (without trailing slash); this parameter needs to + be rebound when writing the website. Its value will be used whenever we need + the full URL to the website. + + We should use relative URL paths wherever possible, but sometime it is + necessary to expose a fully qualified URL, e.g. when providing a share link + or generating an RSS feed.") + +(defparameter *site-language* "en" + "Language code of the website, can for example be inserted into the 'lang' + attribute of the '' tag.") diff --git a/src/hssg/package.lisp b/src/hssg/package.lisp new file mode 100644 index 0000000..479ec66 --- /dev/null +++ b/src/hssg/package.lisp @@ -0,0 +1,71 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; package.lisp Package definitions +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . + +(defpackage #:hssg.reader + (:documentation "Interface to the various content readers.") + (:use :cl) + (:export get-reader register-reader with-readers)) + +(defpackage #:hssg.artifact + (:documentation "Helper package, defines the artifact protocl.") + (:use #:cl) + (:export write-artifact artifacts + verbatim-artifact compound-artifact directory-artifact html-artifact xml-artifact)) + +(defpackage #:hssg.artifact.html + (:documentation "Implementaion of HTML artifacts.") + (:use #:cl) + (:export static-page read-html-lisp)) + +(defpackage #:hssg.artifact.xml + (:documentation "Implementation of XML artifacts.") + (:use #:cl) + (:export)) + +(defpackage #:hssg.artifact.verbatim + (:documentation "Helper package, contains verbatim artifacts") + (:use #:cl) + (:export make-verbatim-artifact)) + +(defpackage #:hssg.artifact.directory + (:documentation "Helper package, contains verbatim artifacts") + (:use #:cl) + (:export make-directory-artifact)) + +(defpackage #:hssg.template + (:documentation "HTML template macros; templates transform SHTML aslists.") + (:use #:cl) + (:export deftemplate template let-metadata apply-template identity-template chain-templates)) + +(defpackage #:hssg + (:documentation "The hackable static site generator") + (:use #:cl) + (:import-from #:hssg.artifact write-artifact compound-artifact artifacts html-artifact xml-artifact ) + (:import-from #:hssg.artifact.verbatim make-verbatim-artifact) + (:import-from #:hssg.artifact.directory make-directory-artifact) + (:import-from #:hssg.artifact.html static-page read-html-lisp) + (:import-from #:hssg.template deftemplate template let-metadata apply-template identity-template chain-templates) + (:export + *site-url* *site-language* + write-artifact compound-artifact artifacts + html-artifact xml-artifact static-page read-html-lisp + make-verbatim-artifact make-directory-artifact + deftemplate template let-metadata apply-template identity-template chain-templates)) +(unintern 'hssg:xml-artifact) diff --git a/src/hssg/reader.lisp b/src/hssg/reader.lisp new file mode 100644 index 0000000..cbac802 --- /dev/null +++ b/src/hssg/reader.lisp @@ -0,0 +1,50 @@ +;;;; SPDX-License-Identifier: AGPL-3.0-or-later + +;;;; reader.lisp Interface to the various content readers +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.reader) + +(defvar *file-readers* '() + "Association list of file readers; each entry maps a type string to a reader + function. A reader function is a function which accepts a PATHNAME object and + returns an association list of metadata.") + +(defun get-reader (type) + "For a given file TYPE return the currently registered file reader function." + (declare (type string type)) + (let ((entry (assoc type *file-readers* :test #'string=))) + (if entry + (cdr entry) + (error "No file reader registered for type ~A." type)))) + +(defun register-reader (type reader) + "Permanently registeres a new READER function for the given TYPE." + (declare (type string type)) + (push (cons type reader) *file-readers*)) + +(defmacro with-readers ((&rest reader-spec) &body body) + "Evaluate the BODY expressions with temporarily registered readers. Each + reader specification is a list of to elements of the form (TYPE READER). The + entire expression evaluates to the evaluation of the last BODY form." + (flet ((reader-spec->list (spec) + (destructuring-bind (type reader) spec + `(cons ,type ,reader)))) + `(let ((*file-readers* (concatenate 'list + (list ,@(mapcar #'reader-spec->list reader-spec)) + *file-readers*))) + ,@body))) diff --git a/src/hssg/readers/lisp.lisp b/src/hssg/readers/lisp.lisp new file mode 100644 index 0000000..ea3f91d --- /dev/null +++ b/src/hssg/readers/lisp.lisp @@ -0,0 +1,50 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; lisp.lisp Lisp content reader implementation +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . + +(defpackage #:hssg.reader.lisp + (:documentation "Lisp reader implementation") + (:use :cl)) +(in-package #:hssg.reader.lisp) + +;;; Idea: if creating a new package is too much effort we could create a +;;; reserved package that can be cleaned and recycled between reads (make sure +;;; to synchronize on the package if multiple reader can read concurrently). +(defmacro with-package ((&rest using) &body body) + "Evaluate the BODY expressions within a separate package." + ;; FIXME: the new package is not actually used. + (let ((name (gensym)) + (package (gensym))) + `(let* ((,name (gensym)) + (,package (make-package ,name :use (list ,@using)))) + (unwind-protect (let ((*PACKAGE* ,package)) + ,@body) + (delete-package ,package))))) + +(defun read-lisp-metadata (pathname) + "Read the contents of an HTML document from a Lisp file." + (declare (type (or string pathname) pathname)) + (with-open-file (*STANDARD-INPUT* pathname) + (with-package (:cl) + (loop for exp = (read *STANDARD-INPUT* nil) + with value + if (null exp) return value + else do (setf value (eval exp)))))) + +(hssg.reader:register-reader "lisp" #'read-lisp-metadata) diff --git a/src/hssg/template.lisp b/src/hssg/template.lisp new file mode 100644 index 0000000..b9ca10b --- /dev/null +++ b/src/hssg/template.lisp @@ -0,0 +1,118 @@ +;;;; SPDX-License-Identifier AGPL-3.0-or-later + +;;;; template.lisp Definition of HTML template macros +;;;; Copyright (C) 2022 Alejandro "HiPhish" Sanchez +;;;; +;;;; This file is part of CL-HSSG. +;;;; +;;;; CL-HSSG is free software: you can redistribute it and/or modify it under +;;;; the terms of the GNU Affero General Public License as published by the +;;;; Free Software Foundation, either version 3 of the License, or (at your +;;;; option) any later version. +;;;; +;;;; CL-HSSG is distributed in the hope that it will be useful, but WITHOUT ANY +;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +;;;; FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +;;;; more details. +;;;; +;;;; You should have received a copy of the GNU Affero General Public License +;;;; along with CL-HSSG If not, see . +(in-package #:hssg.template) + +(deftype template () + "An HTML template, either a function object which maps a data + association-list to another association list, or a symbol whose function is a + template function." + '(or symbol function)) + +(defmacro template ((&rest bindings) &rest entries) + "Define an anonymous template. A template is a function which maps an a-list + onto another a-list. + + For each binding in BINDINGS bind the corresponding value from the input + a-list. These bindings can then be used inside the individual ENTRIES." + (let ((data (gensym "METADATA"))) + `(lambda (,data) + (let (,@(mapcar (lambda (binding) + `(,binding (cdr (assoc (intern ,(symbol-name binding) "KEYWORD") + ,data :test #'eq)))) + bindings)) + (append + (list ,@(mapcar + #'(lambda (entry) + (destructuring-bind (field value) entry + `(cons (intern ,(symbol-name field) "KEYWORD") + ,value))) + entries)) + ,data))))) + +(defmacro deftemplate (name (&rest bindings) &rest entries) + "Define a named template, which is just an ordinary function. Refer to + TEMPLATE for details." + (let* ((docstring (and (typep (car entries) 'string) + (car entries))) + (entries (if docstring (cdr entries) entries))) + `(progn + (setf (fdefinition ',name) + (template ,bindings ,@entries)) + (setf (documentation ',name 'function) ,docstring) + ',name))) + +(defmacro let-metadata (data-expr (&rest bindings) &body body) + "Binds the metadata from the DATA-EXPRession to the BINDINGS for the + evaluation of the BODY expressions. Each bindings is a tuple of the form + (KEYWORD SYMBOL &OPTIONAL DEFAULT). The DEFAULT expression will only be + evaluated if the KEYWORD is not present in the DATA. + + The result is the value of the last BODY form. + + Example: + (let ((data '((:a . \"a\") (:b . \"b\")))) + (let-metadata data ((a :a) + (b :b) + (c :c \"c\") ; Explicit default + (d :d)) ; Implicit default + (list a b c d))) + -- + (\"a\" \"b\" \"c\" NIL)" + (let ((data (gensym "DATA"))) + (flet ((transform-binding (binding) + (destructuring-bind (identifier kword &optional (default nil)) binding + (let ((entry (gensym))) + `(,identifier (let ((,entry (assoc ,kword ,data))) + (if ,entry (cdr ,entry) ,default))))))) + `(let ((,data ,data-expr)) + (let (,@(mapcar #'transform-binding bindings)) + ,@body))))) + + +;; Alternative: use symbol as template name, then use (SYMBOL-FUNCTION +;; TEMPLATE) to get the function. +(defmacro apply-template (template data) + "Apply the data transformation of TEMPLATE to the DATA." + (let ((template-var (gensym)) + (data-var (gensym))) + `(let ((,template-var ,template) + (,data-var ,data)) + (etypecase ,template-var + (symbol (funcall (symbol-function ,template-var) ,data-var)) + (function (funcall ,template-var ,data-var)))))) + +(deftemplate identity-template () + "The neutral template, returns its input data unchanged. (I don't know why + anyone would need this, but it is necessary in order to make the template + type a monoid.)" + ;; Intentionally left blank + ) + +(defun chain-templates (&rest templates) + "Returns a template which will apply all TEMPLATES in order from left to + right one after another to the data. (This is the operation of the template + monoid)" + (lambda (data) + (declare (type list data)) + (let ((result data)) + (declare (type list result)) + (dolist (tmp templates result) + (declare (type template tmp)) + (setf result (apply-template tmp result))))))