Is there a way to integrate Prettify with Blogger/BlogSpot?

I am currently utilizing blogger.com as a platform to showcase programming texts and I am interested in incorporating Prettify (similar to Stack Overflow) to enhance the appearance of my code samples.

What is the best method for installing the Prettify scripts onto my blog's domain? Should I consider linking to a shared copy instead, if feasible? Additionally, I have access to webspace on a separate domain - could this be beneficial?

Answer №1

Creating a new entry on Blogger allows you to utilize HTML in your post and edit your blog entries as needed.

To get started, visit , sign in, and then go to PostingEdit PostsEdit. Insert the following code snippet at the top:

<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/lang-css.min.js"></script>
<script type="text/javascript">
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(function() {
    prettyPrint();
});
</script>
<style type="text/css">
/* Pretty printing styles. Used with prettify.js. */
...

Note that directly using prettyPrint as an event handler may cause confusion, so it is recommended to follow the provided instructions for proper implementation.

If linking to the stylesheet is not allowed, embed the contents of prettify.css within your Blogger post. Include a <code></code> or <pre></pre> tag with the class name ""prettyprint"" to display formatted code snippets.

You can enhance all pages by default by adding the specified code to your HTML layout. In newer Blogger versions, CSS files can be easily linked in the <head> section for seamless integration.

...

Answer №2

This method worked like a charm for me right away.

  • Navigate to Blogger > Layout > Edit HTML
  • Copy the snippet below and paste it right after <head> in the "Edit template" section:

snippet:

<link href='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.css' rel='stylesheet' type='text/css'/>
<script src='http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js' type='text/javascript'></script>
  • Replace <body> with
    <body onload='prettyPrint()'>
    after </head>
  • Click on "SAVE TEMPLATE"
  • Go to Blogger > Posting > New Post
  • Ensure you are editing the HTML by selecting "Edit HTML." In the blank field, try:

<pre class="prettyprint">int foo=0;
NSLog(@"%i", foo);
</pre>

  • If you click "Preview" now, you'll only see the code in black. Don't worry about it yet.
  • Click "PUBLISH POST" followed by "VIEW BLOG". Your code should now look prettier.

Answer №3

Google Code Prettify now features a convenient auto-loader script that allows you to easily load the necessary JavaScript and CSS using just one URL.

To implement this feature on your Blogger template, simply add the following script to the <head> section:

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

For more detailed instructions, check out Getting Started.

Answer №4

Adding the Google Code prettifier to your Blogger account is a simple process.

All you need to do is insert the JavaScript library below into your Blogger account just before the closing tag.

<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

It should look something like this...

Congratulations! You have now successfully integrated the Google Code prettifier into your Blogger account.

If you wish to include code (HTML, CSS, PHP, etc.) in your Blogger post, simply place it between .... tags.

 <pre class="prettyprint">...</pre>

or

<code class="prettyprint">...</code>

View a demo of the Google Prettify on Blogger here

For more guidance on adding the Google prettifier to your Blogger account, please refer to this documentation available at the following link.

Syntax Highlighter For Blogger Using Google Prettify

Answer №5

Check out the SyntaxHightlighter tool.

The website provides step-by-step instructions for using it on blogger.com and also offers a hosted version of the necessary scripts, eliminating the need to host files on your own server.

Answer №6

If you're looking for an alternative solution, consider trying out the syntaxhighlighter 2.0 JavaScript library. I recently implemented it on my website and so far, it's been working really well.

For more information, check out this helpful blog post:

Syntax Highlighting with Blogger Engine

Answer №7

Although it may not directly address your inquiry, have you considered exploring GitHub? By creating a free account, you can enjoy syntax-colored "gists" that can be shared and hosted on your own webpage.

However, it is important to note that the content will be hosted on GitHub's platform, so if their site experiences downtime, it may affect your accessibility as well.

Answer №8

cdnjs offers the library "SyntaxHighlighter".

Visit BloggerTemplateEdit template. Insert the following code right before closing the body tag and save the template.

An example is provided for Python in the code snippet below.

You can also include script files for other languages from cdnjs.

Code for Syntax Highlighting:

<pre class="brush: py">
    print("Hello, World!")
</pre>

To get scripts for other languages, visit:

https://i.sstatic.net/4HAL6.png

<!-- Syntax highlighter-->
<link href='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/styles/shThemeDefault.css' rel='stylesheet'/>
<script src='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shCore.min.js'/>
<script src='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shAutoloader.min.js'/>

