Meta:Guides/Style Guide/Code

提供: wiki
移動先: 案内検索

テンプレート:Meta:Guides/index


Code examples (python, c/c++, etc) should be marked-up as below.

Single line of code

<code> tag

<code>from math import *</code>

returns:
from math import *

Note: The <code> tag can be used inline, which means that it can be used inside of a paragraph of text and it will not force a newline to be outputted on the paragraph of text it is used in. However the code tag does not (yet) support color syntax highlight. So if you want to use color syntax highlighting and the text you wish to highlight will appear on its own separate line please use the <source> tag in future.

Multiple lines of code

<pre> tag

<pre>
line 1
line 2
...
</pre>

returns

line 1
line 2
...

Starting a line with a space

By putting one or more spaces in front of each line, lines are shown as below:

line 1
line 2
...

Syntax highlighting

You have to enclose your code into <source lang="your-language"> and </source> where "your-language" can be "python", "c", etc.

Note: At the moment the <source> tag is a block level tag (none inline), which mean that when it is used it will automatically put the text it is highlighting on it own separate line.

Python

This wikitext

<source lang="python">

...python code here...

</source>

returns

# <pep8 compliant>

from _bpy import types as bpy_types
from Mathutils import Vector

import os, sys

StructRNA = bpy_types.Struct.__bases__[0]
# StructRNA = bpy_types.Struct


class Context(StructRNA):
    __slots__ = ()

    def copy(self):
        new_context = {}
        generic_keys = StructRNA.__dict__.keys()
        for item in dir(self):
            if item not in generic_keys:
                new_context[item] = getattr(self, item)

        return new_context


class Object(bpy_types.ID):
    __slots__ = ()

    @property
    def children(self):
        """All the children of this object"""
        import bpy
        return [child for child in bpy.data.objects if child.parent == self]

    @property
    def group_users(self):
        """The groups this object is in"""
        import bpy
        name = self.name
        return [group for group in bpy.data.groups if name in group.objects]

    @property
    def scene_users(self):
        """The scenes this object is in"""
        import bpy
        name = self.name
        return [scene for scene in bpy.data.scenes if name in scene.objects]

C

This wikitext

<source lang="c">

...C code here...

</source>

returns

**
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Blender Foundation 2009.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>


/************************ Structs and Defines *************************/

#define RNA_NO_INDEX	-1
#define RNA_ENUM_VALUE	-2

#define EM_SEPR_X		6
#define EM_SEPR_Y		6

/* uiLayoutRoot */

typedef struct uiLayoutRoot {
	struct uiLayoutRoot *next, *prev;

	int type;
	int opcontext;

	int emw, emh;

	uiMenuHandleFunc handlefunc;
	void *argv;

	uiStyle *style;
	uiBlock *block;
	uiLayout *layout;
} uiLayoutRoot;

/* Item */

typedef enum uiItemType {
	ITEM_BUTTON,

	ITEM_LAYOUT_ROW,
	ITEM_LAYOUT_COLUMN,
	ITEM_LAYOUT_COLUMN_FLOW,
	ITEM_LAYOUT_ROW_FLOW,
	ITEM_LAYOUT_BOX,
	ITEM_LAYOUT_ABSOLUTE,
	ITEM_LAYOUT_SPLIT,
	ITEM_LAYOUT_OVERLAP,

	ITEM_LAYOUT_ROOT
#if 0
	TEMPLATE_COLUMN_FLOW,
	TEMPLATE_SPLIT,
	TEMPLATE_BOX,

	TEMPLATE_HEADER,
	TEMPLATE_HEADER_ID
#endif
} uiItemType;


struct uiLayout {
	uiItem item;

	uiLayoutRoot *root;
	bContextStore *context;
	ListBase items;

	int x, y, w, h;
	float scale[2];
	short space;
	char align;
	char active;
	char enabled;
	char redalert;
	char keepaspect;
	char alignment;
};


/************************** Item ***************************/

static char *ui_item_name_add_colon(char *name, char namestr[UI_MAX_NAME_STR])
{
	int len= strlen(name);

	if(len != 0 && len+1 < UI_MAX_NAME_STR) {
		BLI_strncpy(namestr, name, UI_MAX_NAME_STR);
		namestr[len]= ':';
		namestr[len+1]= '\0';
		return namestr;
	}

	return name;
}

static int ui_item_fit(int item, int pos, int all, int available, int last, int alignment, int *offset)
{
	/* available == 0 is unlimited */
	if(available == 0)
		return item;
	
	if(offset)
		*offset= 0;
	
	if(all > available) {
		/* contents is bigger than available space */
		if(last)
			return available-pos;
		else
			return (item*available)/all;
	}
	else {
		/* contents is smaller or equal to available space */
		if(alignment == UI_LAYOUT_ALIGN_EXPAND) {
			if(last)
				return available-pos;
			else
				return (item*available)/all;
		}
		else
			return item;
	}
}

