Skip to content

Commit

Permalink
Release 0.17.6 (#322)
Browse files Browse the repository at this point in the history
Release 0.17.6
  • Loading branch information
cjbrooks12 committed Oct 14, 2019
2 parents 8f93da4 + b2d6a79 commit 471f1da
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.eden.orchid.api.OrchidContext;
import com.eden.orchid.api.options.OrchidFlags;
import com.eden.orchid.api.theme.pages.OrchidReference;
import kotlin.collections.ArraysKt;
import kotlin.collections.CollectionsKt;
import kotlin.text.StringsKt;
import org.apache.commons.io.IOUtils;

import java.io.File;
Expand All @@ -19,7 +22,7 @@
* through the `page` variable. When used with renderRaw(), the raw contents (after having the embedded data removed)
* will be written directly instead.
*/
public final class FileResource extends FreeableResource {
public final class FileResource extends FreeableResource {

private final File file;

Expand All @@ -38,13 +41,12 @@ public FileResource(File file, OrchidReference reference) {

@Override
protected void loadContent() {
if(rawContent == null) {
if (rawContent == null) {
try {
if (file != null) {
rawContent = IOUtils.toString(new FileInputStream(file), Charset.forName("UTF-8"));
}
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
Expand All @@ -58,22 +60,35 @@ private static String pathFromFile(OrchidContext context, File file) {

private static String pathFromFile(OrchidContext context, File file, String basePath) {
String filePath = file.getPath();

// normalise Windows-style backslashes to common forward slashes
basePath = basePath.replaceAll("\\\\", "/");
filePath = filePath.replaceAll("\\\\", "/");

if(filePath.startsWith(basePath)) {
// Remove the common base path from the actual file path
if (filePath.startsWith(basePath)) {
filePath = filePath.replaceAll(basePath, "");
}

if (filePath.startsWith("/")) {
filePath = StringsKt.removePrefix(filePath, "/");
}

// if the path is not a child of the base path (i.e. still has relative path segments), strip those away. The
// resolved "path" of this resource will be the portion after those relative segments.

filePath = CollectionsKt.joinToString(
ArraysKt.filter(filePath.split("/"), (it) -> !(it.equals("..") || it.equals("."))),
"/", "", "", -1, "", null);

return filePath;
}

@Override
public InputStream getContentStream() {
try {
return new FileInputStream(file);
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
return null;
}
Expand All @@ -91,14 +106,14 @@ public boolean canDelete() {

@Override
public void update(InputStream newContent) throws IOException {
if(file != null && newContent != null) {
if (file != null && newContent != null) {
Files.write(file.toPath(), IOUtils.toByteArray(newContent));
}
}

@Override
public void delete() throws IOException {
if(file != null) {
if (file != null) {
file.delete();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.eden.orchid.api.OrchidContext;
import com.eden.orchid.api.resources.resource.ExternalResource;
import com.eden.orchid.api.resources.resource.OrchidResource;
import com.eden.orchid.api.theme.pages.OrchidPage;
import com.eden.orchid.utilities.OrchidUtils;
import org.apache.commons.io.FilenameUtils;

Expand Down Expand Up @@ -91,7 +90,7 @@ public JsPage addJs(JsPage jsAsset) {
}

private JsPage addJs(JsPage jsAsset, boolean renderImmediately) {
return addAssetInternal(jsAsset, "js", renderImmediately);
return addAssetInternal(jsAsset, renderImmediately);
}

@Override
Expand All @@ -100,7 +99,7 @@ public CssPage addCss(CssPage cssAsset) {
}

private CssPage addCss(CssPage cssAsset, boolean renderImmediately) {
return addAssetInternal(cssAsset, "css", renderImmediately);
return addAssetInternal(cssAsset, renderImmediately);
}

@Override
Expand All @@ -109,52 +108,38 @@ public AssetPage addAsset(AssetPage asset) {
}

private AssetPage addAsset(AssetPage asset, boolean renderImmediately) {
return addAssetInternal(asset, null, renderImmediately);
return addAssetInternal(asset, renderImmediately);
}

// Add assets by string
//----------------------------------------------------------------------------------------------------------------------

@Override
public JsPage addJs(String jsAsset) {
return addAssetInternal(jsAsset, "JS", true, JsPage::new, this::addJs);
return addAssetInternal(jsAsset, true, JsPage::new, this::addJs);
}

@Override
public CssPage addCss(String cssAsset) {
return addAssetInternal(cssAsset, "CSS", true, CssPage::new, this::addCss);
return addAssetInternal(cssAsset, true, CssPage::new, this::addCss);
}

@Override
public AssetPage addAsset(String asset) {
return addAssetInternal(asset, "", true, AssetPage::new, this::addAsset);
return addAssetInternal(asset, true, AssetPage::new, this::addAsset);
}

// internals
//----------------------------------------------------------------------------------------------------------------------

private boolean validAsset(OrchidPage asset, String targetExtension) {
return asset.getReference().getOutputExtension().equalsIgnoreCase(targetExtension);
private <T extends AssetPage> T addAssetInternal(T asset, boolean renderImmediately) {
asset.getReference().setUsePrettyUrl(false);
AssetPage actualAsset = context.getAssetManager().addAsset(asset, renderImmediately);
assets.add(actualAsset);
return asset;
}

private <T extends AssetPage> T addAssetInternal(T asset, String expectedOutputExtension, boolean renderImmediately) {
if(expectedOutputExtension == null || validAsset(asset, expectedOutputExtension)) {
asset.getReference().setUsePrettyUrl(false);
AssetPage actualAsset = context.getAssetManager().addAsset(asset, renderImmediately);
assets.add(actualAsset);
return asset;
}
else {
Clog.w("#{$1} is not a valid #{$2} asset, perhaps you are missing a #{$3}->#{$4} Compiler extension?",
asset.getReference().getOriginalFullFileName(),
expectedOutputExtension.toUpperCase(),
asset.getReference().getOutputExtension(),
expectedOutputExtension);
return null;
}
}

private <T extends AssetPage> T addAssetInternal(String asset, String assetTypeName, boolean renderImmediately, CreateAssetInterface<T> creator, BiConsumer<T, Boolean> adder) {
private <T extends AssetPage> T addAssetInternal(String asset, boolean renderImmediately, CreateAssetInterface<T> creator, BiConsumer<T, Boolean> adder) {
OrchidResource resource = context.getResourceEntry(asset);
if(resource != null) {
boolean setPrefix = !EdenUtils.isEmpty(prefix);
Expand All @@ -174,7 +159,7 @@ private <T extends AssetPage> T addAssetInternal(String asset, String assetTypeN
return page;
}
else {
Clog.w("Could not find {} asset: {}", assetTypeName, asset);
Clog.w("Could not find asset: {}", asset);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.eden.orchid.api.options.archetypes.AssetMetadataArchetype;
import com.eden.orchid.api.resources.resource.OrchidResource;
import com.eden.orchid.api.theme.pages.OrchidPage;
import kotlin.collections.CollectionsKt;

import java.util.HashMap;
import java.util.Map;

@Archetype(value = AssetMetadataArchetype.class, key = "assetmeta")
@Description(value = "A static asset, like Javascript, CSS, or an image.", name = "Asset")
Expand All @@ -19,6 +23,10 @@ public class AssetPage extends OrchidPage {
@Description("The asset alt text.")
private String alt;

@Option
@Description("Arbitrary attributes to apply to this element when rendered to page")
private Map<String, String> attrs = new HashMap<>();

public AssetPage(Object source, String sourceKey, OrchidResource resource, String key, String title) {
super(resource, key, title);
this.source = source;
Expand All @@ -30,6 +38,10 @@ public String renderAssetToPage() {
return "";
}

protected String renderAttrs() {
return CollectionsKt.joinToString(attrs.entrySet(), " ", "", "", -1, "...", entry -> "" + entry.getKey() + "=\"" + entry.getValue() + "\"");
}

@Override
public String getLink() {
if(!rendered) {
Expand Down Expand Up @@ -76,4 +88,12 @@ public void setRendered(boolean rendered) {
public void setAlt(String alt) {
this.alt = alt;
}

public Map<String, String> getAttrs() {
return attrs;
}

public void setAttrs(Map<String, String> attrs) {
this.attrs = attrs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public CssPage(Object source, String sourceKey, OrchidResource resource, String

public String renderAssetToPage() {
if (resource instanceof InlineResource) {
return "<style>\n" + resource.compileContent(this) + "\n</style>";
return "<style " + renderAttrs() + ">\n" + resource.compileContent(this) + "\n</style>";
}
else {
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.getLink() + "\"/>";
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + this.getLink() + "\" " + renderAttrs() + "/>";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public JsPage(Object source, String sourceKey, OrchidResource resource, String k

public String renderAssetToPage() {
if (resource instanceof InlineResource) {
return "<script>\n" + resource.compileContent(this) + "\n</script>";
return "<script " + renderAttrs() + ">\n" + resource.compileContent(this) + "\n</script>";
} else {
String tagString = "<script";
String tagString = "<script " + renderAttrs();
if (async) {
tagString += " async";
}
Expand Down
7 changes: 7 additions & 0 deletions OrchidCore/src/orchid/resources/changelog/0.17/0.17.6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- Removes restrictions on filenames that can be used for assets. CSS assets no longer requires a `.css` extension, and
likewise JavaScript assets no longer require a `.js` extension. It's up to you to make sure an asset is valid or not
before it gets added to the page.
- When creating a Reference to a file that is not a child of the base dir, remove relative path segments so that it will
be copied properly to the rendered site, and not outside of the build dir.
- Adds `kotlinPlayground` component to the `OrchidSyntaxHighlighter` artifact, for converting Kotlin code snippets into
runnable playgrounds.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ tags:
## About

Add syntax highlighting to code snippets in your Orchid site. Supports pre-rendered highlighting with
[Pygments](http://pygments.org/), and browser-based highlighting with [PrismJS](https://prismjs.com/)
[Pygments](http://pygments.org/), browser-based highlighting with [PrismJS](https://prismjs.com/), and runnable Kotlin
code snippets with [Kotlin Playground](https://github.com/JetBrains/kotlin-playground).

## Demo

Expand Down Expand Up @@ -96,3 +97,28 @@ allPages:
- 'kotlin'
- 'yaml'
```

### Kotlin Playground

The Kotlin Playground allows you to convert Kotlin code snippets into playgrounds that are runnable right in your
browser. The `kotlinPlayground` component adds the script from their CDN, which will select all elements on the page
with your runnable Kotlin code and convert them into embedded runnable playgrounds. By default, all Markdown code
snippets with a language of `run-kotlin` are converted.

```yaml
allPages:
components:
- type: 'pageContent'
- type: 'kotlinPlayground'
selector: "pre code[class='language-run-kotlin']"
```

You can configure each individual playground using the attributes described in the [Kotlin playground docs](https://github.com/JetBrains/kotlin-playground#customizing-editors).
These can be added from Markdown snippets with the following syntax:

```run-kotlin
fun main() {
println("Running from Kotlin Playground!")
}
```
{theme='idea' lines='true'}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.eden.orchid.languages.highlighter
import com.eden.orchid.api.compilers.TemplateTag
import com.eden.orchid.api.registration.OrchidModule
import com.eden.orchid.api.theme.components.OrchidComponent
import com.eden.orchid.languages.highlighter.components.KotlinPlaygroundComponent
import com.eden.orchid.languages.highlighter.components.PrismComponent
import com.eden.orchid.languages.highlighter.tags.HighlightTag
import com.eden.orchid.utilities.addToSet
Expand All @@ -13,6 +14,9 @@ class SyntaxHighlighterModule : OrchidModule() {
withResources(750)

addToSet<TemplateTag, HighlightTag>()
addToSet<OrchidComponent, PrismComponent>()
addToSet<OrchidComponent>(
PrismComponent::class,
KotlinPlaygroundComponent::class
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.eden.orchid.languages.highlighter.components

import com.eden.orchid.api.OrchidContext
import com.eden.orchid.api.options.annotations.Description
import com.eden.orchid.api.options.annotations.Option
import com.eden.orchid.api.options.annotations.StringDefault
import com.eden.orchid.api.theme.components.OrchidComponent
import javax.inject.Inject

@Description(
"Add the Kotlin Playground to your pages, to convert Kotlin code snippets into interactive, embedded " +
"development playgrounds.",
name = "Kotlin Playground"
)
class KotlinPlaygroundComponent
@Inject
constructor(
context: OrchidContext
) : OrchidComponent(context, "kotlinPlayground", 100) {

@Option
@Description("The base URL to load Kotlin Playground JS files from.")
@StringDefault("https://unpkg.com/kotlin-playground@1")
lateinit var kotlinPlaygroundSource: String

@Option
@Description("Select which elements on the page are converted. Defaults to markdown code blocks with the " +
"`run-kotlin` language."
)
@StringDefault("pre code[class='language-run-kotlin']")
lateinit var selector: String

@Option
@Description("The URL to a self-hosted server instance for running code snippets.")
lateinit var server: String

override fun loadAssets() {
addJs(kotlinPlaygroundSource).apply {
attrs["data-selector"] = selector

if (server.isNotBlank()) {
attrs["data-server"] = server
}
}
}

override fun isHidden(): Boolean {
return true
}
}
Loading

0 comments on commit 471f1da

Please sign in to comment.