<!-- For Python -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/SyntaxHighlighter/3.0.83/scripts/shBrushPython.min.js'/>

<!-- Include other languages like JavaScript and PHP -->
<script language='javascript'>
    SyntaxHighlighter.config.bloggerMode = true;
    SyntaxHighlighter.all();
</script>

Answer №9

Check out this solution that I found helpful. Simply insert the following code snippet within the <head>...</head> section of your dynamic Blogger HTML:

<script>
    $(window.blogger.ui()).on('viewitem', function (event, post, element) {
        prettyPrint();
    });
</script>

Answer №10

To implement syntax highlighting in your Blogger theme, go to the theme section and select edit HTML. Add a reference to the Google Prettify CDN within the head tag of the HTML.

Next, include a theme for code snippets below this script. In this example, I have included the Desert theme.
<!--Desert theme-->
<style type='text/css'>pre .atn,pre .kwd,pre .tag{font-weight:700}pre.prettyprint{display:block;background-color:#333}pre .nocode{background-color:none;color:#000}pre .str{color:#ffa0a0}pre .kwd{color:khaki}pre .com{color:#87ceeb}pre .typ{color:#98fb98}pre .lit{color:#cd5c5c}pre .pln,pre .pun{color:#fff}pre .tag{color:khaki}pre .atn{color:#bdb76b}pre .atv{color:#ffa0a0}pre .dec{color:#98fb98}ol.linenums{margin-top:0;margin-bottom:0;color:#AEAEAE}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}@media print{pre.prettyprint{background-color:none}code .str,pre .str{color:#060}code .kwd,pre .kwd{color:#006;font-weight:700}code .com,pre .com{color:#600;font-style:italic}code .typ,pre .typ{color:#404;font-weight:700}code .lit,pre .lit{color:#044}code .pun,pre .pun{color:#440}code .pln,pre .pln{color:#000}code .tag,pre .tag{color:#006;font-weight:700}code .atn,pre .atn{color:#404}code .atv,pre .atv{color:#060}}</style>
For more themes, visit [Prettify themes](http://www.compromath.com/2017/02/adding-specific-code-syntax-highlighter.html). When creating a post, switch from visual mode to **HTML** editing and navigate to where you want to insert the code snippet. Then paste the code like this:
<pre class="prettyprint">
  <code class="language-html">
      <!-- Your code snippet here -->
  </code>
</pre>
You can customize the code style by choosing different languages like HTML, CSS, PHP, JavaScript, etc. In this example, an **HTML** code snippet is used.

Answer №11

My method is straightforward and offers flexibility.

To access your Blogger account, go to ThemeCustomizeAdvancedAdd CSS.

Next, paste the CSS code provided below.

code {
    font-family: Courier, monospace;
    color: #000;
    background-color: #f8f9fa;
    border: 1px solid #eaecf0;
    border-radius: 2px;
    padding: 1px 4px;
}
pre, .mw-code {
    padding: 5px;
    font-family: Courier, monospace;
    font-size: inherit;
    line-height: 1rem;
    color: #000;
    background-color: #f8f9fa;
    border: 1px solid #eaecf0;
    padding: 1em;
    white-space: pre-wrap;
    overflow-x: hidden;
    word-wrap: break-word;
}

For example, to execute a command like this:

To view the MX record of a domain on Windows <code> nslookup </code> :<br />

<pre>temp = foo
foo = bar
bar = temp
</pre>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered the error message "A property 'split' cannot be read of undefined" while attempting to run a React Native application

I was able to run a react-native app without any issues yesterday, but today when I tried to run it again, I encountered some problems. After running "npm start" to start metro, I then attempted to run "npx react-native run-android". However, I received th ...

The Vue.js development server compiler is hitting a roadblock at 49% progress

My Vue JS project keeps getting stuck at 49% or sometimes 62% when I run npm run serve. No matter how long I wait, it never seems to move past that point. I have searched online many times for a solution to this issue, but it appears that no one else is e ...

Updating views with AJAX after database updates without the use of jQuery via PHP

Learning MVC and Ajax has been quite an adventure for me. I decided to build a site using PHP and MySQL, where I successfully implemented a new controller method to handle ajax requests. With the help of Ajax HTTPRequest, I am now able to update my databas ...

Error encountered in MySQL and NodeJS: Unable to add new query after invoking quit with transactions

While working on implementing MySQL for NodeJS and Restify, I encountered a flawless experience with queries. However, when attempting to utilize data updating functionality through transactions, I faced the error message: Error: Cannot enqueue Query after ...

The filter function in JavaScript's Array is malfunctioning on Internet Explorer version 7

My web application includes a jQuery plugin that is functioning correctly in Internet Explorer 10 and 11. However, it is not working in IE 7. Upon investigation, I discovered that the value of the filter method is showing as undefined. The line of code th ...

When it comes to successful payments, should you use `checkout.session.async_payment_succeeded` or `checkout.session.completed` in the Stripe event?

I'm feeling a little uncertain about the differences between two events: checkout.session.async_payment_succeeded & checkout.session.completed Currently, I am utilizing checkout.session.completed, but I'm wondering if checkout.session.async ...

What is the best way to validate if fields are blank before sending a message using the button?

<template> <div> <div class="form-group"> <label for="name">First Name</label> <input type="text" class="form-control" v-model="firstName" placeholder="Ente ...

Click a button to switch the visibility of a section in an

Is there a way to create a toggle feature in Angular.JS that will open and close a div when clicked on the same div again? Currently, it opens the div when clicked but I want it to also close the div when clicked again. <a ng-href ng-click="openAccordi ...

Is there a way to tally up the overall count of digits in a number using TypeScript?

Creating a number variable named temp in TypeScript: temp: number = 0.123; Is there a way to determine the total count of digits in a number (in this case, it's 3)? ...

A Vue.js component modifies one store state variable but leaves another unchanged

Currently developing a Vue 3 application that utilizes Vuex and the Composition API. Encountered an issue while working on it. A component is supposed to display elements based on the state of two store variables. One is an array, and the other is a bool ...

`res.render when all data queries are completed``

When I make an app.get request in my server.js file, I retrieve a dataset from MongoDB and then render my page while passing the data to it. Here is an example: //page load app.get('/', (req, res) => { //find all data in test table ...

Can a promise be safely resolved more than once?

In my application, there is an i18n service that includes the following code snippet: var i18nService = function() { this.ensureLocaleIsLoaded = function() { if( !this.existingPromise ) { this.existingPromise = $q.defer(); var deferred ...

New update: Adjusting the chart by using the slider to modify the date values on the x-axis

Update: I have made changes to the question, the issue with values not appearing on the x-axis has been resolved. Now, as the slider is moved, the data on the graph remains constant and I can see changing x values. I am currently working on incorporating ...

What is the best way to adjust the size of a button using CSS within a ReactJS

I am facing an issue where I need to create a button with a specific width, but the template I'm using already has predefined styles for buttons. When I try to customize the button's style, nothing seems to change. Below is the code snippet: Her ...

Incorporating fresh JSON information into an established database

I'm currently facing an issue with adding new data (from JSON) to an existing table using jQuery. Within my HTML, there's a sample table structure like this: <table data-role="table" data-mode="columntoggle" class="ui-responsive" data-column ...

Is there a way to incorporate a base64 encoded image from a text file into an HTML image?

I have several images stored as base64 blobs in a remote location. Let's focus on one specific image: llorkcir.txt data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEhASEBAQDxAQDw8QEA8PEA8PDw0PFRIWFhURFRUYHSggGBolGxUVITEhJSkrLi4uFx8zODMt ...

Encountering issues with ASP.NET WebAPI: When using $.ajax, a 404 error occurs, while using $.getJSON results in an Uncaught

Currently, I am developing an ASP.NET web API with a C# project that I am attempting to call from JavaScript. Below is the snippet of my JavaScript code: function LoadGraph() { var file = document.getElementById("file-datas"); if ('files' in fi ...

Enhanced Rating System for Asp.Net

How can I retrieve the selected value (CurrentValue) of the ASP.NET Rating control in javascript? I have it implemented within a datagrid, and I am facing difficulty in accessing the CurrentValue property. Any suggestions or solutions for this issue woul ...

Converting a ReadStream to a ReadableStream in NodeJS: A Step-by-Step Guide

This question poses the opposite scenario to converting a ReadableStream into a ReadStream. ReadStream is a data structure utilized in Node.js ReadableStream is a structure that comes from the web platform, as detailed here As new runtimes like Deno or t ...

How to Load JSON Data Using D3 When Column Definitions are Separated

I'm currently working with d3.js and struggling to grasp how I can effectively load a JSON file representing a table that has separate column definitions. A typical JSON file, which I have no issue loading, might appear like this: [ { "id": 1, ...