static int ui_layout_local_dir(uiLayout *layout)
{
	switch(layout->item.type) {
		case ITEM_LAYOUT_ROW:
		case ITEM_LAYOUT_ROOT:
		case ITEM_LAYOUT_OVERLAP:
			return UI_LAYOUT_HORIZONTAL;
		case ITEM_LAYOUT_COLUMN:
		case ITEM_LAYOUT_COLUMN_FLOW:
		case ITEM_LAYOUT_SPLIT:
		case ITEM_LAYOUT_ABSOLUTE:
		case ITEM_LAYOUT_BOX:
		default:
			return UI_LAYOUT_VERTICAL;
	}
}

C++

This wikitext

<source lang="cpp">

...c++ commands...

</source>

returns

#include <iostream.h>

// First definition and use of C++ class

/* This is file is a minimal class definition
and use in C++ - showing the most basic of
structure.

*/

class Hotel {
        public:
                int roomcount;
                float occrate;
};

int main () {
        Hotel manor;
        Hotel beechfield;
        manor.roomcount = 6;
        beechfield.roomcount = 18;
        manor.occrate = 0.85;
        beechfield.occrate = 0.35;

        int totrooms = manor.roomcount + beechfield.roomcount;

        cout << "Total rooms listed: " << totrooms << "\n" ;

        return 0;
        }

Linux

Bash

This wikitext

<source lang="bash">

...bash commands...

</source>

returns

ls
dir
date
scons
svn co


Windows

DOS

This wikitext

<source lang="dos">

...dos commands...

</source>

returns

DIR C:\
TYPE "CHEESE.TXT"
RMDIR C:\BOOT.TXT
FORMAT C:\
EDIT MSDOS.SYS

INI

This wikitext

<source lang="ini">

...ini formatted commands...

</source>

returns

[USERS]
MARY = c:\HOME\MARY
JOHN = \HOME\JOHN

[DESKTOPBACKGROUND]
FORMAT = 8BIT
LOCATION = C:\WINDOWS\BACKGROUNDS\PICTURE.BMP

Web languages

PHP

This wikitext

<source lang="php">

...php commands...

</source>

returns

<?
$today = date("l");
print("$today");
if($today == "Sunday")
{
$bgcolor = "#FEF0C5";
}
elseif($today == "Monday")
{
$bgcolor = "#FFFFFF";
}
elseif($today == "Tuesday")
{
$bgcolor = "#FBFFC4";
}
elseif($today == "Wednesday")
{
$bgcolor = "#FFE0DD";
}
elseif($today == "Thursday")
{
$bgcolor = "#E6EDFF";
}
elseif($today == "Friday")
{
$bgcolor = "#E9FFE6";
}
else
{
// Since it is not any of the days above it must be Saturday
$bgcolor = "#F0F4F1";
}
print("<body bgcolor=\"$bgcolor\">\n");
?>

Javascript

This wikitext

<source lang="javascript">

...javascript commands...

</source>

returns

quicksort(int a[], int lo, int hi)
/* sort a[lo..hi] */
 { int left, right, median, temp;

   if( hi > lo ) /* i.e. at least 2 elements, then */
    { left=lo; right=hi;
      median=a[lo];  /* NB. just an estimate! */

      while(right >= left) /* partition a[lo..hi] */
      /* a[lo..left-1]<=median and a[right+1..hi]>=median */
       { while(a[left] < median) left++;
         /* a[left] >= median */

         while(a[right] > median) right--;
         /* a[left] >= median >= a[right] */

         if(left > right) break;

         //swap:
         temp=a[left]; a[left]=a[right]; a[right]=temp;
         left++; right--
       }
      /* a[lo..left-1]<=median and a[right+1..hi]>=median
         and left > right */

      quicksort(a, lo, right);// divide and conquer
      quicksort(a, left,  hi);
    }
 }/*quicksort*/


function quick(a, N)
/* sort a[1..N], N.B. 1 to N */
 { quicksort(a, 1, N); }

CSS

This wikitext

<source lang="css">

...css...

</source>

returns

label{
float: left;
width: 120px;
font-weight: bold;
}

input, textarea{
width: 180px;
margin-bottom: 5px;
}

textarea{
width: 250px;
height: 150px;
}

.boxes{
width: 1em;
}

#submitbutton{
margin-left: 120px;
margin-top: 5px;
width: 90px;
}

br{
clear: left;
}

Text

This wikitext

<source lang="text">

...text...

</source>

returns

Mary had a little lamb,
Little lamb, little lamb,
Mary had a little lamb,
Its fleece was white as snow

Everywhere that Mary went,
Mary went, Mary went,
Everywhere that Mary went
The lamb was sure to go

It followed her to school one day
School one day, school one day
It followed her to school one day
Which was against the rules.

It made the children laugh and play,
Laugh and play, laugh and play,
It made the children laugh and play
To see a lamb at school

And so the teacher turned it out,
Turned it out, turned it out,
And so the teacher turned it out,
But still it lingered near

And waited patiently about,
Patiently about, patiently about,
And waited patiently about
Till Mary did appear

"Why does the lamb love Mary so?"
Love Mary so? Love Mary so?
"Why does the lamb love Mary so?"
The eager children cry

"Why, Mary loves the lamb, you know."
Loves the lamb, you know, loves the lamb, you know
"Why, Mary loves the lamb, you know."
The teacher did reply