Fixes #4798
authorAlex <onedrawingperday@gmail.com>
Wed, 30 May 2018 20:42:32 +0000 (23:42 +0300)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 31 May 2018 16:05:38 +0000 (18:05 +0200)
config/privacy/privacyConfig.go
config/privacy/privacyConfig_test.go
config/services/servicesConfig.go
config/services/servicesConfig_test.go
tpl/tplimpl/embedded/templates.autogen.go
tpl/tplimpl/embedded/templates/shortcodes/twitter.html
tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html [new file with mode: 0644]

index d4820396dc48fde9f8c740bd2c9b3e73a16e4cbc..a058c3593a4a7dc7e30b1ccdb7cfe7910c470e1b 100644 (file)
@@ -77,6 +77,9 @@ type Twitter struct {
        // When set to true, the Tweet and its embedded page on your site are not used
        // for purposes that include personalized suggestions and personalized ads.
        EnableDNT bool
+
+       // If simple mode is enabled, a static and no-JS version of the Tweet will be built.
+       Simple bool
 }
 
 // Vimeo holds the privacy configuration settingsrelated to the Vimeo shortcode.
index 5593872dede0f901b961703b9408aa8cd2903405..8d9c9f14f7d10b884a7035e8d333d0ceb37fc9d9 100644 (file)
@@ -44,6 +44,7 @@ disable = true
 [privacy.twitter]
 disable = true
 enableDNT = true
+simple = true
 [privacy.vimeo]
 disable = true
 simple = true
@@ -69,6 +70,7 @@ simple = true
        assert.True(pc.SpeakerDeck.Disable)
        assert.True(pc.Twitter.Disable)
        assert.True(pc.Twitter.EnableDNT)
+       assert.True(pc.Twitter.Simple)
        assert.True(pc.Vimeo.Disable)
        assert.True(pc.Vimeo.Simple)
        assert.True(pc.YouTube.PrivacyEnhanced)
index d78b809483456918798bb37edbdb00dccc53f359..bc321141a2760504ca32da9c40d99577b20aa189 100644 (file)
@@ -30,6 +30,7 @@ type Config struct {
        Disqus          Disqus
        GoogleAnalytics GoogleAnalytics
        Instagram       Instagram
+       Twitter         Twitter
 }
 
 // Disqus holds the functional configuration settings related to the Disqus template.
@@ -52,6 +53,14 @@ type Instagram struct {
        DisableInlineCSS bool
 }
 
+// Twitter holds the functional configuration settings related to the Twitter shortcodes.
+type Twitter struct {
+       // The Simple variant of Twitter is decorated with a basic set of inline styles.
+       // This means that if you want to provide your own CSS, you want
+       // to disable the inline CSS provided by Hugo.
+       DisableInlineCSS bool
+}
+
 func DecodeConfig(cfg config.Provider) (c Config, err error) {
        m := cfg.GetStringMap(servicesConfigKey)
 
index 69dec03507a06df11e0ccf13dee1c357f9d3f339..367b401531e1f2a8ce19331b8df99eb2a497364a 100644 (file)
@@ -35,6 +35,8 @@ shortname = "DS"
 id = "ga_id"
 [services.instagram]
 disableInlineCSS = true
+[services.twitter]
+disableInlineCSS = true
 `
        cfg, err := config.FromConfigString(tomlConfig, "toml")
        assert.NoError(err)
index e9fcc3325fad1bc5eccbc79c5af8fe04bfda174f..44a3306225f42bb9334e9443408e7730ba71d78e 100644 (file)
@@ -390,10 +390,47 @@ if (!doNotTrack) {
 {{- end -}}`},
        {`shortcodes/twitter.html`, `{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
 {{- if not $pc.Disable -}}
+{{- if $pc.Simple -}}
+{{ template "_internal/shortcodes/twitter_simple.html" . }}
+{{- else -}}
 {{- $url := printf "https://api.twitter.com/1/statuses/oembed.json?id=%s&dnt=%t" (index .Params 0) $pc.EnableDNT -}}
 {{- $json := getJSON $url -}}
 {{ $json.html | safeHTML }}
+{{- end -}}
 {{- end -}}`},
+       {`shortcodes/twitter_simple.html`, `{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
+{{- $sc := .Page.Site.Config.Services.Twitter -}}
+{{- if not $pc.Disable -}}
+{{- $id := .Get 0 -}}
+{{- $json := getJSON "https://api.twitter.com/1/statuses/oembed.json?id=" $id "&omit_script=true" -}}
+{{- if not $sc.DisableInlineCSS -}}
+{{ template "__h_simple_twitter_css" $ }}
+{{- end -}}
+{{ $json.html | safeHTML }}
+{{- end -}}
+
+{{ define "__h_simple_twitter_css" }}
+{{ if not (.Page.Scratch.Get "__h_simple_twitter_css") }}
+{{/* Only include once */}}
+{{  .Page.Scratch.Set "__h_simple_twitter_css" true }}
+<style type="text/css">
+  .twitter-tweet {
+  font: 14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
+  border-left: 4px solid #2b7bb9;
+  padding-left: 1.5em;
+  color: #555;
+}
+  .twitter-tweet a {
+  color: #2b7bb9;
+  text-decoration: none;
+}
+  blockquote.twitter-tweet a:hover,
+  blockquote.twitter-tweet a:focus {
+  text-decoration: underline;
+}
+</style>
+{{ end }}
+{{ end }}`},
        {`shortcodes/vimeo.html`, `{{- $pc := .Page.Site.Config.Privacy.Vimeo -}}
 {{- if not $pc.Disable -}}
 {{- if $pc.Simple -}}
index 472bd8572579aa81b99f6e28bece1dca7fbd01fb..ea7f10c383d7b493d625931397b3874afbde7823 100755 (executable)
@@ -1,6 +1,10 @@
 {{- $pc := .Page.Site.Config.Privacy.Twitter -}}
 {{- if not $pc.Disable -}}
+{{- if $pc.Simple -}}
+{{ template "_internal/shortcodes/twitter_simple.html" . }}
+{{- else -}}
 {{- $url := printf "https://api.twitter.com/1/statuses/oembed.json?id=%s&dnt=%t" (index .Params 0) $pc.EnableDNT -}}
 {{- $json := getJSON $url -}}
 {{ $json.html | safeHTML }}
+{{- end -}}
 {{- end -}}
\ No newline at end of file
diff --git a/tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html b/tpl/tplimpl/embedded/templates/shortcodes/twitter_simple.html
new file mode 100644 (file)
index 0000000..45d594f
--- /dev/null
@@ -0,0 +1,33 @@
+{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
+{{- $sc := .Page.Site.Config.Services.Twitter -}}
+{{- if not $pc.Disable -}}
+{{- $id := .Get 0 -}}
+{{- $json := getJSON "https://api.twitter.com/1/statuses/oembed.json?id=" $id "&omit_script=true" -}}
+{{- if not $sc.DisableInlineCSS -}}
+{{ template "__h_simple_twitter_css" $ }}
+{{- end -}}
+{{ $json.html | safeHTML }}
+{{- end -}}
+
+{{ define "__h_simple_twitter_css" }}
+{{ if not (.Page.Scratch.Get "__h_simple_twitter_css") }}
+{{/* Only include once */}}
+{{  .Page.Scratch.Set "__h_simple_twitter_css" true }}
+<style type="text/css">
+  .twitter-tweet {
+  font: 14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
+  border-left: 4px solid #2b7bb9;
+  padding-left: 1.5em;
+  color: #555;
+}
+  .twitter-tweet a {
+  color: #2b7bb9;
+  text-decoration: none;
+}
+  blockquote.twitter-tweet a:hover,
+  blockquote.twitter-tweet a:focus {
+  text-decoration: underline;
+}
+</style>
+{{ end }}
+{{ end }}
\ No newline at end of file